str_enum 0.3.1

Simple declarative macro for an enum with associated strings for each variant
Documentation

str_enum

A real simple declarative macro that I have ended up copy pasting into a few of my projects, spruced up with a couple features to make it more flexible. Unless you're a real stickler for compile times you're probably fine with strum over this for more features.

It implements basically everything str does, minus the derives which are opt-in. So you can probably use this anywhere you use &str.

use str_enum::str_enum;

str_enum! {
    #[phf] // optional: adds a constant PHF_MAP, which is used for try_from_str and FromStr
    #[error_type(MyError)] // optional: adds a FromStr implementation with the chosen error as the error type
    #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] // optional: adds the derives you specify to the enum. just not de/serialize, enable the serde feature for that
    #[repr(u8)] // optional: decide the repr
    pub(crate) enum MyEnum {
        Variant1 = 1 => "Variant1", // can optionally decide the discriminant for the variant
        Variant2 => "Variant2"("variant1"), // can add optional valid forms of input in brackets, if you want to cover lower case for example
    }
}

Features

feature description
serde Enables serde as a dependency and implements Serialize and Deserialize for the enum, respecting alternate valid forms.
strum Enables strum as a dependency and implements EnumCount, IntoDiscriminant (if you have a defined repr), IntoEnumIterator, VariantArray, VariantIterator, VariantNames and VariantMetadata. Note that the IntoDiscriminant implementation requires your enum to opt into implementing Copy
phf Enables phf as a dependency. When the #[phf] attribute is added to a str_enum it adds a constant PHF_MAP and replaces the Self::try_from_str and thus the FromStr implementation to leverage it.