pub trait Casing<T: AsRef<str>> {
// Required methods
fn to_case(&self, case: Case) -> String;
fn from_case(&self, case: Case) -> StateConverter<'_, T>;
fn with_boundaries(&self, bs: &[Boundary]) -> StateConverter<'_, T>;
fn is_case(&self, case: Case) -> bool;
}
Expand description
Describes items that can be converted into a case. This trait is used
in conjunction with the StateConverter
struct which is returned from a couple
methods on Casing
.
Implemented for strings &str
, String
, and &String
.
Required Methods§
Sourcefn to_case(&self, case: Case) -> String
fn to_case(&self, case: Case) -> String
Convert the string into the given case. It will reference self
and create a new
String
with the same pattern and delimeter as case
. It will split on boundaries
defined at Boundary::defaults()
.
use convert_case::{Case, Casing};
assert_eq!(
"tetronimo-piece-border",
"Tetronimo piece border".to_case(Case::Kebab)
);
Sourcefn from_case(&self, case: Case) -> StateConverter<'_, T>
fn from_case(&self, case: Case) -> StateConverter<'_, T>
Start the case conversion by storing the boundaries associated with the given case.
use convert_case::{Case, Casing};
assert_eq!(
"2020-08-10_dannie_birthday",
"2020-08-10 Dannie Birthday"
.from_case(Case::Title)
.to_case(Case::Snake)
);
Sourcefn with_boundaries(&self, bs: &[Boundary]) -> StateConverter<'_, T>
fn with_boundaries(&self, bs: &[Boundary]) -> StateConverter<'_, T>
Creates a StateConverter
struct initialized with the boundaries
provided.
use convert_case::{Boundary, Case, Casing};
assert_eq!(
"e1_m1_hangar",
"E1M1 Hangar"
.with_boundaries(&[Boundary::DigitUpper, Boundary::Space])
.to_case(Case::Snake)
);
Sourcefn is_case(&self, case: Case) -> bool
fn is_case(&self, case: Case) -> bool
Determines if self
is of the given case. This is done simply by applying
the conversion and seeing if the result is the same.
use convert_case::{Case, Casing};
assert!( "kebab-case-string".is_case(Case::Kebab));
assert!( "Train-Case-String".is_case(Case::Train));
assert!(!"kebab-case-string".is_case(Case::Snake));
assert!(!"kebab-case-string".is_case(Case::Train));