Crate enum_stringify

Source
Expand description

§enum-stringify

A procedural macro that derives implementations for:

§Example

use enum_stringify::EnumStringify;
use std::str::FromStr;

#[derive(EnumStringify, Debug, PartialEq)]
enum Numbers {
   One,
   Two,
}

assert_eq!(Numbers::One.to_string(), "One");
assert_eq!(Numbers::Two.to_string(), "Two");


assert_eq!(Numbers::try_from("One").unwrap(), Numbers::One);
assert_eq!(Numbers::try_from("Two").unwrap(), Numbers::Two);

assert!(Numbers::try_from("Three").is_err());

§Custom Prefix and Suffix

You can add a prefix and/or suffix to the string representation:

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(prefix = "Pre", suffix = "Post")]
enum Numbers {
    One,
    Two,
}

assert_eq!(Numbers::One.to_string(), "PreOnePost");
assert_eq!(Numbers::try_from("PreOnePost").unwrap(), Numbers::One);

§Case Conversion

Convert enum variant names to different cases using the convert_case crate.

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(case = "flat")]
enum Numbers {
    One,
    Two,
}

assert_eq!(Numbers::One.to_string(), "one");
assert_eq!(Numbers::try_from("one").unwrap(), Numbers::One);

§Rename Variants

Customize the string representation of specific variants:

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
enum Istari {
    #[enum_stringify(rename = "Ólorin")]
    Gandalf,
    Saruman,
}

assert_eq!(Istari::Gandalf.to_string(), "Ólorin");
assert_eq!(Istari::try_from("Ólorin").unwrap(), Istari::Gandalf);

This takes precedence over the other attributes :

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper")]
enum Istari {
    #[enum_stringify(rename = "Ólorin")]
    Gandalf,
}

assert_eq!(Istari::Gandalf.to_string(), "Ólorin");
assert_eq!(Istari::try_from("Ólorin").unwrap(), Istari::Gandalf);

§Using All Options Together

You can combine all options: renaming, prefix, suffix, and case conversion.

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper_flat")]
enum Status {
    #[enum_stringify(rename = "okay")]
    Okk,
    Error3,
}

assert_eq!(Status::Okk.to_string(), "okay");
assert_eq!(Status::Error3.to_string(), "PREERROR3POST");

assert_eq!(Status::try_from("okay").unwrap(), Status::Okk);
assert_eq!(Status::try_from("PREERROR3POST").unwrap(), Status::Error3);

And using another case :

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper")]
enum Status {
    #[enum_stringify(rename = "okay")]
    Okk,
    Error3,
}

assert_eq!(Status::Okk.to_string(), "okay");
assert_eq!(Status::Error3.to_string(), "PRE ERROR 3 POST");

assert_eq!(Status::try_from("okay").unwrap(), Status::Okk);
assert_eq!(Status::try_from("PRE ERROR 3 POST").unwrap(), Status::Error3);

§Error Handling

When conversion from a string fails, the error type is String, containing a descriptive message:

use enum_stringify::EnumStringify;

#[derive(EnumStringify, Debug, PartialEq)]
#[enum_stringify(case = "lower")]
enum Numbers {
    One,
    Two,
}

let result = Numbers::try_from("Three");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Failed to parse string 'Three' for enum Numbers");

§Generated Implementations

The macro generates the following trait implementations:

enum Numbers { One, Two }

impl ::std::fmt::Display for Numbers {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::One => write!(f, "One"),
            Self::Two => write!(f, "Two"),
        }
    }
}

impl TryFrom<&str> for Numbers {
    type Error = String;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s {
            "One" => Ok(Self::One),
            "Two" => Ok(Self::Two),
            _ => Err(format!("Invalid value '{}'", s)),
        }
    }
}

impl TryFrom<String> for Numbers {
    type Error = String;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.as_str().try_into()
    }
}

impl ::std::str::FromStr for Numbers {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.try_into()
    }
}

Derive Macros§

EnumStringify