pub enum Case<'a> {
}Expand description
Defines the case of an identifier.
use convert_case::{Case, Casing};
let super_mario_title: String = "super_mario_64".to_case(Case::Title);
assert_eq!("Super Mario 64", super_mario_title);A case is the pair of a pattern and a delimeter (a string). Given a list of words, a pattern describes how to mutate the words and a delimeter is how the mutated words are joined together. These inherantly are the properties of what makes a “multiword identifier case”, or simply “case”.
| pattern | underscore _ | hyphen - | empty string | space |
|---|---|---|---|---|
| lowercase | snake_case | kebab-case | flatcase | lower case |
| uppercase | CONSTANT_CASE | COBOL-CASE | UPPERFLATCASE | UPPER CASE |
| capital | Ada_Case | Train-Case | PascalCase | Title Case |
| camel | camelCase |
There are other less common cases, such as Case::Sentence, Case::Alternating, and Case::Toggle.
Then there are two random cases [Case::Random] and [Case::PseudoRandom] from the random feature.
This crate provides the ability to convert “from” a case. This introduces a different feature
of cases which are the word boundaries that segment the identifier into words. For example, a
snake case identifier my_var_name can be split on underscores _ to segment into words. A
camel case identifier myVarName is split where a lowercase letter is followed by an
uppercase letter. Each case is also associated with a list of boundaries that are used when
converting “from” a particular case.
Variants§
Custom
Custom cases can be delimited by any static string slice and mutate words using any pattern. Further, they can use any list of boundaries for splitting identifiers into words.
This flexibility can create cases not present as another variant of the Case enum. For instance, you could create a “dot case” like so.
use convert_case::{Case, Casing, Boundary, pattern};
let dot_case = Case::Custom {
boundaries: &[Boundary::from_delim(".")],
pattern: pattern::lowercase,
delim: ".",
};
assert_eq!(
"my.new.case",
"myNewCase".to_case(dot_case),
);
assert_eq!(
"My New Case",
"my.new.case".from_case(dot_case).to_case(Case::Title),
);Snake
Snake case strings are delimited by underscores _ and are all lowercase.
- Boundaries: Underscore
- Pattern: lowercase
- Delimeter: Underscore
"_"
use convert_case::{Case, Casing};
assert_eq!("my_variable_name", "My variable NAME".to_case(Case::Snake))Constant
Constant case strings are delimited by underscores _ and are all uppercase.
- Boundaries: Underscore
- Pattern: uppercase
- Delimeter: Underscore
"_"
use convert_case::{Case, Casing};
assert_eq!("MY_VARIABLE_NAME", "My variable NAME".to_case(Case::Constant))UpperSnake
Upper snake case is an alternative name for constant case.
Ada
Ada case strings are delimited by underscores _. The leading letter of
each word is uppercase, while the rest is lowercase.
- Boundaries: Underscore
- Pattern: capital
- Delimeter: Underscore
"_"
use convert_case::{Case, Casing};
assert_eq!("My_Variable_Name", "My variable NAME".to_case(Case::Ada))Kebab
Kebab case strings are delimited by hyphens - and are all lowercase.
use convert_case::{Case, Casing};
assert_eq!("my-variable-name", "My variable NAME".to_case(Case::Kebab))Cobol
Cobol case strings are delimited by hyphens - and are all uppercase.
use convert_case::{Case, Casing};
assert_eq!("MY-VARIABLE-NAME", "My variable NAME".to_case(Case::Cobol))UpperKebab
Upper kebab case is an alternative name for Cobol case.
Train
Train case strings are delimited by hyphens -. All characters are lowercase
except for the leading character of each word.
use convert_case::{Case, Casing};
assert_eq!("My-Variable-Name", "My variable NAME".to_case(Case::Train))Flat
Flat case strings are all lowercase, with no delimiter. Note that word boundaries are lost.
- Boundaries: No boundaries
- Pattern: lowercase
- Delimeter: Empty string
""
use convert_case::{Case, Casing};
assert_eq!("myvariablename", "My variable NAME".to_case(Case::Flat))UpperFlat
Upper flat case strings are all uppercase, with no delimiter. Note that word boundaries are lost.
- Boundaries: No boundaries
- Pattern: uppercase
- Delimeter: Empty string
""
use convert_case::{Case, Casing};
assert_eq!("MYVARIABLENAME", "My variable NAME".to_case(Case::UpperFlat))Pascal
Pascal case strings are lowercase, but for every word the first letter is capitalized.
- Boundaries: LowerUpper, DigitUpper, UpperDigit, DigitLower, LowerDigit, Acronym
- Pattern: capital
- Delimeter: Empty string
""
use convert_case::{Case, Casing};
assert_eq!("MyVariableName", "My variable NAME".to_case(Case::Pascal))UpperCamel
Upper camel case is an alternative name for Pascal case.
Camel
Camel case strings are lowercase, but for every word except the first the first letter is capitalized.
- Boundaries: LowerUpper, DigitUpper, UpperDigit, DigitLower, LowerDigit, Acronym
- Pattern: camel
- Delimeter: Empty string
""
use convert_case::{Case, Casing};
assert_eq!("myVariableName", "My variable NAME".to_case(Case::Camel))Lower
Lowercase strings are delimited by spaces and all characters are lowercase.
use convert_case::{Case, Casing};
assert_eq!("my variable name", "My variable NAME".to_case(Case::Lower))Upper
Lowercase strings are delimited by spaces and all characters are lowercase.
use convert_case::{Case, Casing};
assert_eq!("MY VARIABLE NAME", "My variable NAME".to_case(Case::Upper))Title
Title case strings are delimited by spaces. Only the leading character of each word is uppercase. No inferences are made about language, so words like “as”, “to”, and “for” will still be capitalized.
use convert_case::{Case, Casing};
assert_eq!("My Variable Name", "My variable NAME".to_case(Case::Title))Sentence
Sentence case strings are delimited by spaces. Only the leading character of the first word is uppercase.
use convert_case::{Case, Casing};
assert_eq!("My variable name", "My variable NAME".to_case(Case::Sentence))Alternating
Alternating case strings are delimited by spaces. Characters alternate between uppercase and lowercase.
- Boundaries: Space
- Pattern: alternating
- Delimeter: Space
" "
use convert_case::{Case, Casing};
assert_eq!("mY vArIaBlE nAmE", "My variable NAME".to_case(Case::Alternating));Toggle
Toggle case strings are delimited by spaces. All characters are uppercase except for the leading character of each word, which is lowercase.
use convert_case::{Case, Casing};
assert_eq!("mY vARIABLE nAME", "My variable NAME".to_case(Case::Toggle))Implementations§
Source§impl Case<'_>
impl Case<'_>
Sourcepub fn boundaries(&self) -> &[Boundary]
pub fn boundaries(&self) -> &[Boundary]
Returns the boundaries used in the corresponding case. That is, where can word boundaries be distinguished in a string of the given case. The table outlines which cases use which set of boundaries.
| Cases | Boundaries |
|---|---|
| Snake, Constant, UpperSnake, Ada | UNDERSCORE |
| Kebab, Cobol, UpperKebab, Train | HYPHEN |
| Lower, Upper, Title, Alternating, Toggle, Random, PseudoRandom | SPACE |
| Pascal, UpperCamel, Camel | LOWER_UPPER, LOWER_DIGIT, UPPER_DIGIT, DIGIT_LOWER, DIGIT_UPPER, ACRONYM |
| Flat, UpperFlat | No boundaries |
Sourcepub const fn delim(&self) -> &'static str
pub const fn delim(&self) -> &'static str
Returns the delimiter used in the corresponding case. The following table outlines which cases use which delimeter.
| Cases | Delimeter |
|---|---|
| Snake, Constant, UpperSnake, Ada | Underscore "_" |
| Kebab, Cobol, UpperKebab, Train | Hyphen "-" |
| Upper, Lower, Title, Sentence, Alternating, Toggle, Random, PseudoRandom | Space " " |
| Flat, UpperFlat, Pascal, UpperCamel, Camel | Empty string "" |
Sourcepub const fn pattern(&self) -> Pattern
pub const fn pattern(&self) -> Pattern
Returns the pattern used in the corresponding case. The following table outlines which cases use which pattern.
| Cases | Pattern |
|---|---|
| Constant, UpperSnake, Cobol, UpperKebab, UpperFlat, Upper | uppercase |
| Snake, Kebab, Flat, Lower | lowercase |
| Ada, Train, Pascal, UpperCamel, Title | capital |
| Camel | camel |
| Alternating | alternating |
| Random | random |
| PseudoRandom | pseudo_random |
Sourcepub fn split<T>(self, s: &T) -> Vec<&str>
pub fn split<T>(self, s: &T) -> Vec<&str>
Split an identifier into words based on the boundaries of this case.
use convert_case::Case;
assert_eq!(
vec!["get", "Total", "Length"],
Case::Pascal.split(&"getTotalLength"),
);Sourcepub fn mutate(self, words: &[&str]) -> Vec<String>
pub fn mutate(self, words: &[&str]) -> Vec<String>
Mutate a list of words based on the pattern of this case.
use convert_case::Case;
assert_eq!(
vec!["get", "total", "length"],
Case::Snake.mutate(&["get", "Total", "Length"]),
);Sourcepub fn join(self, words: &[String]) -> String
pub fn join(self, words: &[String]) -> String
Join a list of words into a single identifier using the delimiter of this case.
use convert_case::Case;
assert_eq!(
String::from("get_total_length"),
Case::Snake.join(&[
String::from("get"),
String::from("total"),
String::from("length")
]),
);Sourcepub fn all_cases() -> &'static [Case<'static>]
pub fn all_cases() -> &'static [Case<'static>]
Array of all non-custom case enum variants. Does not include aliases.
Sourcepub fn deterministic_cases() -> &'static [Case<'static>]
pub fn deterministic_cases() -> &'static [Case<'static>]
Array of all the cases that do not depend on randomness. This is all the cases not in the “random” feature. Does not include aliases.