7 releases
0.1.6 | Mar 25, 2023 |
---|---|
0.1.5 | Mar 24, 2023 |
#302 in Procedural macros
2,106 downloads per month
9KB
118 lines
enum2str
enum2str is a rust derive macro that creates a Display impl for enums. This is useful for strongly typing composable sets of strings.
Usage
Add this to your Cargo.toml
:
enum2str = "0.1.6"
Example:
use enum2str::EnumStr;
#[derive(EnumStr)]
enum Object {
Generic(String),
#[enum2str("Color: {}. Shape: {}.")]
Complex(Color, Shape),
}
#[derive(EnumStr)]
enum Color {
Green,
#[enum2str("Burgundy")]
Red,
}
#[derive(EnumStr)]
enum Shape {
#[enum2str("Circle with radius: {}")]
Circle(u8),
}
#[test]
fn unit_to_string() {
assert_eq!(Color::Green.to_string(), "Green");
}
#[test]
fn unit_override_string() {
assert_eq!(Color::Red.to_string(), "Burgundy");
}
#[test]
fn unnamed_to_string() {
assert_eq!(Object::Generic("Hello!".to_string()).to_string(), "Hello!");
}
#[test]
fn nested_to_string() {
assert_eq!(
Object::Complex(Color::Green, Shape::Circle(2)).to_string(),
"Color: Green. Shape: Circle with radius: 2."
);
}
#[test]
fn unit_template() {
assert_eq!(Color::Green.template(), "Green");
}
#[test]
fn unit_override_template() {
assert_eq!(Color::Red.template(), "Burgundy");
}
#[test]
fn unnamed_template() {
assert_eq!(Shape::Circle(2).template(), "Circle with radius: {}");
}
#[test]
fn nested_template() {
assert_eq!(
Object::Complex(Color::Green, Shape::Circle(2)).template(),
"Color: {}. Shape: {}."
);
}
#[test]
fn unit_args() {
assert_eq!(Color::Green.arguments().len(), 0);
}
#[test]
fn unnamed_args() {
assert_eq!(
Object::Generic("Hello!".to_string()).arguments(),
vec!["Hello!".to_string()]
);
}
#[test]
fn complex_args() {
assert_eq!(
Object::Complex(Color::Green, Shape::Circle(2)).arguments(),
vec!["Green", "Circle with radius: 2"],
);
}
Dependencies
~0.9–1.2MB
~30K SLoC