#enums #string #macro-derive #macro

macro enum2str

enum2str is a rust derive macro that creates a Display impl for enums. This is useful for strongly typing composable sets of strings

17 releases

new 0.1.16 Mar 17, 2025
0.1.15 Mar 17, 2025
0.1.11 Nov 11, 2024
0.1.10 Dec 8, 2023
0.1.6 Mar 25, 2023

#2543 in Procedural macros

Download history 496/week @ 2024-11-25 78/week @ 2024-12-02 309/week @ 2024-12-09 62/week @ 2024-12-16 162/week @ 2024-12-23 121/week @ 2024-12-30 121/week @ 2025-01-06 132/week @ 2025-01-13 219/week @ 2025-01-20 46/week @ 2025-01-27 255/week @ 2025-02-03 281/week @ 2025-02-10 115/week @ 2025-02-17 217/week @ 2025-02-24 251/week @ 2025-03-03 146/week @ 2025-03-10

733 downloads per month
Used in chdb

MIT license

24KB
341 lines

enum2str

github crates.io docs.rs

enum2str is a rust derive macro that creates a Display impl for enums. This is useful for strongly typing composable sets of strings. The crate emits no-std compatible code by default.

Features

  • try_from_string: Enables TryFrom<String> implementation for enums with only unit variants. This feature is not enabled by default and requires std. To enable it, use:

    enum2str = { version = "0.1.16", features = ["try_from_string"] }
    

Usage

Add this to your Cargo.toml:

enum2str = "0.1.16"

Changelog

0.1.16

  • Fixed ambiguity issue with TryFrom implementation when enum has a variant named Error

0.1.15

  • Fixed ambiguity issue with TryFrom implementation when enum has a variant named Error

Example:

use enum2str::EnumStr;

#[derive(EnumStr)]
enum Object {
    Generic(String),

    #[enum2str("Color: {}. Shape: {}.")]
    Complex(Color, Shape),

    // Variant fields can be ignored
    #[enum2str("Material")]
    Material(Color),
}

#[derive(EnumStr)]
enum Color {
    Green,

    #[enum2str("Burgundy")]
    Red,

    Blue {
        _hue: u8,
    },

    #[enum2str("Custom Color")]
    Custom {
        _red: u8,
        _green: u8,
        _blue: u8,
    },

    #[enum2str("Unique - {label}_{id}")]
    Unique {
        id: u8,
        label: String,
    },
}

#[derive(EnumStr)]
enum Shape {
    #[enum2str("Circle with radius: {}")]
    Circle(u8),
}

#[test]
fn color_variant_names() {
    assert_eq!(
        Color::variant_names(),
        vec![
            "Green".to_string(),
            "Red".to_string(),
            "Blue".to_string(),
            "Custom".to_string(),
            "Unique".to_string(),
        ]
    );
}

#[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"],
    );
}

#[test]
fn plain_to_string() {
    assert_eq!(Color::Blue { _hue: 3 }.to_string(), "Blue");
}

#[test]
fn unique_to_string() {
    assert_eq!(
        Color::Unique {
            label: "unique_color".to_string(),
            id: 3
        }
        .to_string(),
        "Unique - unique_color_3",
    );
}

#[test]
fn custom_args() {
    assert_eq!(
        Color::Unique {
            label: "unique_color".to_string(),
            id: 3
        }
        .arguments()
        .len(),
        2
    );
}

Dependencies

~1.5MB
~38K SLoC