1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use ::std::fmt::{Display, Formatter};
use ::std::error::Error;
type Result = ::std::fmt::Result;
#[derive(Debug, Clone, Copy)]
pub struct ParseEnumVariantError {
enum_name: &'static str,
}
impl ParseEnumVariantError {
pub fn new(enum_name: &'static str) -> Self {
Self { enum_name }
}
}
impl Display for ParseEnumVariantError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "Cannot convert string to {}", self.enum_name)
}
}
impl Error for ParseEnumVariantError {
fn description(&self) -> &str {
"Cannot convert string to enum"
}
}
#[cfg(test)]
mod test {
use super::ParseEnumVariantError;
use enum_from_str_derive::FromStr;
#[derive(FromStr)]
enum Test {
#[from_str="foo"]
Foo,
Bar,
}
#[test]
pub fn test_enum() {
"foo".parse::<Test>().unwrap();
"Bar".parse::<Test>().unwrap();
}
}