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
+3 -3
View File
@@ -39,12 +39,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
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)),
}
}
}
+1 -1
View File
@@ -91,5 +91,5 @@ enum Animal {
// Invalid conversions
let animal = "nya".parse::<Animal>();
assert!(matches!(animal, Err(())));
assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err());
}