Skip to main content

wire_enum

Macro wire_enum 

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

Declares a closed set the client sends: one wire spelling per variant.

The enum derives Debug, Clone, Copy, PartialEq, Eq, Hash and serde’s Serialize and Deserialize, renamed to the wire spellings, and gets ALL, as_str, Display and a FromStr whose error is UnknownVariant. Attributes written above the enum, such as #[non_exhaustive], are kept, and so are those above each variant.

The expansion derives ::serde::Serialize and ::serde::Deserialize, so the calling crate depends on serde with its derive feature.

polyoxide_venue::wire_enum! {
    /// Side of a trade or position.
    pub enum Side { Long => "long", Short => "short" }
}

assert_eq!(Side::Long.as_str(), "long");
assert_eq!("short".parse::<Side>(), Ok(Side::Short));
assert_eq!(Side::ALL, &[Side::Long, Side::Short]);
let err = "flat".parse::<Side>().unwrap_err();
assert_eq!(err.to_string(), r#""flat" is not a valid Side"#);