forked from Peternator7/strum
/
from_str.rs
127 lines (111 loc) · 2.68 KB
/
from_str.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extern crate strum;
#[macro_use]
extern crate strum_macros;
use std::str::FromStr;
#[derive(Debug, Eq, PartialEq, EnumString)]
enum Color {
Red,
Blue {
hue: usize,
},
#[strum(serialize = "y", serialize = "yellow")]
Yellow,
#[strum(default = "true")]
Green(String),
#[strum(to_string = "purp")]
Purple,
}
#[test]
fn color_simple() {
assert_eq!(Color::Red, Color::from_str("Red").unwrap());
}
#[test]
fn color_value() {
assert_eq!(Color::Blue { hue: 0 }, Color::from_str("Blue").unwrap());
}
#[test]
fn color_serialize() {
assert_eq!(Color::Yellow, Color::from_str("y").unwrap());
assert_eq!(Color::Yellow, Color::from_str("yellow").unwrap());
}
#[test]
fn color_to_string() {
assert_eq!(Color::Purple, Color::from_str("purp").unwrap());
}
#[test]
fn color_default() {
assert_eq!(
Color::Green(String::from("not found")),
Color::from_str("not found").unwrap()
);
}
#[derive(Debug, Eq, PartialEq, EnumString)]
#[strum(serialize_all = "snake_case")]
enum Brightness {
DarkBlack,
Dim {
glow: usize,
},
#[strum(serialize = "Bright")]
BrightWhite,
}
#[test]
fn brightness_serialize_all() {
assert_eq!(
Brightness::DarkBlack,
Brightness::from_str("dark_black").unwrap()
);
assert_eq!(
Brightness::Dim { glow: 0 },
Brightness::from_str("dim").unwrap()
);
assert_eq!(
Brightness::BrightWhite,
Brightness::from_str("Bright").unwrap()
);
}
#[derive(Debug, Eq, PartialEq, EnumString)]
enum Week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}
#[test]
fn week_not_found() {
assert_eq!(
Result::Err(::strum::ParseError::VariantNotFound),
Week::from_str("Humpday")
);
}
#[test]
fn week_found() {
assert_eq!(Result::Ok(Week::Sunday), Week::from_str("Sunday"));
assert_eq!(Result::Ok(Week::Monday), Week::from_str("Monday"));
assert_eq!(Result::Ok(Week::Tuesday), Week::from_str("Tuesday"));
assert_eq!(Result::Ok(Week::Wednesday), Week::from_str("Wednesday"));
assert_eq!(Result::Ok(Week::Thursday), Week::from_str("Thursday"));
assert_eq!(Result::Ok(Week::Friday), Week::from_str("Friday"));
assert_eq!(Result::Ok(Week::Saturday), Week::from_str("Saturday"));
}
#[derive(Debug, Eq, PartialEq, EnumString)]
enum Lifetime<'a> {
Life(&'a str),
None,
}
#[test]
fn lifetime_test() {
assert_eq!(Lifetime::Life(""), Lifetime::from_str("Life").unwrap());
}
#[derive(Debug, Eq, PartialEq, EnumString)]
enum Generic<T: Default> {
Gen(T),
None,
}
#[test]
fn generic_test() {
assert_eq!(Generic::Gen(""), Generic::from_str("Gen").unwrap());
}