mirror of
https://github.com/danog/strum.git
synced 2024-12-11 17:09:36 +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
68 lines
1.4 KiB
Rust
68 lines
1.4 KiB
Rust
extern crate strum;
|
|
#[macro_use]
|
|
extern crate strum_macros;
|
|
|
|
use std::str::FromStr;
|
|
use std::string::ToString;
|
|
|
|
#[derive(Debug, Eq, PartialEq, EnumString, ToString)]
|
|
enum Color {
|
|
#[strum(to_string = "RedRed")]
|
|
Red,
|
|
#[strum(serialize = "b", to_string = "blue")]
|
|
Blue { hue: usize },
|
|
#[strum(serialize = "y", serialize = "yellow")]
|
|
Yellow,
|
|
#[strum(default = "true")]
|
|
Green(String),
|
|
}
|
|
|
|
#[test]
|
|
fn to_blue_string() {
|
|
assert_eq!(
|
|
String::from("blue"),
|
|
(Color::Blue { hue: 0 }).to_string().as_ref()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn to_yellow_string() {
|
|
assert_eq!(String::from("yellow"), (Color::Yellow).to_string().as_ref());
|
|
}
|
|
|
|
#[test]
|
|
fn to_red_string() {
|
|
assert_eq!(String::from("RedRed"), (Color::Red).to_string());
|
|
assert_eq!(
|
|
Color::Red,
|
|
Color::from_str((Color::Red).to_string().as_ref()).unwrap()
|
|
);
|
|
}
|
|
|
|
#[derive(Debug, Eq, PartialEq, ToString)]
|
|
#[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()
|
|
);
|
|
}
|