| Crates.io | og-enum-to-string |
| lib.rs | og-enum-to-string |
| version | 0.1.0 |
| created_at | 2025-03-04 02:23:01.365332+00 |
| updated_at | 2025-03-21 05:47:34.693421+00 |
| description | Provides the `EnumToString` derive macro which implements the methods `as_str()` and `as_dbg()` to enums with no associated values |
| homepage | https://github.com/gabrielfalcao/enum-to-string |
| repository | |
| max_upload_size | |
| id | 1576646 |
| size | 12,032 |
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.
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.