#[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")]- Override the string representation of all unit variants by using the givenrule. 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.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.SCREAMING-KEBAB-CASE- Add a-before all uppercase characters except at the start and convert all characters to uppercase.MyVariantbecomesMY-VARIANT.M_MATRIX_ERROR_CASE- The case usually used for error codes in the Matrix specification. This is the same asSCREAMING_SNAKE_CASE, prepended withM_.MyVariantbecomesM_MY_VARIANT.m.snake_case=> The case usually used for namespaced fields in the Matrix specification. This is the same assnake_case, prepended withm..MyVariantbecomesm.my_variant.m.lowercase=> A variant ofm.snake_casebased on thelowercaserule.MyVariantbecomesm.myvariant.m.dotted.case=> A variant ofm.snake_casewhere the_is replaced by a..MyVariantbecomesm.my.variant..m.rule.snake_case=> A variant ofm.snake_casewhere the prefix is.m.rule., usually used for push rulesrule_ids in the Matrix Specification.MyVariantbecomes.m.rule.my_variant.m.role.snake_case=> A variant ofm.snake_casewhere the prefix ism.role., usually used for contact methods roles in the Matrix Specification.MyVariantbecomesm.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. 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>);