serde-macros: Add m.snake_case and m.dotted.case rename rules
This commit is contained in:
parent
f16d7b67c3
commit
672422e742
@ -36,6 +36,10 @@ pub enum RenameRule {
|
|||||||
/// Rename direct children to "M_MATRIX_ERROR_CASE" style, as used for responses with error in
|
/// Rename direct children to "M_MATRIX_ERROR_CASE" style, as used for responses with error in
|
||||||
/// Matrix spec.
|
/// Matrix spec.
|
||||||
MatrixErrorCase,
|
MatrixErrorCase,
|
||||||
|
/// Rename the direct children to "m.snake_case" style.
|
||||||
|
MatrixSnakeCase,
|
||||||
|
/// Rename the direct children to "m.dotted.case" style.
|
||||||
|
MatrixDottedCase,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenameRule {
|
impl RenameRule {
|
||||||
@ -60,6 +64,10 @@ impl RenameRule {
|
|||||||
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-"),
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||||
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_variant(variant),
|
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_variant(variant),
|
||||||
|
MatrixSnakeCase => String::from("m.") + &SnakeCase.apply_to_variant(variant),
|
||||||
|
MatrixDottedCase => {
|
||||||
|
String::from("m.") + &SnakeCase.apply_to_variant(variant).replace('_', ".")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,6 +100,8 @@ impl RenameRule {
|
|||||||
KebabCase => field.replace('_', "-"),
|
KebabCase => field.replace('_', "-"),
|
||||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
||||||
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_field(field),
|
MatrixErrorCase => String::from("M_") + &ScreamingSnakeCase.apply_to_field(field),
|
||||||
|
MatrixSnakeCase => String::from("m.") + field,
|
||||||
|
MatrixDottedCase => String::from("m.") + &field.replace('_', "."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,6 +120,8 @@ impl FromStr for RenameRule {
|
|||||||
"kebab-case" => Ok(KebabCase),
|
"kebab-case" => Ok(KebabCase),
|
||||||
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
|
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
|
||||||
"M_MATRIX_ERROR_CASE" => Ok(MatrixErrorCase),
|
"M_MATRIX_ERROR_CASE" => Ok(MatrixErrorCase),
|
||||||
|
"m.snake_case" => Ok(MatrixSnakeCase),
|
||||||
|
"m.dotted.case" => Ok(MatrixDottedCase),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,34 +129,48 @@ impl FromStr for RenameRule {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_variants() {
|
fn rename_variants() {
|
||||||
for &(original, lower, upper, camel, snake, screaming, kebab, screaming_kebab, matrix_error) in
|
for &(
|
||||||
&[
|
original,
|
||||||
(
|
lower,
|
||||||
"Outcome",
|
upper,
|
||||||
"outcome",
|
camel,
|
||||||
"OUTCOME",
|
snake,
|
||||||
"outcome",
|
screaming,
|
||||||
"outcome",
|
kebab,
|
||||||
"OUTCOME",
|
screaming_kebab,
|
||||||
"outcome",
|
matrix_error,
|
||||||
"OUTCOME",
|
m_snake,
|
||||||
"M_OUTCOME",
|
m_dotted,
|
||||||
),
|
) in &[
|
||||||
(
|
(
|
||||||
"VeryTasty",
|
"Outcome",
|
||||||
"verytasty",
|
"outcome",
|
||||||
"VERYTASTY",
|
"OUTCOME",
|
||||||
"veryTasty",
|
"outcome",
|
||||||
"very_tasty",
|
"outcome",
|
||||||
"VERY_TASTY",
|
"OUTCOME",
|
||||||
"very-tasty",
|
"outcome",
|
||||||
"VERY-TASTY",
|
"OUTCOME",
|
||||||
"M_VERY_TASTY",
|
"M_OUTCOME",
|
||||||
),
|
"m.outcome",
|
||||||
("A", "a", "A", "a", "a", "A", "a", "A", "M_A"),
|
"m.outcome",
|
||||||
("Z42", "z42", "Z42", "z42", "z42", "Z42", "z42", "Z42", "M_Z42"),
|
),
|
||||||
]
|
(
|
||||||
{
|
"VeryTasty",
|
||||||
|
"verytasty",
|
||||||
|
"VERYTASTY",
|
||||||
|
"veryTasty",
|
||||||
|
"very_tasty",
|
||||||
|
"VERY_TASTY",
|
||||||
|
"very-tasty",
|
||||||
|
"VERY-TASTY",
|
||||||
|
"M_VERY_TASTY",
|
||||||
|
"m.very_tasty",
|
||||||
|
"m.very.tasty",
|
||||||
|
),
|
||||||
|
("A", "a", "A", "a", "a", "A", "a", "A", "M_A", "a"),
|
||||||
|
("Z42", "z42", "Z42", "z42", "z42", "Z42", "z42", "Z42", "M_Z42", "z42"),
|
||||||
|
] {
|
||||||
assert_eq!(None.apply_to_variant(original), original);
|
assert_eq!(None.apply_to_variant(original), original);
|
||||||
assert_eq!(LowerCase.apply_to_variant(original), lower);
|
assert_eq!(LowerCase.apply_to_variant(original), lower);
|
||||||
assert_eq!(Uppercase.apply_to_variant(original), upper);
|
assert_eq!(Uppercase.apply_to_variant(original), upper);
|
||||||
@ -155,13 +181,37 @@ fn rename_variants() {
|
|||||||
assert_eq!(KebabCase.apply_to_variant(original), kebab);
|
assert_eq!(KebabCase.apply_to_variant(original), kebab);
|
||||||
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
|
assert_eq!(ScreamingKebabCase.apply_to_variant(original), screaming_kebab);
|
||||||
assert_eq!(MatrixErrorCase.apply_to_variant(original), matrix_error);
|
assert_eq!(MatrixErrorCase.apply_to_variant(original), matrix_error);
|
||||||
|
assert_eq!(MatrixSnakeCase.apply_to_variant(original), m_snake);
|
||||||
|
assert_eq!(MatrixDottedCase.apply_to_variant(original), m_dotted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_fields() {
|
fn rename_fields() {
|
||||||
for &(original, upper, pascal, camel, screaming, kebab, screaming_kebab, matrix_error) in &[
|
for &(
|
||||||
("outcome", "OUTCOME", "Outcome", "outcome", "OUTCOME", "outcome", "OUTCOME", "M_OUTCOME"),
|
original,
|
||||||
|
upper,
|
||||||
|
pascal,
|
||||||
|
camel,
|
||||||
|
screaming,
|
||||||
|
kebab,
|
||||||
|
screaming_kebab,
|
||||||
|
matrix_error,
|
||||||
|
m_snake,
|
||||||
|
m_dotted,
|
||||||
|
) in &[
|
||||||
|
(
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"Outcome",
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"outcome",
|
||||||
|
"OUTCOME",
|
||||||
|
"M_OUTCOME",
|
||||||
|
"m.outcome",
|
||||||
|
"m.outcome",
|
||||||
|
),
|
||||||
(
|
(
|
||||||
"very_tasty",
|
"very_tasty",
|
||||||
"VERY_TASTY",
|
"VERY_TASTY",
|
||||||
@ -171,9 +221,11 @@ fn rename_fields() {
|
|||||||
"very-tasty",
|
"very-tasty",
|
||||||
"VERY-TASTY",
|
"VERY-TASTY",
|
||||||
"M_VERY_TASTY",
|
"M_VERY_TASTY",
|
||||||
|
"m.very_tasty",
|
||||||
|
"m.very.tasty",
|
||||||
),
|
),
|
||||||
("a", "A", "A", "a", "A", "a", "A", "M_A"),
|
("a", "A", "A", "a", "A", "a", "A", "M_A", "a", "a"),
|
||||||
("z42", "Z42", "Z42", "z42", "Z42", "z42", "Z42", "M_Z42"),
|
("z42", "Z42", "Z42", "z42", "Z42", "z42", "Z42", "M_Z42", "z42", "z42"),
|
||||||
] {
|
] {
|
||||||
assert_eq!(None.apply_to_field(original), original);
|
assert_eq!(None.apply_to_field(original), original);
|
||||||
assert_eq!(Uppercase.apply_to_field(original), upper);
|
assert_eq!(Uppercase.apply_to_field(original), upper);
|
||||||
@ -184,5 +236,7 @@ fn rename_fields() {
|
|||||||
assert_eq!(KebabCase.apply_to_field(original), kebab);
|
assert_eq!(KebabCase.apply_to_field(original), kebab);
|
||||||
assert_eq!(ScreamingKebabCase.apply_to_field(original), screaming_kebab);
|
assert_eq!(ScreamingKebabCase.apply_to_field(original), screaming_kebab);
|
||||||
assert_eq!(MatrixErrorCase.apply_to_field(original), matrix_error);
|
assert_eq!(MatrixErrorCase.apply_to_field(original), matrix_error);
|
||||||
|
assert_eq!(MatrixSnakeCase.apply_to_field(original), m_snake);
|
||||||
|
assert_eq!(MatrixDottedCase.apply_to_field(original), m_dotted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user