Crates.io | og-enum-to-string |
lib.rs | og-enum-to-string |
version | |
source | src |
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 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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.