Skip to main content

open_enum

Macro open_enum 

Source
macro_rules! open_enum {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $( $(#[$vmeta:meta])* $variant:ident => $wire:literal ),+ $(,)?
        }
    ) => { ... };
}
Expand description

Declares a set the venue reports and extends over time: a spelling this version does not know is kept verbatim in Other instead of failing the response.

The enum is #[non_exhaustive] and derives Debug, Clone, PartialEq, Eq and Hash. It gets ALL (the known variants), as_str, from_wire, Display, a FromStr that never fails, and serde’s Serialize and Deserialize as a string. Attributes written above the enum and each variant are kept.

The expansion implements serde’s Serialize and Deserialize by hand, naming ::serde, so the calling crate depends on serde, without needing its derive feature.

polyoxide_venue::open_enum! {
    /// Lifecycle state of a transaction.
    pub enum State {
        /// Accepted.
        New => "STATE_NEW",
        /// Confirmed on chain.
        Confirmed => "STATE_CONFIRMED",
    }
}

assert_eq!(State::from_wire("STATE_NEW"), State::New);
let queued = State::from_wire("STATE_QUEUED");
assert_eq!(queued, State::Other("STATE_QUEUED".into()));
assert_eq!(queued.as_str(), "STATE_QUEUED");
assert_eq!(State::ALL, &[State::New, State::Confirmed]);