Generates conversions of enums from strings and into strings based on variant identifiers
```rust use enumvariantsstrings::EnumVariantsStrings;
enum Variants { X, Y(i32), #[enumvariantsstrings_mappings("z", "zee")] Z { x: String, y: String, }, }
fn main() { asserteq!(Variants::fromstr("x"), Ok(Variants::X)); asserteq!(Variants::fromstr("y"), Ok(Variants::Y(0))); asserteq!( Variants::fromstr("z"), Ok(Variants::Z { x: String::default(), y: String::default(), }) );
assert_eq!(Variants::X.to_str(), "x");
assert_eq!(
Variants::Z {
x: "abc".into(),
y: "xyz".into()
}
.to_str(),
"zee"
);
} ```
By default variant identifier/names are transformed to their snake case version
This can be changed via #[enum_variants_strings_transform(transform = ...)]
```rust use enumvariantsstrings::EnumVariantsStrings;
enum EnumA { Foo, Bar, } ```
There are several transforms
"snake_case"
, separate uppercase and numeric boundaries with _
(default)"kebab_case"
, snake case with -
instead of underscores"upper_case"
, uppercase of identifier in source"lower_case"
, lowercase of identifier in source"none"
, no mapping from the identifier in the source