pub struct Converter {
pub boundaries: Vec<Boundary>,
pub pattern: Option<Pattern>,
pub delim: String,
}Expand description
The parameters for performing a case conversion.
A Converter stores three fields needed for case conversion.
boundaries: how a string is segmented into words.pattern: how words are mutated, or how each character’s case will change.delimor delimeter: how the mutated words are joined into the final string.
Then calling convert on a Converter will apply a case conversion
defined by those fields. The Converter struct is what is used underneath those functions
available in the Casing struct.
You can use Converter when you need more specificity on conversion
than those provided in Casing, or if it is simply more convenient or explicit.
use convert_case::{Boundary, Case, Casing, Converter, Pattern};
let s = "DialogueBox-border-shadow";
// Convert using Casing trait
assert_eq!(
"dialoguebox_border_shadow",
s.from_case(Case::Kebab).to_case(Case::Snake)
);
// Convert using similar functions on Converter
let conv = Converter::new()
.from_case(Case::Kebab)
.to_case(Case::Snake);
assert_eq!("dialoguebox_border_shadow", conv.convert(s));
// Convert by setting each field explicitly.
let conv = Converter::new()
.set_boundaries(&[Boundary::Hyphen])
.set_pattern(Pattern::Lowercase)
.set_delim("_");
assert_eq!("dialoguebox_border_shadow", conv.convert(s));Or you can use Converter when you are trying to make a unique case
not provided as a variant of Case.
use convert_case::{Boundary, Case, Casing, Converter, Pattern};
let dot_camel = Converter::new()
.set_boundaries(&[Boundary::LowerUpper, Boundary::LowerDigit])
.set_pattern(Pattern::Camel)
.set_delim(".");
assert_eq!("collision.Shape.2d", dot_camel.convert("CollisionShape2D"));Fields§
§boundaries: Vec<Boundary>How a string is segmented into words.
pattern: Option<Pattern>How each word is mutated before joining. In the case that there is no pattern, none of the words will be mutated before joining and will maintain whatever case they were in the original string.
delim: StringThe string used to join mutated words together.
Implementations§
Source§impl Converter
impl Converter
Sourcepub fn new() -> Converter
pub fn new() -> Converter
Creates a new Converter with default fields. This is the same as Default::default().
The Converter will use Boundary::defaults() for boundaries, no pattern, and an empty
string as a delimeter.
use convert_case::Converter;
let conv = Converter::new();
assert_eq!("DeathPerennialQUEST", conv.convert("Death-Perennial QUEST"))Sourcepub fn convert<T>(&self, s: T) -> String
pub fn convert<T>(&self, s: T) -> String
Converts a string.
use convert_case::{Case, Converter};
let conv = Converter::new()
.to_case(Case::Camel);
assert_eq!("xmlHttpRequest", conv.convert("XML_HTTP_Request"))Sourcepub fn to_case(self, case: Case) -> Converter
pub fn to_case(self, case: Case) -> Converter
Set the pattern and delimiter to those associated with the given case.
use convert_case::{Case, Converter};
let conv = Converter::new()
.to_case(Case::Pascal);
assert_eq!("VariableName", conv.convert("variable name"))Sourcepub fn from_case(self, case: Case) -> Converter
pub fn from_case(self, case: Case) -> Converter
Sets the boundaries to those associated with the provided case. This is used
by the from_case function in the Casing trait.
use convert_case::{Case, Converter};
let conv = Converter::new()
.from_case(Case::Snake)
.to_case(Case::Title);
assert_eq!("Dot Productvalue", conv.convert("dot_productValue"))Sourcepub fn set_boundaries(self, bs: &[Boundary]) -> Converter
pub fn set_boundaries(self, bs: &[Boundary]) -> Converter
Sets the boundaries to those provided.
use convert_case::{Boundary, Case, Converter};
let conv = Converter::new()
.set_boundaries(&[Boundary::Underscore, Boundary::LowerUpper])
.to_case(Case::Lower);
assert_eq!("panic attack dream theater", conv.convert("panicAttack_dreamTheater"))Sourcepub fn add_boundary(self, b: Boundary) -> Converter
pub fn add_boundary(self, b: Boundary) -> Converter
Adds a boundary to the list of boundaries.
use convert_case::{Boundary, Case, Converter};
let conv = Converter::new()
.from_case(Case::Title)
.add_boundary(Boundary::Hyphen)
.to_case(Case::Snake);
assert_eq!("my_biography_video_1", conv.convert("My Biography - Video 1"))Sourcepub fn add_boundaries(self, bs: &[Boundary]) -> Converter
pub fn add_boundaries(self, bs: &[Boundary]) -> Converter
Adds a vector of boundaries to the list of boundaries.
use convert_case::{Boundary, Case, Converter};
let conv = Converter::new()
.from_case(Case::Kebab)
.to_case(Case::Title)
.add_boundaries(&[Boundary::Underscore, Boundary::LowerUpper]);
assert_eq!("2020 10 First Day", conv.convert("2020-10_firstDay"));Sourcepub fn remove_boundary(self, b: Boundary) -> Converter
pub fn remove_boundary(self, b: Boundary) -> Converter
Removes a boundary from the list of boundaries if it exists.
use convert_case::{Boundary, Case, Converter};
let conv = Converter::new()
.remove_boundary(Boundary::Acronym)
.to_case(Case::Kebab);
assert_eq!("httprequest-parser", conv.convert("HTTPRequest_parser"));Sourcepub fn remove_boundaries(self, bs: &[Boundary]) -> Converter
pub fn remove_boundaries(self, bs: &[Boundary]) -> Converter
Removes all the provided boundaries from the list of boundaries if it exists.
use convert_case::{Boundary, Case, Converter};
let conv = Converter::new()
.remove_boundaries(&Boundary::digits())
.to_case(Case::Snake);
assert_eq!("c04_s03_path_finding.pdf", conv.convert("C04 S03 Path Finding.pdf"));Sourcepub fn set_delim<T>(self, d: T) -> Converterwhere
T: ToString,
pub fn set_delim<T>(self, d: T) -> Converterwhere
T: ToString,
Sets the delimeter.
use convert_case::{Case, Converter};
let conv = Converter::new()
.to_case(Case::Snake)
.set_delim(".");
assert_eq!("lower.with.dots", conv.convert("LowerWithDots"));Sourcepub fn remove_delim(self) -> Converter
pub fn remove_delim(self) -> Converter
Sets the delimeter to an empty string.
use convert_case::{Case, Converter};
let conv = Converter::new()
.to_case(Case::Snake)
.remove_delim();
assert_eq!("nodelimshere", conv.convert("No Delims Here"));Sourcepub fn set_pattern(self, p: Pattern) -> Converter
pub fn set_pattern(self, p: Pattern) -> Converter
Sets the pattern.
use convert_case::{Case, Converter, Pattern};
let conv = Converter::new()
.set_delim("_")
.set_pattern(Pattern::Sentence);
assert_eq!("Bjarne_case", conv.convert("BJARNE CASE"));Sourcepub fn remove_pattern(self) -> Converter
pub fn remove_pattern(self) -> Converter
Sets the pattern field to None. Where there is no pattern, a character’s case is never
mutated and will be maintained at the end of conversion.
use convert_case::{Case, Converter};
let conv = Converter::new()
.from_case(Case::Title)
.to_case(Case::Snake)
.remove_pattern();
assert_eq!("KoRn_Alone_I_Break", conv.convert("KoRn Alone I Break"));Trait Implementations§
Auto Trait Implementations§
impl Freeze for Converter
impl RefUnwindSafe for Converter
impl Send for Converter
impl Sync for Converter
impl Unpin for Converter
impl UnwindSafe for Converter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more