#enums #macro-derive #string #traits #associated #values #enum-to-string

enum-to-string-traits

Provides the EnumToString derive macro which implements the methods as_str() and as_dbg() to enums with no associated values

2 unstable releases

Uses new Rust 2024

new 0.1.0 Mar 21, 2025
0.0.1 Mar 4, 2025

#23 in #associated

Download history 77/week @ 2025-02-26 56/week @ 2025-03-05 7/week @ 2025-03-12

140 downloads per month
Used in og-enum-to-string

MIT license

2KB

enum-to-string (O.G.)

This crate provides the EnumToString derive macro.

The derive macro EnumToString implements the methods variants(), as_str() and as_dbg() to enums with no associated values

EnumToString also the traits std::fmt::Display and std::fmt::Debug by default and can switched on/off through the #[enum_to_string()] attribute-like macro.

Example

Turning off both std::fmt::Display and std::fmt::Debug trait implementations.

use enum_to_string::EnumToString;
use enum_to_string::enum_to_string;


#[derive(EnumToString, Debug)]
#[enum_to_string(display = false, debug = false)]
enum Shell {
    Sh,
    Bash,
}

impl std::fmt::Display for Shell {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}::{}",
            module_path!(),
            self.as_dbg()
        )
    }
}



fn main() {
    println!("{}", dbg!(Shell::Sh.as_str()));
    println!("{}", dbg!(Shell::Bash.as_str()));
    println!("{}", dbg!(Shell::Sh.as_dbg()));
    println!("{}", dbg!(Shell::Bash.as_dbg()));
    println!("{}", dbg!(format!("{}", Shell::Sh)));
    println!("{}", dbg!(format!("{}", Shell::Bash)));
    println!("{}", dbg!(format!("{:#?}", Shell::Sh)));
    println!("{}", dbg!(format!("{:#?}", Shell::Bash)));
}

You can find this example as well as other examples in the examples directory.

No runtime deps