FromToStr

Derive Macro FromToStr 

Source
#[derive(FromToStr)]
{
    // Attributes available to this derive:
    #[fromtostr]
}
Expand description

Implements AsRef<str>, Into<String>1, Display (therefore ToString1), FromStr, TryFrom<String>1, Serialize2 and Deserialize2 for enum.

 use fieldless_enum_tools::FromToStr;

 #[derive(FromToStr, Debug, PartialEq, Eq, Clone, Copy)]
 #[fromtostr(format(style = "delimited", separator = "😎"))]
 enum CoolEnum {
     #[fromtostr(aliases("cool_variant_one"))]
     CoolVariantOne,
     #[fromtostr(rename("Very😎Cool😎Variant😎Two"))]
     CoolVariantTwo
 }


 let cool = CoolEnum::CoolVariantOne;

 assert_eq!(cool.as_ref(), "Cool😎Variant😎One");
 assert_eq!("cool_variant_one".parse(), Ok(cool));
 assert_eq!("Cool😎Variant😎One".parse(), Ok(cool));

 let cool = CoolEnum::CoolVariantTwo;
 assert_eq!("Very😎Cool😎Variant😎Two".parse(), Ok(cool));

 assert_eq!("uncool variant :(".parse::<CoolEnum>(), Err(()));
 // errors because we renamed it to Very😎Cool😎Variant😎Two
 assert_eq!("Cool😎Variant😎Two".parse::<CoolEnum>(), Err(()));

§Attributes

§Outer attributes

#[fromtostr(skip(...*))]

Skips implementing specified traits

Possible Values

ValueSkips
TryFromStringTryFrom<String>
FromStrFromStr
AsRefStrAsRef<str>
IntoStringInto<String>
DisplayDisplay
SerializeSerialize
DeserializeDeserialize

#[fromtostr(format(style = "...", separator = "..."?))]

Format variants using specified style

§Variant attributes

#[fromtostr(aliases("..."*))]

Specifies one (or more aliases) for this variant


#[fromtostr(rename("..."))] or #[fromtostr(rename(style = "...", separator = "..."?))]

Renames this variant with specified string or specified format style

§Possible Styles

Style NameDescriptionExampleNote
nonekeep it as isTwoWords
lowerto lowercasetwowords
UPPERto uppercaseTWOWORDS
snaketo snake casetwo_wordsalias to delimitedlower style with a _ separator
kebabto kebab casetwo-wordsalias to delimitedlower style with a - separator
SCREAMING_SNAKEto screaming snake caseTWO_WORDSalias to DELIMITEDUPPER with a _ separator
SCREAMING-KEBABto screaming kebab caseTWO-WORDSalias it to DELIMITEDUPPER style with a - separator
camelto camel casetwoWords
camel_Snaketo camel snake casetwo_Words
Pascalto pascal caseTwoWords
Pascal_Snaketo pascal snake caseTwo_Words
Trainto train caseTwo-Words
delimiteddelimits every word with separatorTwo{separator}Wordsneeds to specify a separator value
delimitedlowerdelimits every word with separator, then to lowercasetwo{separator}wordsneeds to specify a separator value
DELIMITEDUPPERdelimits every word with separator, then to uppercaseTWO{SEPARATOR}WORDSneeds to specify a separator value

  1. if crate feature std or alloc avaliable. â†© 1 2 3

  2. if crate feature serde avaliable. â†© 1 2