2017-02-16 04:16:40 +01:00
|
|
|
use std::str::FromStr;
|
2020-09-17 20:21:21 +02:00
|
|
|
use strum::EnumString;
|
2017-02-16 04:16:40 +01:00
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString)]
|
2017-02-16 04:16:40 +01:00
|
|
|
enum Color {
|
|
|
|
Red,
|
2018-09-26 21:13:45 +02:00
|
|
|
Blue {
|
|
|
|
hue: usize,
|
|
|
|
},
|
|
|
|
#[strum(serialize = "y", serialize = "yellow")]
|
2017-02-16 04:16:40 +01:00
|
|
|
Yellow,
|
2020-07-29 18:36:48 +02:00
|
|
|
#[strum(default)]
|
2017-02-16 04:16:40 +01:00
|
|
|
Green(String),
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(to_string = "purp")]
|
2017-04-16 04:01:58 +02:00
|
|
|
Purple,
|
2021-05-03 05:59:48 +02:00
|
|
|
#[strum(serialize = "blk", serialize = "Black", ascii_case_insensitive)]
|
|
|
|
Black,
|
2017-02-16 04:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
|
2017-04-16 04:01:58 +02:00
|
|
|
#[test]
|
|
|
|
fn color_to_string() {
|
|
|
|
assert_eq!(Color::Purple, Color::from_str("purp").unwrap());
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:16:40 +01:00
|
|
|
#[test]
|
|
|
|
fn color_default() {
|
2018-09-26 21:13:45 +02:00
|
|
|
assert_eq!(
|
|
|
|
Color::Green(String::from("not found")),
|
|
|
|
Color::from_str("not found").unwrap()
|
|
|
|
);
|
2017-02-16 04:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 05:59:48 +02:00
|
|
|
#[test]
|
|
|
|
fn color_ascii_case_insensitive() {
|
|
|
|
assert_eq!(Color::Black, Color::from_str("BLK").unwrap());
|
|
|
|
assert_eq!(Color::Black, Color::from_str("bLaCk").unwrap());
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[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)]
|
2017-02-16 04:16:40 +01:00
|
|
|
enum Week {
|
|
|
|
Sunday,
|
|
|
|
Monday,
|
|
|
|
Tuesday,
|
|
|
|
Wednesday,
|
|
|
|
Thursday,
|
|
|
|
Friday,
|
|
|
|
Saturday,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn week_not_found() {
|
2018-09-26 21:13:45 +02:00
|
|
|
assert_eq!(
|
|
|
|
Result::Err(::strum::ParseError::VariantNotFound),
|
|
|
|
Week::from_str("Humpday")
|
|
|
|
);
|
2017-02-16 04:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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"));
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString)]
|
2017-02-16 04:16:40 +01:00
|
|
|
enum Lifetime<'a> {
|
|
|
|
Life(&'a str),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lifetime_test() {
|
|
|
|
assert_eq!(Lifetime::Life(""), Lifetime::from_str("Life").unwrap());
|
|
|
|
}
|
2017-02-16 06:16:16 +01:00
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString)]
|
2017-02-16 06:16:16 +01:00
|
|
|
enum Generic<T: Default> {
|
|
|
|
Gen(T),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_test() {
|
|
|
|
assert_eq!(Generic::Gen(""), Generic::from_str("Gen").unwrap());
|
|
|
|
}
|
2021-05-03 05:59:48 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString)]
|
|
|
|
#[strum(ascii_case_insensitive)]
|
|
|
|
enum CaseInsensitiveEnum {
|
|
|
|
NoAttr,
|
|
|
|
#[strum(ascii_case_insensitive = false)]
|
|
|
|
NoCaseInsensitive,
|
|
|
|
#[strum(ascii_case_insensitive = true)]
|
|
|
|
CaseInsensitive,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_enum_no_attr() {
|
|
|
|
assert_eq!(
|
|
|
|
CaseInsensitiveEnum::NoAttr,
|
|
|
|
CaseInsensitiveEnum::from_str("noattr").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_enum_no_case_insensitive() {
|
|
|
|
assert_eq!(
|
|
|
|
CaseInsensitiveEnum::NoCaseInsensitive,
|
|
|
|
CaseInsensitiveEnum::from_str("NoCaseInsensitive").unwrap(),
|
|
|
|
);
|
|
|
|
assert!(CaseInsensitiveEnum::from_str("nocaseinsensitive").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_enum_case_insensitive() {
|
|
|
|
assert_eq!(
|
|
|
|
CaseInsensitiveEnum::CaseInsensitive,
|
|
|
|
CaseInsensitiveEnum::from_str("CaseInsensitive").unwrap(),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
CaseInsensitiveEnum::CaseInsensitive,
|
|
|
|
CaseInsensitiveEnum::from_str("caseinsensitive").unwrap(),
|
|
|
|
);
|
|
|
|
}
|