Skip to content

Commit

Permalink
Create m.lowercase renaming rule
Browse files Browse the repository at this point in the history
  • Loading branch information
zecakeh committed Apr 7, 2024
1 parent 2313217 commit 1e185dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 3 additions & 3 deletions crates/ruma-events/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ impl StreamMetadata {
/// The purpose of a VoIP stream.
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "m.snake_case")]
#[ruma_enum(rename_all = "m.lowercase")]
#[non_exhaustive]
pub enum StreamPurpose {
/// `m.usermedia`.
///
/// A stream that contains the webcam and/or microphone tracks.
Usermedia,
UserMedia,

/// `m.screenshare`.
///
/// A stream with the screen-sharing tracks.
Screenshare,
ScreenShare,

#[doc(hidden)]
_Custom(PrivOwnedStr),
Expand Down
34 changes: 32 additions & 2 deletions crates/ruma-macros/src/serde/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum RenameRule {
/// Rename direct children to "M_MATRIX_ERROR_CASE" style, as used for responses with error in
/// Matrix spec.
MatrixErrorCase,
/// Rename the direct children to "m.lowercase" style.
MatrixLowerCase,
/// Rename the direct children to "m.snake_case" style.
MatrixSnakeCase,
/// Rename the direct children to "m.dotted.case" style.
Expand Down Expand Up @@ -68,6 +70,7 @@ impl RenameRule {
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-"),
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_variant(variant),
MatrixLowerCase => String::from("m.") + &LowerCase.apply_to_variant(variant),
MatrixSnakeCase => String::from("m.") + &SnakeCase.apply_to_variant(variant),
MatrixDottedCase => {
String::from("m.") + &SnakeCase.apply_to_variant(variant).replace('_', ".")
Expand Down Expand Up @@ -106,6 +109,7 @@ impl RenameRule {
KebabCase => field.replace('_', "-"),
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_field(field),
MatrixLowerCase => String::from("m.") + field,
MatrixSnakeCase => String::from("m.") + field,
MatrixDottedCase => String::from("m.") + &field.replace('_', "."),
MatrixRuleSnakeCase => String::from(".m.rule.") + field,
Expand All @@ -129,6 +133,7 @@ impl FromStr for RenameRule {
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
"M_MATRIX_ERROR_CASE" => Ok(MatrixErrorCase),
"m.snake_case" => Ok(MatrixSnakeCase),
"m.lowercase" => Ok(MatrixLowerCase),
"m.dotted.case" => Ok(MatrixDottedCase),
".m.rule.snake_case" => Ok(MatrixRuleSnakeCase),
"m.role.snake_case" => Ok(MatrixRoleSnakeCase),
Expand All @@ -149,6 +154,7 @@ fn rename_variants() {
kebab,
screaming_kebab,
matrix_error,
m_lower,
m_snake,
m_dotted,
m_rule_snake,
Expand All @@ -166,6 +172,7 @@ fn rename_variants() {
"M_OUTCOME",
"m.outcome",
"m.outcome",
"m.outcome",
".m.rule.outcome",
"m.role.outcome",
),
Expand All @@ -179,12 +186,28 @@ fn rename_variants() {
"very-tasty",
"VERY-TASTY",
"M_VERY_TASTY",
"m.verytasty",
"m.very_tasty",
"m.very.tasty",
".m.rule.very_tasty",
"m.role.very_tasty",
),
("A", "a", "A", "a", "a", "A", "a", "A", "M_A", "m.a", "m.a", ".m.rule.a", "m.role.a"),
(
"A",
"a",
"A",
"a",
"a",
"A",
"a",
"A",
"M_A",
"m.a",
"m.a",
"m.a",
".m.rule.a",
"m.role.a",
),
(
"Z42",
"z42",
Expand All @@ -197,6 +220,7 @@ fn rename_variants() {
"M_Z42",
"m.z42",
"m.z42",
"m.z42",
".m.rule.z42",
"m.role.z42",
),
Expand All @@ -211,6 +235,7 @@ fn rename_variants() {
assert_eq!(KebabCase.apply_to_variant(original), kebab);
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
assert_eq!(MatrixErrorCase.apply_to_variant(original), matrix_error);
assert_eq!(MatrixLowerCase.apply_to_variant(original), m_lower);
assert_eq!(MatrixSnakeCase.apply_to_variant(original), m_snake);
assert_eq!(MatrixDottedCase.apply_to_variant(original), m_dotted);
assert_eq!(MatrixRuleSnakeCase.apply_to_variant(original), m_rule_snake);
Expand All @@ -229,6 +254,7 @@ fn rename_fields() {
kebab,
screaming_kebab,
matrix_error,
m_lower,
m_snake,
m_dotted,
m_rule_snake,
Expand All @@ -245,6 +271,7 @@ fn rename_fields() {
"M_OUTCOME",
"m.outcome",
"m.outcome",
"m.outcome",
".m.rule.outcome",
"m.role.outcome",
),
Expand All @@ -258,11 +285,12 @@ fn rename_fields() {
"VERY-TASTY",
"M_VERY_TASTY",
"m.very_tasty",
"m.very_tasty",
"m.very.tasty",
".m.rule.very_tasty",
"m.role.very_tasty",
),
("a", "A", "A", "a", "A", "a", "A", "M_A", "m.a", "m.a", ".m.rule.a", "m.role.a"),
("a", "A", "A", "a", "A", "a", "A", "M_A", "m.a", "m.a", "m.a", ".m.rule.a", "m.role.a"),
(
"z42",
"Z42",
Expand All @@ -274,6 +302,7 @@ fn rename_fields() {
"M_Z42",
"m.z42",
"m.z42",
"m.z42",
".m.rule.z42",
"m.role.z42",
),
Expand All @@ -287,6 +316,7 @@ fn rename_fields() {
assert_eq!(KebabCase.apply_to_field(original), kebab);
assert_eq!(ScreamingKebabCase.apply_to_field(original), screaming_kebab);
assert_eq!(MatrixErrorCase.apply_to_field(original), matrix_error);
assert_eq!(MatrixLowerCase.apply_to_field(original), m_lower);
assert_eq!(MatrixSnakeCase.apply_to_field(original), m_snake);
assert_eq!(MatrixDottedCase.apply_to_field(original), m_dotted);
assert_eq!(MatrixRuleSnakeCase.apply_to_field(original), m_rule_snake);
Expand Down

0 comments on commit 1e185dd

Please sign in to comment.