mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-05-06 07:57:56 +00:00
Support for SCREAMING-KEBAB-CASE
This commit is contained in:
@@ -27,6 +27,8 @@ pub enum RenameRule {
|
|||||||
ScreamingSnakeCase,
|
ScreamingSnakeCase,
|
||||||
/// Rename direct children to "kebab-case" style.
|
/// Rename direct children to "kebab-case" style.
|
||||||
KebabCase,
|
KebabCase,
|
||||||
|
/// Rename direct children to "SCREAMING-KEBAB-CASE" style.
|
||||||
|
ScreamingKebabCase
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenameRule {
|
impl RenameRule {
|
||||||
@@ -47,6 +49,7 @@ impl RenameRule {
|
|||||||
}
|
}
|
||||||
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
|
ScreamingSnakeCase => SnakeCase.apply_to_variant(variant).to_ascii_uppercase(),
|
||||||
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
KebabCase => SnakeCase.apply_to_variant(variant).replace('_', "-"),
|
||||||
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_variant(variant).replace('_', "-")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +77,7 @@ impl RenameRule {
|
|||||||
}
|
}
|
||||||
ScreamingSnakeCase => field.to_ascii_uppercase(),
|
ScreamingSnakeCase => field.to_ascii_uppercase(),
|
||||||
KebabCase => field.replace('_', "-"),
|
KebabCase => field.replace('_', "-"),
|
||||||
|
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,6 +93,7 @@ impl FromStr for RenameRule {
|
|||||||
"snake_case" => Ok(SnakeCase),
|
"snake_case" => Ok(SnakeCase),
|
||||||
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
|
"SCREAMING_SNAKE_CASE" => Ok(ScreamingSnakeCase),
|
||||||
"kebab-case" => Ok(KebabCase),
|
"kebab-case" => Ok(KebabCase),
|
||||||
|
"SCREAMING-KEBAB-CASE" => Ok(ScreamingKebabCase),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,12 +101,12 @@ impl FromStr for RenameRule {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_variants() {
|
fn rename_variants() {
|
||||||
for &(original, lower, camel, snake, screaming, kebab) in
|
for &(original, lower, camel, snake, screaming, kebab, screaming_kebab) in
|
||||||
&[
|
&[
|
||||||
("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome"),
|
("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
||||||
("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty"),
|
("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty", "VERY-TASTY"),
|
||||||
("A", "a", "a", "a", "A", "a"),
|
("A", "a", "a", "a", "A", "a", "A"),
|
||||||
("Z42", "z42", "z42", "z42", "Z42", "z42"),
|
("Z42", "z42", "z42", "z42", "Z42", "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);
|
||||||
@@ -110,17 +115,18 @@ fn rename_variants() {
|
|||||||
assert_eq!(SnakeCase.apply_to_variant(original), snake);
|
assert_eq!(SnakeCase.apply_to_variant(original), snake);
|
||||||
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
|
assert_eq!(ScreamingSnakeCase.apply_to_variant(original), screaming);
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rename_fields() {
|
fn rename_fields() {
|
||||||
for &(original, pascal, camel, screaming, kebab) in
|
for &(original, pascal, camel, screaming, kebab, screaming_kebab) in
|
||||||
&[
|
&[
|
||||||
("outcome", "Outcome", "outcome", "OUTCOME", "outcome"),
|
("outcome", "Outcome", "outcome", "OUTCOME", "outcome", "OUTCOME"),
|
||||||
("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty"),
|
("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty", "VERY-TASTY"),
|
||||||
("a", "A", "a", "A", "a"),
|
("a", "A", "a", "A", "a", "A"),
|
||||||
("z42", "Z42", "z42", "Z42", "z42"),
|
("z42", "Z42", "z42", "Z42", "z42", "Z42"),
|
||||||
] {
|
] {
|
||||||
assert_eq!(None.apply_to_field(original), original);
|
assert_eq!(None.apply_to_field(original), original);
|
||||||
assert_eq!(PascalCase.apply_to_field(original), pascal);
|
assert_eq!(PascalCase.apply_to_field(original), pascal);
|
||||||
@@ -128,5 +134,6 @@ fn rename_fields() {
|
|||||||
assert_eq!(SnakeCase.apply_to_field(original), original);
|
assert_eq!(SnakeCase.apply_to_field(original), original);
|
||||||
assert_eq!(ScreamingSnakeCase.apply_to_field(original), screaming);
|
assert_eq!(ScreamingSnakeCase.apply_to_field(original), screaming);
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1149,7 +1149,7 @@ fn test_rename_all() {
|
|||||||
SerializeMap {
|
SerializeMap {
|
||||||
serialize: bool,
|
serialize: bool,
|
||||||
serialize_seq: bool,
|
serialize_seq: bool,
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
@@ -1159,6 +1159,13 @@ fn test_rename_all() {
|
|||||||
serialize_seq: bool,
|
serialize_seq: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
|
#[serde(rename_all = "SCREAMING-KEBAB-CASE")]
|
||||||
|
struct ScreamingKebab {
|
||||||
|
serialize: bool,
|
||||||
|
serialize_seq: bool,
|
||||||
|
}
|
||||||
|
|
||||||
assert_tokens(
|
assert_tokens(
|
||||||
&E::Serialize {
|
&E::Serialize {
|
||||||
serialize: true,
|
serialize: true,
|
||||||
@@ -1218,4 +1225,19 @@ fn test_rename_all() {
|
|||||||
Token::StructEnd,
|
Token::StructEnd,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_tokens(
|
||||||
|
&ScreamingKebab {
|
||||||
|
serialize: true,
|
||||||
|
serialize_seq: true,
|
||||||
|
},
|
||||||
|
&[
|
||||||
|
Token::Struct { name: "ScreamingKebab", len: 2 },
|
||||||
|
Token::Str("SERIALIZE"),
|
||||||
|
Token::Bool(true),
|
||||||
|
Token::Str("SERIALIZE-SEQ"),
|
||||||
|
Token::Bool(true),
|
||||||
|
Token::StructEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user