2018-03-10 22:06:17 +01:00
|
|
|
extern crate strum;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate strum_macros;
|
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString, Display)]
|
2018-03-10 22:06:17 +01:00
|
|
|
enum Color {
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(to_string = "RedRed")]
|
2018-03-10 22:06:17 +01:00
|
|
|
Red,
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(serialize = "b", to_string = "blue")]
|
2018-03-10 22:06:17 +01:00
|
|
|
Blue { hue: usize },
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(serialize = "y", serialize = "yellow")]
|
2018-03-10 22:06:17 +01:00
|
|
|
Yellow,
|
2020-07-29 18:36:48 +02:00
|
|
|
#[strum(default)]
|
2018-03-10 22:06:17 +01:00
|
|
|
Green(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_blue_string() {
|
|
|
|
assert_eq!(String::from("blue"), format!("{}", Color::Blue { hue: 0 }));
|
|
|
|
}
|
|
|
|
|
2020-06-12 22:54:51 +02:00
|
|
|
#[test]
|
|
|
|
fn test_formatters() {
|
2020-07-29 18:36:48 +02:00
|
|
|
assert_eq!(
|
|
|
|
String::from(" blue"),
|
|
|
|
format!("{:>6}", Color::Blue { hue: 0 })
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
String::from("blue "),
|
|
|
|
format!("{:<6}", Color::Blue { hue: 0 })
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
String::from(" blue "),
|
|
|
|
format!("{:^6}", Color::Blue { hue: 0 })
|
|
|
|
);
|
2020-06-12 22:54:51 +02:00
|
|
|
assert_eq!(String::from("bl"), format!("{:.2}", Color::Blue { hue: 0 }));
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:06:17 +01:00
|
|
|
#[test]
|
|
|
|
fn to_yellow_string() {
|
|
|
|
assert_eq!(String::from("yellow"), format!("{}", Color::Yellow));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_red_string() {
|
|
|
|
assert_eq!(String::from("RedRed"), format!("{}", Color::Red));
|
|
|
|
}
|
2018-09-26 21:13:45 +02:00
|
|
|
|
|
|
|
#[derive(Display, Debug, Eq, PartialEq)]
|
|
|
|
#[strum(serialize_all = "snake_case")]
|
|
|
|
enum Brightness {
|
|
|
|
DarkBlack,
|
|
|
|
Dim {
|
|
|
|
glow: usize,
|
|
|
|
},
|
|
|
|
#[strum(serialize = "bright")]
|
|
|
|
BrightWhite,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn brightness_to_string() {
|
|
|
|
assert_eq!(
|
|
|
|
String::from("dark_black"),
|
|
|
|
Brightness::DarkBlack.to_string().as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
String::from("dim"),
|
|
|
|
Brightness::Dim { glow: 0 }.to_string().as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
String::from("bright"),
|
|
|
|
Brightness::BrightWhite.to_string().as_ref()
|
|
|
|
);
|
|
|
|
}
|