Enum serde_rename_rule::RenameRule
source · pub enum RenameRule {
LowerCase,
UpperCase,
PascalCase,
CamelCase,
SnakeCase,
ScreamingSnakeCase,
KebabCase,
ScreamingKebabCase,
}
Variants§
LowerCase
Rename direct children to “lowercase” style.
UpperCase
Rename direct children to “UPPERCASE” style.
PascalCase
Rename direct children to “PascalCase” style, as typically used for enum variants.
CamelCase
Rename direct children to “camelCase” style.
SnakeCase
Rename direct children to “snake_case” style, as commonly used for fields.
ScreamingSnakeCase
Rename direct children to “SCREAMING_SNAKE_CASE” style, as commonly used for constants.
KebabCase
Rename direct children to “kebab-case” style.
ScreamingKebabCase
Rename direct children to “SCREAMING-KEBAB-CASE” style.
Implementations§
source§impl RenameRule
impl RenameRule
sourcepub fn from_rename_all_str(s: &str) -> Result<Self, ParseError>
pub fn from_rename_all_str(s: &str) -> Result<Self, ParseError>
sourcepub fn to_rename_all_str(&self) -> &'static str
pub fn to_rename_all_str(&self) -> &'static str
sourcepub fn apply_to_variant(&self, variant: &str) -> String
pub fn apply_to_variant(&self, variant: &str) -> String
Apply a renaming rule to an enum variant, returning the version expected in the source.
Examples found in repository?
src/lib.rs (line 80)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
pub fn apply_to_variant(&self, variant: &str) -> String {
match *self {
Self::PascalCase => variant.into(),
Self::LowerCase => variant.to_ascii_lowercase(),
Self::UpperCase => variant.to_ascii_uppercase(),
Self::CamelCase => variant[..1].to_ascii_lowercase() + &variant[1..],
Self::SnakeCase => {
let mut snake = String::new();
for (i, ch) in variant.char_indices() {
if i > 0 && ch.is_uppercase() {
snake.push('_');
}
snake.push(ch.to_ascii_lowercase());
}
snake
}
Self::ScreamingSnakeCase => Self::SnakeCase
.apply_to_variant(variant)
.to_ascii_uppercase(),
Self::KebabCase => Self::SnakeCase.apply_to_variant(variant).replace('_', "-"),
Self::ScreamingKebabCase => Self::ScreamingSnakeCase
.apply_to_variant(variant)
.replace('_', "-"),
}
}
sourcepub fn apply_to_field(&self, field: &str) -> String
pub fn apply_to_field(&self, field: &str) -> String
Apply a renaming rule to a struct field, returning the version expected in the source.
Examples found in repository?
src/lib.rs (line 110)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
pub fn apply_to_field(&self, field: &str) -> String {
match *self {
Self::LowerCase | Self::SnakeCase => field.into(),
Self::UpperCase => field.to_ascii_uppercase(),
Self::PascalCase => {
let mut pascal = String::new();
let mut capitalize = true;
for ch in field.chars() {
if ch == '_' {
capitalize = true;
} else if capitalize {
pascal.push(ch.to_ascii_uppercase());
capitalize = false;
} else {
pascal.push(ch);
}
}
pascal
}
Self::CamelCase => {
let pascal = Self::PascalCase.apply_to_field(field);
pascal[..1].to_ascii_lowercase() + &pascal[1..]
}
Self::ScreamingSnakeCase => field.to_ascii_uppercase(),
Self::KebabCase => field.replace('_', "-"),
Self::ScreamingKebabCase => Self::ScreamingSnakeCase
.apply_to_field(field)
.replace('_', "-"),
}
}
Trait Implementations§
source§impl Clone for RenameRule
impl Clone for RenameRule
source§fn clone(&self) -> RenameRule
fn clone(&self) -> RenameRule
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more