#[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) -> &strfmt::Displayfmt::DebugSerializeOrdandPartialOrdEqandPartialEq
From<T: AsRef<str> + Into<Box<str>>>to convert a string to variants, and aDeserializeimplementation 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")]or#[ruma_enum(rename_all(prefix = "prefix", rule = "rule"))]- Override the string representation of all unit variants by using the givenprefixandrule. By default, the string representation uses the unit variant name, with the same case. This attribute allows to change the string representation by adding the given prefix or applying case transformation according to one of the following rules. When using the first syntax, the prefix is assumed to be empty. The case of the name of the rule always matches the transformation of the string.lowercase- Convert to lowercase.MyVariantbecomesmyvariant.UPPERCASE- Convert to uppercase.MyVariantbecomesMYVARIANT.camelCase- Convert the first character to lowercase.MyVariantbecomesmyVariant.snake_case- Add a_before all uppercase characters except at the start and convert all characters to lowercase.MyVariantbecomesmy_variant.SCREAMING_SNAKE_CASE- Add a_before all uppercase characters except at the start and convert all characters to uppercase.MyVariantbecomesMY_VARIANT.kebab-case- Add a-before all uppercase characters except at the start and convert all characters to lowercase.MyVariantbecomesmy-variant.
§Field attributes
These attributes are only valid on unit variants.
#[ruma_enum(rename = "value")]- Override the main string representation of the variant. Thevaluethat 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 isvalue. 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>);