Enum Case

#[non_exhaustive]
pub enum Case<'b>

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 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.

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 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

Custom { boundaries: &'b [Boundary], pattern: Pattern, delim: &'static str }

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, delim_boundary, Pattern};
let dot_case = Case::Custom {
    boundaries: &[delim_boundary!(".")],
    pattern: Pattern::Lowercase,
    delim: ".",
};

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",
);
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
  • Delimeter: 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
  • Delimeter: 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

impl Case<'_>

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 Space
Pascal, UpperCamel, Camel LowerUpper, LowerDigit, UpperDigit, DigitLower, DigitUpper, Acronym
Flat, UpperFlat No boundaries
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 Space " "
Flat, UpperFlat, Pascal, UpperCamel, Camel Empty string ""
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
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"],
);
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"],
);
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"),
);
fn all_cases() -> &'static [Case<'static>]

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

Trait Implementations

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

fn clone(&self) -> Self

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

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

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

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

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

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

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

fn eq(&self, other: &Self) -> bool

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> UnsafeUnpin for Case<'b>

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

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Case<'b> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Case<'b> where T: ?Sized,

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

impl<T> CloneToUninit for Case<'b> where T: Clone,

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

impl<T> From<T> for Case<'b>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Case<'b> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Case<'b> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for Case<'b> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Case<'b> where U: TryFrom<T>,

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