mirror of
https://github.com/danog/strum.git
synced 2024-12-03 18:07:52 +01:00
1c00f6cfa5
* Run rustfmt over repository. * Bump `syn` to 0.15 * Implemented ability to `serialize_all` using cases from `heck`. Issue #21 * Use `path` and `version` in dependency specifications. Issue #21 * Updated documentation. Issue #21 * Added `CHANGELOG.md`. * Also convert case when deriving `Display`. Issue #21 * Added `EnumDiscriminants` derive. Issue #33 * Added the ability to rename derived `EnumDiscriminants`. Issue #33 * Updated `README.md` and lib.rs docs. Issue #33 * Updated `CHANGELOG.md`. Issue #33 * WIP: refactoring to allow attributes on discriminants enum. * Use single `strum_discriminants` top level attribute. Issue #33 * Allow multiple declarations of `strum_discriminants` attribute. Issue #33 * Pass through all other attributes to discriminant enum. Issue #33 * Add `impl From<MyEnum> for MyEnumDiscriminants`. Issue #33 * Add `impl<'_enum> From<&'_enum MyEnum> for MyEnumDiscriminants`. Issue #33 * Added complex case test for `From` derivation. Issue #33 * Added docs to some helper functions. * Added docs about `From` impls. Issue #33
128 lines
2.7 KiB
Rust
128 lines
2.7 KiB
Rust
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());
|
|
}
|