Enum Case
#[non_exhaustive]
pub enum Case<'b>
Defines the case of an identifier.
use ccase;
assert_eq!;
use ;
assert_eq!;
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 ; let dot_case = Custom ; assert_eq!; assert_eq!;-
Snake Snake case strings are delimited by underscores
_and are all lowercase.- Boundaries : Underscore
- Pattern : Lowercase
- Delimeter : Underscore
"_"
use ccase; assert_eq!; use ; assert_eq!;-
Constant Constant case strings are delimited by underscores
_and are all uppercase.- Boundaries: Underscore
- Pattern: Uppercase
- Delimeter: Underscore
"_"
use ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
Kebab Kebab case strings are delimited by hyphens
-and are all lowercase.use ccase; assert_eq!; use ; assert_eq!;-
Cobol Cobol case strings are delimited by hyphens
-and are all uppercase.use ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
Lower Lowercase strings are delimited by spaces and all characters are lowercase.
use ccase; assert_eq!; use ; assert_eq!;-
Upper Uppercase strings are delimited by spaces and all characters are uppercase.
use ccase; assert_eq!; use ; assert_eq!;-
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 ccase; assert_eq!; use ; assert_eq!;-
Sentence Sentence case strings are delimited by spaces. Only the leading character of the first word is uppercase.
use ccase; assert_eq!; use ; assert_eq!;
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 strReturns 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) -> PatternReturns 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 Case; assert_eq!;fn mutate(self, words: &[&str]) -> Vec<String>Mutate a list of words based on the pattern of this case.
use Case; assert_eq!;fn join(self, words: &[String]) -> StringJoin a list of words into a single identifier using the delimiter of this case.
use Case; assert_eq!;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) -> TReturns the argument unchanged.
impl<T> ToOwned for Case<'b>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Case<'b>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses 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>