#[derive(AsRefStr)]
{
// Attributes available to this derive:
#[ruma_enum]
}
Expand description
Derive the AsRef<str> trait for an enum.
The enum can contain unit variants, or tuple or struct variants containing a single field
which is a newtype struct around a type implementing Deref with a Target of str.
§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.
§Example
#[derive(AsRefStr)]
#[ruma_enum(rename_all = "lowercase")]
pub enum MyEnum {
Unit,
#[ruma_enum(rename = "unstable_other_unit")]
OtherUnit,
Struct {
inner: PrivOwnedStr,
},
Tuple(PrivOwnedStr),
}
pub struct PrivOwnedStr(Box<str>);