| Crates.io | enum_stringify |
| lib.rs | enum_stringify |
| version | 0.6.4 |
| created_at | 2023-09-13 11:03:00.028246+00 |
| updated_at | 2025-06-08 23:54:01.253493+00 |
| description | Macro to generate string conversion functions for enums |
| homepage | https://github.com/Yag000/enum_stringify |
| repository | https://github.com/Yag000/enum_stringify |
| max_upload_size | |
| id | 971388 |
| size | 67,097 |
A procedural macro crate to generate string representations of Rust enums.
Derive EnumStringify to automatically implement Display, TryFrom<&str>, TryFrom<String>, and FromStr for your enum, using the variant name as the string representation.
Display, FromStr, and TryFrom for enums.use enum_stringify::EnumStringify;
use std::convert::TryFrom;
use std::str::FromStr;
#[derive(EnumStringify)]
enum MyEnum {
Variant1,
Variant2,
Variant3,
}
fn main() {
// Display
println!("{}", MyEnum::Variant1); // Prints "Variant1"
// To string
assert_eq!(MyEnum::Variant1.to_string(), "Variant1");
// TryFrom<&str>
assert_eq!(MyEnum::try_from("Variant2").unwrap(), MyEnum::Variant2);
// TryFrom<String>
assert_eq!(MyEnum::try_from("Variant3".to_string()).unwrap(), MyEnum::Variant3);
// FromStr
assert_eq!(MyEnum::from_str("Variant1").unwrap(), MyEnum::Variant1);
}
You can customize the string representation using attributes for prefix, suffix, and case.
use enum_stringify::EnumStringify;
#[derive(EnumStringify)]
#[enum_stringify(prefix = "MyPrefix", suffix = "MySuffix", case = "upper_flat")]
enum MyEnum {
Variant1,
Variant2,
Variant3,
}
In this example, the string representation of MyEnum::Variant1 will be MyPrefixVARIANT1MySuffix.
"upper_flat", "lower_flat", etc.)This project is licensed under the MIT License. See LICENSE for details.