Skip to main content

Case

Enum Case 

Source
#[non_exhaustive]
pub enum Case<'b> {
Show 18 variants Custom { boundaries: &'b [Boundary], pattern: Pattern, delimiter: &'static str, }, Snake, Constant, UpperSnake, Ada, Kebab, Cobol, UpperKebab, Train, Flat, UpperFlat, Pascal, UpperCamel, Camel, Lower, Upper, Title, Sentence,
}
Expand description

Defines the case of an identifier.

use convert_case::ccase;
assert_eq!(ccase!(title, "super_mario_64"), "Super Mario 64");

use convert_case::{Case, Casing};
assert_eq!("super_mario_64".to_case(Case::Title), "Super Mario 64");

A case is the pair of a pattern and a delimiter (a string). Given a list of words, a pattern describes how to mutate the words and a delimiter is how the mutated words are joined together.

There are additionally Case::Sentence.

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 (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future 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, separator, Pattern};
let dot_case = Case::Custom {
    boundaries: &[separator!(".")],
    pattern: Pattern::Lowercase,
    delimiter: ".",
};

assert_eq!(
    "myNewCase".to_case(dot_case),
    "my.new.case",
);
assert_eq!(
    "my.new.case".from_case(dot_case).to_case(Case::Title),
    "My New Case",
);

Fields

§boundaries: &'b [Boundary]
§pattern: Pattern
§delimiter: &'static str
§

Snake

Snake case strings are delimited by underscores _ and are all lowercase.

use convert_case::ccase;
assert_eq!(ccase!(snake, "My variable NAME"), "my_variable_name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Snake), "my_variable_name");
§

Constant

Constant case strings are delimited by underscores _ and are all uppercase.

use convert_case::ccase;
assert_eq!(ccase!(constant, "My variable NAME"), "MY_VARIABLE_NAME");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Constant), "MY_VARIABLE_NAME");
§

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.

use convert_case::ccase;
assert_eq!(ccase!(ada, "My variable NAME"), "My_Variable_Name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Ada), "My_Variable_Name");
§

Kebab

Kebab case strings are delimited by hyphens - and are all lowercase.

use convert_case::ccase;
assert_eq!(ccase!(kebab, "My variable NAME"), "my-variable-name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Kebab), "my-variable-name");
§

Cobol

Cobol case strings are delimited by hyphens - and are all uppercase.

use convert_case::ccase;
assert_eq!(ccase!(cobol, "My variable NAME"), "MY-VARIABLE-NAME");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Cobol), "MY-VARIABLE-NAME");
§

UpperKebab

Upper kebab case is an alternative name for Cobol case.

§

Train

Train case strings are delimited by hyphens -. The leading letter of each word is uppercase, while the rest is lowercase.

use convert_case::ccase;
assert_eq!(ccase!(train, "My variable NAME"), "My-Variable-Name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Train), "My-Variable-Name");
§

Flat

Flat case strings are all lowercase, with no delimiter. Note that word boundaries are lost.

  • Boundaries: No boundaries
  • Pattern: Lowercase
  • Delimiter: Empty string ""
use convert_case::ccase;
assert_eq!(ccase!(flat, "My variable NAME"), "myvariablename");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Flat), "myvariablename");
§

UpperFlat

Upper flat case strings are all uppercase, with no delimiter. Note that word boundaries are lost.

  • Boundaries: No boundaries
  • Pattern: Uppercase
  • Delimiter: Empty string ""
use convert_case::ccase;
assert_eq!(ccase!(upper_flat, "My variable NAME"), "MYVARIABLENAME");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::UpperFlat), "MYVARIABLENAME");
§

Pascal

Pascal case strings are lowercase, but for every word the first letter is capitalized.

use convert_case::ccase;
assert_eq!(ccase!(pascal, "My variable NAME"), "MyVariableName");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Pascal), "MyVariableName");
§

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.

use convert_case::ccase;
assert_eq!(ccase!(camel, "My variable NAME"), "myVariableName");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Camel), "myVariableName");
§

Lower

Lowercase strings are delimited by spaces and all characters are lowercase.

use convert_case::ccase;
assert_eq!(ccase!(lower, "My variable NAME"), "my variable name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Lower), "my variable name");
§

Upper

Uppercase strings are delimited by spaces and all characters are uppercase.

use convert_case::ccase;
assert_eq!(ccase!(upper, "My variable NAME"), "MY VARIABLE NAME");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Upper), "MY VARIABLE NAME");
§

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::ccase;
assert_eq!(ccase!(title, "My variable NAME"), "My Variable Name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Title), "My Variable Name");
§

Sentence

Sentence case strings are delimited by spaces. Only the leading character of the first word is uppercase.

use convert_case::ccase;
assert_eq!(ccase!(sentence, "My variable NAME"), "My variable name");

use convert_case::{Case, Casing};
assert_eq!("My variable NAME".to_case(Case::Sentence), "My variable name");

Implementations§

Source§

impl Case<'_>

Source

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.

CasesBoundaries
Snake, Constant, UpperSnake, AdaUnderscore
Kebab, Cobol, UpperKebab, TrainHyphen
Lower, Upper, TitleSpace
Pascal, UpperCamel, CamelLowerUpper, LowerDigit, UpperDigit, DigitLower, DigitUpper, Acronym
Flat, UpperFlatNo boundaries
Source

pub const fn delimiter(&self) -> &'static str

Returns the delimiter used in the corresponding case. The following table outlines which cases use which delimiter.

CasesDelimiter
Snake, Constant, UpperSnake, AdaUnderscore "_"
Kebab, Cobol, UpperKebab, TrainHyphen "-"
Upper, Lower, Title, SentenceSpace " "
Flat, UpperFlat, Pascal, UpperCamel, CamelEmpty string ""
Source

pub const fn pattern(&self) -> Pattern

Returns the pattern used in the corresponding case. The following table outlines which cases use which pattern.

CasesPattern
Constant, UpperSnake, Cobol, UpperKebab, UpperFlat, UpperUppercase
Snake, Kebab, Flat, LowerLowercase
Ada, Train, Pascal, UpperCamel, TitleCapital
CamelCamel
Source

pub fn split<T>(self, s: &T) -> Vec<&str>
where T: AsRef<str>,

Split an identifier into words based on the boundaries of this case.

use convert_case::Case;
assert_eq!(
    Case::Pascal.split(&"getTotalLength"),
    vec!["get", "Total", "Length"],
);
Source

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!(
    Case::Snake.mutate(&["get", "Total", "Length"]),
    vec!["get", "total", "length"],
);
Source

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!(
    Case::Snake.join(&[
        String::from("get"),
        String::from("total"),
        String::from("length")
    ]),
    String::from("get_total_length"),
);
Source

pub fn all_cases() -> &'static [Case<'static>]

Array of all non-custom case enum variants. Does not include aliases.

Trait Implementations§

Source§

impl<'b> Clone for Case<'b>

Source§

fn clone(&self) -> Case<'b>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'b> Debug for Case<'b>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'b> Hash for Case<'b>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'b> PartialEq for Case<'b>

Source§

fn eq(&self, other: &Case<'b>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'b> Copy for Case<'b>

Source§

impl<'b> Eq for Case<'b>

Source§

impl<'b> StructuralPartialEq for Case<'b>

Auto Trait Implementations§

§

impl<'b> Freeze for Case<'b>

§

impl<'b> RefUnwindSafe for Case<'b>

§

impl<'b> Send for Case<'b>

§

impl<'b> Sync for Case<'b>

§

impl<'b> Unpin for Case<'b>

§

impl<'b> UnwindSafe for Case<'b>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.