StringEnum

Derive Macro StringEnum 

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

Shorthand for the derives AsRefStr, FromString, DisplayAsRefStr, DebugAsRefStr, SerializeAsRefStr, DeserializeFromCowStr, EqAsRefStr and OrdAsRefStr.

The enum can contain any number of unit variants, and must contain a single tuple or struct variant containing a single field which is a newtype struct around a Box<str>. This tuple or struct variant will be used as a fallback to catch any string that doesn’t match any of the unit variants.

This will generate the following implementations:

  • AsRef<str> to convert variants to their string representation, and the following implementations based on it:
    • fn as_str(&self) -> &str
    • fmt::Display
    • fmt::Debug
    • Serialize
    • Ord and PartialOrd
    • Eq and PartialEq
  • From<T: AsRef<str> + Into<Box<str>>> to convert a string to variants, and a Deserialize implementation based on it. The string to convert from must match exactly the expected string representation of a unit variants to be converted to it. If there is a difference of case, it will match the fallback variant instead.

§Container Attributes

  • #[ruma_enum(rename_all = "rule")] - Override the string representation of all unit variants by using the given rule. By default, the string representation uses the unit variant name, with the same case. This attribute allows to vary the case of the string representation with the following rules. The name of the rule always matches the transformation of the string.

    • lowercase - Convert to lowercase. MyVariant becomes myvariant.
    • UPPERCASE - Convert to uppercase. MyVariant becomes MYVARIANT.
    • camelCase - Convert the first character to lowercase. MyVariant becomes myVariant.
    • snake_case - Add a _ before all uppercase characters except at the start and convert all characters to lowercase. MyVariant becomes my_variant.
    • SCREAMING_SNAKE_CASE - Add a _ before all uppercase characters except at the start and convert all characters to uppercase. MyVariant becomes MY_VARIANT.
    • kebab-case - Add a - before all uppercase characters except at the start and convert all characters to lowercase. MyVariant becomes my-variant.
    • SCREAMING-KEBAB-CASE - Add a - before all uppercase characters except at the start and convert all characters to uppercase. MyVariant becomes MY-VARIANT.
    • M_MATRIX_ERROR_CASE - The case usually used for error codes in the Matrix specification. This is the same as SCREAMING_SNAKE_CASE, prepended with M_. MyVariant becomes M_MY_VARIANT.
    • m.snake_case => The case usually used for namespaced fields in the Matrix specification. This is the same as snake_case, prepended with m.. MyVariant becomes m.my_variant.
    • m.lowercase => A variant of m.snake_case based on the lowercase rule. MyVariant becomes m.myvariant.
    • m.dotted.case => A variant of m.snake_case where the _ is replaced by a .. MyVariant becomes m.my.variant.
    • .m.rule.snake_case => A variant of m.snake_case where the prefix is .m.rule., usually used for push rules rule_ids in the Matrix Specification. MyVariant becomes .m.rule.my_variant.
    • m.role.snake_case => A variant of m.snake_case where the prefix is m.role., usually used for contact methods roles in the Matrix Specification. MyVariant becomes m.role.my_variant.

§Field attributes

These attributes are only valid on unit variants.

  • #[ruma_enum(rename = "value")] - Override the main string representation of the variant. The value that is provided is the string representation of the variant.
  • #[ruma_enum(alias = "value")] - Allow this variant to be converted from a string that is not its main string representation, which is value. This attribute can be used several times to match more strings.

§Example

#[derive(StringEnum)]
#[ruma_enum(rename_all = "lowercase")]
pub enum MyEnum {
    Unit,
    #[ruma_enum(rename = "stable_other_unit", alias = "unstable_other_unit")]
    OtherUnit,
    #[doc(hidden)]
    _Custom(PrivOwnedStr),
}

pub struct PrivOwnedStr(Box<str>);