Skip to content

SneedSeedFeed/str_enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

An overly comprehensive string enum declarative macro for Rust

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-Apache.txt
MIT
LICENSE-MIT.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages