1
Fork 0

add error message to string_enum!s string conversions

This commit is contained in:
Rémy Rakic 2025-01-12 08:13:45 +00:00
parent d19bdf9b48
commit 33ce74f308
2 changed files with 4 additions and 4 deletions

View file

@ -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)),
}
}
}

View file

@ -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());
}