2017-08-24 04:18:23 +02:00
|
|
|
extern crate strum;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate strum_macros;
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
2018-05-26 19:57:04 +02:00
|
|
|
use strum::AsStaticRef;
|
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString, AsRefStr, AsStaticStr)]
|
2017-08-24 04:18:23 +02:00
|
|
|
enum Color {
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(to_string = "RedRed")]
|
2017-08-24 04:18:23 +02:00
|
|
|
Red,
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(serialize = "b", to_string = "blue")]
|
2017-08-24 04:18:23 +02:00
|
|
|
Blue { hue: usize },
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(serialize = "y", serialize = "yellow")]
|
2017-08-24 04:18:23 +02:00
|
|
|
Yellow,
|
2018-09-26 21:13:45 +02:00
|
|
|
#[strum(default = "true")]
|
2017-08-24 04:18:23 +02:00
|
|
|
Green(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_red_str() {
|
2018-06-26 17:22:19 +02:00
|
|
|
assert_eq!("RedRed", (Color::Red).as_ref());
|
|
|
|
assert_eq!(Color::Red, Color::from_str((Color::Red).as_ref()).unwrap());
|
2017-08-24 04:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_blue_str() {
|
2018-06-26 17:22:19 +02:00
|
|
|
assert_eq!("blue", (Color::Blue { hue: 0 }).as_ref());
|
2018-05-26 19:57:04 +02:00
|
|
|
let _: &'static str = (Color::Blue { hue: 0 }).as_static();
|
2017-08-24 04:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_yellow_str() {
|
|
|
|
assert_eq!("yellow", (Color::Yellow).as_ref());
|
2018-06-26 17:22:19 +02:00
|
|
|
let _: &'static str = (Color::Yellow).as_static();
|
2017-08-24 04:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_green_str() {
|
|
|
|
assert_eq!("Green", (Color::Green(String::default())).as_ref());
|
2018-06-26 17:22:19 +02:00
|
|
|
let _: &'static str = (Color::Green(String::default())).as_static();
|
2017-08-24 04:18:23 +02:00
|
|
|
}
|