message: Add helpers for Contract and DynContract errors
diff: @@ -72,6 +72,14 @@ pub fn invalid<T>(field: &'static str, msg: &'static str) -> Result<T> {
  ValidationErr { field, msg }.fail()
  }
  +pub fn contract<T>(msg: &'static str) -> Result<T> {
  + ContractErr { msg }.fail()
  +}
  +
  +pub fn dyn_contract<T>(msg: String) -> Result<T> {
  + DynContractErr { msg }.fail()
  +}
  +
  #[cfg(test)]
  mod test {
  use super::*;
  @@ -88,4 +96,31 @@ mod test {
  Ok(_) => panic!("invalid must return error"),
  }
  }
  +
  + #[test]
  + // example of reporting static contract errors
  + fn contract_helper() {
  + let e: Result<()> = contract("not implemented");
  + match e {
  + Err(Error::ContractErr { msg, .. }) => {
  + assert_eq!(msg, "not implemented");
  + }
  + Err(e) => panic!("unexpected error, {:?}", e),
  + Ok(_) => panic!("invalid must return error"),
  + }
  + }
  +
  + #[test]
  + // example of reporting contract errors with format!
  + fn dyn_contract_helper() {
  + let guess = 7;
  + let e: Result<()> = dyn_contract(format!("{} is too low", guess));
  + match e {
  + Err(Error::DynContractErr { msg, .. }) => {
  + assert_eq!(msg, String::from("7 is too low"));
  + }
  + Err(e) => panic!("unexpected error, {:?}", e),
  + Ok(_) => panic!("invalid must return error"),
  + }
  + }
  }
  