add error message to string_enum!
s string conversions
This commit is contained in:
parent
d19bdf9b48
commit
33ce74f308
2 changed files with 4 additions and 4 deletions
|
@ -39,12 +39,12 @@ macro_rules! string_enum {
|
|||
}
|
||||
|
||||
impl FromStr for $name {
|
||||
type Err = ();
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, ()> {
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
$($repr => Ok(Self::$variant),)*
|
||||
_ => Err(()),
|
||||
_ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,5 +91,5 @@ fn string_enums() {
|
|||
|
||||
// Invalid conversions
|
||||
let animal = "nya".parse::<Animal>();
|
||||
assert!(matches!(animal, Err(())));
|
||||
assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue