Skip to main content

define_header_enum

Macro define_header_enum 

Source
macro_rules! define_header_enum {
    (
        $(tests_mod: $tests_mod:ident,)?
        error_type: $Err:ident $(=> $err_msg:literal)?,
        $(#[$enum_meta:meta])*
        $vis:vis enum $Name:ident {
            $(
                $(#[$var_meta:meta])*
                $variant:ident => $wire:literal
            ),+ $(,)?
        }
    ) => { ... };
}
Expand description

Generates a non-exhaustive enum mapping Rust variants to canonical protocol strings.

Produces: enum definition, ALL const, as_str(), Display, AsRef<str>, and FromStr. FromStr uses eq_ignore_ascii_case — appropriate for user-facing catalog types (header names, variable names) where input may come from config files. Wire protocol state types use hand-written strict FromStr instead.

Two error forms:

  • error_type: ParseMyEnumError, — the error newtype is defined separately by the caller as struct ParseMyEnumError(pub String).
  • error_type: ParseMyEnumError => "unknown my value", — the newtype, its Display ("unknown my value: {input}"), and std::error::Error are generated.

An optional leading tests_mod: my_enum_tests, generates a #[cfg(test)] module with round-trip, case-insensitivity, Display, and unknown-input tests over ALL (requires PartialEq on the error type).

§Example

define_header_enum! {
    tests_mod: my_enum_tests,
    error_type: ParseMyEnumError => "unknown my value",
    /// Doc comment for the enum.
    pub enum MyEnum {
        Foo => "foo-wire",
        Bar => "bar-wire",
    }
}