Enum config::Case

source ·
pub enum Case {
Show 17 variants Upper, Lower, Title, Toggle, Camel, Pascal, UpperCamel, Snake, UpperSnake, ScreamingSnake, Kebab, Cobol, UpperKebab, Train, Flat, UpperFlat, Alternating,
}
Expand description

Defines the type of casing a string can be.

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

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§

§

Upper

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

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

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

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

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

Camel

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

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

Pascal

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

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.

§

Snake

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

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

UpperSnake

Upper snake case strings are delimited by underscores _ and are all uppercase.

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

ScreamingSnake

Screaming snake case is an alternative name for upper snake case.

§

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: No delimeter
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: No delimeter
use convert_case::{Case, Casing};
assert_eq!("MYVARIABLENAME", "My variable NAME".to_case(Case::UpperFlat))
§

Alternating

Alternating case strings are delimited by spaces. Characters alternate between uppercase and lowercase.

use convert_case::{Case, Casing};
assert_eq!("mY vArIaBlE nAmE", "My variable NAME".to_case(Case::Alternating));

Implementations§

source§

impl Case

source

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

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

CasesDelimeter
Upper, Lower, Title, Toggle, Alternating, Random, PseudoRandomSpace
Snake, UpperSnake, ScreamingSnakeUnderscore _
Kebab, Cobol, UpperKebab, TrainHyphen -
UpperFlat, Flat, Camel, UpperCamel, PascalEmpty string, no delimeter
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
Upper, UpperSnake, ScreamingSnake, UpperFlat, Cobol, UpperKebabUppercase
Lower, Snake, Kebab, FlatLowercase
Title, Pascal, UpperCamel, TrainCapital
CamelCamel
AlternatingAlternating
RandomRandom
PseudoRandomPseudoRandom
source

pub fn boundaries(&self) -> Vec<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
Upper, Lower, Title, Toggle, Alternating, Random, PseudoRandomSpace
Snake, UpperSnake, ScreamingSnakeUnderscore _
Kebab, Cobol, UpperKebab, TrainHyphen -
Camel, UpperCamel, PascalLowerUpper, LowerDigit, UpperDigit, DigitLower, DigitUpper, Acronym
UpperFlat, FlatNo boundaries
source

pub fn all_cases() -> Vec<Case>

Returns a vector with all case enum variants in no particular order.

source

pub fn deterministic_cases() -> Vec<Case>

Returns a vector with all the cases that do not depend on randomness. This is all the cases not in the “random” feature.

Trait Implementations§

source§

impl Clone for Case

source§

fn clone(&self) -> Case

Returns a copy 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 Debug for Case

source§

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

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

impl Hash for Case

source§

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

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 PartialEq for Case

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Case

source§

impl Eq for Case

source§

impl StructuralPartialEq for Case

Auto Trait Implementations§

§

impl RefUnwindSafe for Case

§

impl Send for Case

§

impl Sync for Case

§

impl Unpin for Case

§

impl UnwindSafe for Case

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

§

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

§

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

§

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.