A crate to easily create C-like enums resolving into values.
Example of valued enum use
This creates a public enum where every Number has an associated value of type NumberDescription.
use create_indexed_valued_enum;
use Indexed;
use Valued;
create_indexed_valued_enum!
Example of creating a valued enum
To implement it write: create_indexed_valued_enum!{ Your metadata //Like '#[derive(...)]', this is optional #[features(Feature1, Feature2, ...)] // this is optional Visibility enum Enum's name values as TypeOfValue; Variant1's metadata //this is optional Variant1, Value1, Variant2's metadata //this is optional Variant2, Value2, ... VariantN's metadata //this is optional VariantN, ValueN }
A simple example would look like:
use create_indexed_valued_enum;
create_indexed_valued_enum!
A more complex example would look like:
use create_indexed_valued_enum;
create_indexed_valued_enum!
On each of the fields you can indicate different parameters to change the implementation of the enum:
- EnumsName: Name the enum will have.
- TypeOfValue: type of the values the variant's resolve to.
- Pairs of Variant, Value: Name of the variant's to create along to the name they resolve to, the values must be const and have 'static lifetime.
- Features: List of specific implementations you want your enum to use, they are the following ones:
- DerefToValue: The enum implements Deref, making variants to resolve to their value directly, remember however these values won't mutate as they are constant references (&'static TypeOfValue), this is also the reason why these values require their life-time to be 'static.
- Clone: The enum implements clone calling [Indexed::from_discriminant], this way it's not required for the Derive Clone macro to expand to large enums.
- Delegators: Implements delegator functions over this enum that call on the methods from [Indexed] and [Valued], this way it is not required to import or use the indexed_valued_enums crate directly, however, it doesn't delegate the methods [Valued::value_to_variant] and [Valued::value_to_variant_opt] as they require the type of value to implement [PartialEq], however, you can delegate these too with the feature ValueToVariantDelegators.
- ValueToVariantDelegators: Implements delegator functions for [Valued::value_to_variant] and [Valued::value_to_variant_opt].
- Serialize: Implements serde's Serialize trait where it serializes to an usize that represents this enum's discriminant. Requires the "serde_enums" feature.
- Deserialize: Implements serde's Deserialize trait where it deserializes an enum variant's from it's enum's discriminant. Requires the "serde_enums" feature.
- NanoSerBin: Implements nanoserde's SerBin trait where it serializes to an usize that represents this enum's discriminant.
- NanoDeBin: Implements nanoserde's DeBin trait where it deserializes an enum variant's from it's enum's discriminant.
- NanoSerJson: Implements nanoserde's SerJson trait where it serializes to an usize that represents this enum's discriminant.
- NanoDeJson: Implements nanoserde's DeJson trait where it deserializes an enum variant's from it's enum's discriminant.
Note: You can write metadata (Such as #[derive(...)]) before each pair of Variant, Value, and also before the enum, but it is required that the ##[features(...)] is the last of the metadatas as this is not another metadata (henche the double hashtag to denote it)