Better use of From/Tryfrom within the blueprint module.

This commit is contained in:
KtorZ
2023-01-31 09:58:09 +01:00
parent 22a1c1dfb4
commit 4588ccd040
2 changed files with 17 additions and 11 deletions

View File

@@ -78,7 +78,11 @@ impl Validator {
validator: &CheckedModule,
def: &TypedFunction,
) -> Result<Validator, Error> {
let purpose: Purpose = def.name.clone().into();
let purpose: Purpose = def
.name
.clone()
.try_into()
.expect("unexpected validator name");
assert_return_bool(validator, def)?;
assert_min_arity(validator, def, purpose.min_arity())?;
@@ -141,14 +145,16 @@ impl Display for Purpose {
}
}
impl From<String> for Purpose {
fn from(purpose: String) -> Purpose {
impl TryFrom<String> for Purpose {
type Error = String;
fn try_from(purpose: String) -> Result<Purpose, Self::Error> {
match &purpose[..] {
"spend" => Purpose::Spend,
"mint" => Purpose::Mint,
"withdraw" => Purpose::Withdraw,
"publish" => Purpose::Publish,
unexpected => panic!("Can't turn '{}' into any Purpose", unexpected),
"spend" => Ok(Purpose::Spend),
"mint" => Ok(Purpose::Mint),
"withdraw" => Ok(Purpose::Withdraw),
"publish" => Ok(Purpose::Publish),
unexpected => Err(format!("Can't turn '{}' into any Purpose", unexpected)),
}
}
}