Crates.io | enum-values |
lib.rs | enum-values |
version | 0.1.0 |
created_at | 2025-08-27 10:00:42.170261+00 |
updated_at | 2025-08-27 10:00:42.170261+00 |
description | Exposes enum values via reflection |
homepage | |
repository | https://gitlab.com/wiktor/enum-values |
max_upload_size | |
id | 1812396 |
size | 7,044 |
EnumValues
works with a specific subset of enums: these that have primitive discriminants.
The guiding use case was automatic generation of exit codes documentation from Error
enums.
use enum_values::EnumValues;
/// Mapping for relevant [`Error`] variants to an [`std::process::ExitCode`].
#[derive(EnumValues, Debug)]
#[repr(u8)]
pub enum Error {
/// IO error.
Io(std::io::Error) = 2,
/// Fmt error.
Fmt(std::fmt::Error) = 3,
}
let variants = Error::variants().collect::<Vec<_>>();
eprintln!("Variants: {variants:#?}",);
assert_eq!(variants.len(), 2);
assert_eq!("IO error.", variants[0].doc);
assert_eq!("Io", variants[0].name);
assert_eq!(2, variants[0].value);
assert_eq!("Fmt error.", variants[1].doc);
assert_eq!("Fmt", variants[1].name);
assert_eq!(3, variants[1].value);
This project is licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.