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-12-24 17:33:57 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString, AsRefStr, AsStaticStr, IntoStaticStr)]
|
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
|
|
|
}
|
2018-12-24 17:33:57 +01:00
|
|
|
|
|
|
|
#[derive(IntoStaticStr)]
|
|
|
|
enum Foo<'a> {
|
|
|
|
A,
|
|
|
|
C(&'a i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(IntoStaticStr)]
|
|
|
|
enum Boo<'a, T> {
|
|
|
|
A(T),
|
|
|
|
B,
|
|
|
|
C(&'a i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(IntoStaticStr)]
|
|
|
|
enum Moo<'a, T>
|
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
|
|
|
A(T),
|
|
|
|
B,
|
|
|
|
C(&'a i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_static_str() {
|
|
|
|
assert_eq!("RedRed", <&'static str>::from(Color::Red));
|
|
|
|
assert_eq!("blue", <&'static str>::from(Color::Blue { hue: 0 }));
|
|
|
|
assert_eq!("yellow", <&'static str>::from(Color::Yellow));
|
|
|
|
|
|
|
|
assert_eq!("RedRed", <&'static str>::from(&Color::Red));
|
|
|
|
assert_eq!("blue", <&'static str>::from(&Color::Blue { hue: 0 }));
|
|
|
|
assert_eq!("yellow", <&'static str>::from(&Color::Yellow));
|
|
|
|
|
|
|
|
assert_eq!("A", <&'static str>::from(Foo::A));
|
|
|
|
assert_eq!("C", <&'static str>::from(Foo::C(&17)));
|
|
|
|
|
|
|
|
assert_eq!("A", <&'static str>::from(Boo::A(17)));
|
|
|
|
assert_eq!("B", <&'static str>::from(Boo::B::<i32>));
|
|
|
|
assert_eq!("C", <&'static str>::from(Boo::C::<i32>(&17)));
|
|
|
|
|
|
|
|
assert_eq!("A", <&'static str>::from(Moo::A::<String>("aaa".into())));
|
|
|
|
assert_eq!("B", <&'static str>::from(Moo::B::<String>));
|
|
|
|
assert_eq!("C", <&'static str>::from(Moo::C::<String>(&17)));
|
|
|
|
}
|