og-enum-to-string

Crates.ioog-enum-to-string
lib.rsog-enum-to-string
version0.1.0
created_at2025-03-04 02:23:01.365332+00
updated_at2025-03-21 05:47:34.693421+00
descriptionProvides the `EnumToString` derive macro which implements the methods `as_str()` and `as_dbg()` to enums with no associated values
homepagehttps://github.com/gabrielfalcao/enum-to-string
repository
max_upload_size
id1576646
size12,032
Gabriel Falcão (gabrielfalcao)

documentation

README

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.

Commit count: 0

cargo fmt