mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
add error message to string_enum!s string conversions
This commit is contained in:
@@ -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)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user