enum-values 0.1.0

Exposes enum values via reflection
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 4 items with examples
  • Size
  • Source code size: 6.53 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 17s Average build duration of successful builds.
  • all releases: 17s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wiktor-k

enum-values

CI Crates.io

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);

License

This project is licensed under either of:

at your option.

Contribution

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.