Skip to main content

Converter

Struct Converter 

Source
pub struct Converter {
    pub boundaries: Vec<Boundary>,
    pub patterns: Vec<Pattern>,
    pub delimiter: String,
}
Expand description

The parameters for performing a case conversion.

A Converter stores three fields needed for case conversion.

  1. boundaries: how a string is split into words.
  2. patterns: how words are mutated, or how each character’s case will change.
  3. delimiter: 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!(
    s.from_case(Case::Kebab).to_case(Case::Snake),
    "dialoguebox_border_shadow",
);

// Convert using similar methods on Converter
let conv = Converter::new()
    .from_case(Case::Kebab)
    .to_case(Case::Snake);
assert_eq!(conv.convert(s), "dialoguebox_border_shadow");

// Convert by setting each field explicitly
let conv = Converter::new()
    .set_boundaries(&[Boundary::Hyphen])
    .set_patterns(&[Pattern::Lowercase])
    .set_delimiter("_");
assert_eq!(conv.convert(s), "dialoguebox_border_shadow");

Or you can use Converter when you are performing a transformation not provided as a variant of Case.

let dot_camel = Converter::new()
    .set_boundaries(&[Boundary::LowerUpper, Boundary::LowerDigit])
    .set_patterns(&[Pattern::Camel])
    .set_delimiter(".");
assert_eq!(dot_camel.convert("CollisionShape2D"), "collision.Shape.2d");

Fields§

§boundaries: Vec<Boundary>

How a string is split into words.

§patterns: Vec<Pattern>

How each word is mutated before joining.

§delimiter: String

The string used to join mutated words together.

Implementations§

Source§

impl Converter

Source

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 patterns, and an empty string as a delimiter.

let conv = Converter::new();
assert_eq!(conv.convert("Ice-cream TRUCK"), "IcecreamTRUCK")
Source

pub fn convert<T>(&self, s: T) -> String
where T: AsRef<str>,

Converts a string.

let conv = Converter::new()
    .to_case(Case::Camel);
assert_eq!(conv.convert("XML_HTTP_Request"), "xmlHttpRequest")
Source

pub fn to_case(self, case: Case<'_>) -> Converter

Set the pattern and delimiter to those associated with the given case.

let conv = Converter::new()
    .to_case(Case::Pascal);
assert_eq!(conv.convert("variable name"), "VariableName")
Source

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.

let conv = Converter::new()
    .from_case(Case::Snake)
    .to_case(Case::Title);
assert_eq!(conv.convert("dot_productValue"), "Dot Productvalue")
Source

pub fn set_boundaries(self, bs: &[Boundary]) -> Converter

Sets the boundaries to those provided.

let conv = Converter::new()
    .set_boundaries(&[Boundary::Underscore, Boundary::LowerUpper])
    .to_case(Case::Lower);
assert_eq!(conv.convert("firstName_lastName"), "first name last name");
Source

pub fn add_boundary(self, b: Boundary) -> Converter

Adds a boundary to the list of boundaries.

let conv = Converter::new()
    .from_case(Case::Title)
    .add_boundary(Boundary::Hyphen)
    .to_case(Case::Snake);
assert_eq!(conv.convert("My Biography - Video 1"), "my_biography___video_1")
Source

pub fn add_boundaries(self, bs: &[Boundary]) -> Converter

Adds a vector of boundaries to the list of boundaries.

let conv = Converter::new()
    .from_case(Case::Kebab)
    .to_case(Case::Title)
    .add_boundaries(&[Boundary::Underscore, Boundary::LowerUpper]);
assert_eq!(conv.convert("2020-10_firstDay"), "2020 10 First Day");
Source

pub fn remove_boundary(self, b: Boundary) -> Converter

Removes a boundary from the list of boundaries if it exists.

Note: Boundary::Custom variants are never considered equal due to function pointer comparison limitations, so they cannot be removed using this method. Recall that the default boundaries include no custom enumerations.

let conv = Converter::new()
    .remove_boundary(Boundary::Acronym)
    .to_case(Case::Kebab);
assert_eq!(conv.convert("HTTPRequest_parser"), "httprequest-parser");
Source

pub fn remove_boundaries(self, bs: &[Boundary]) -> Converter

Removes all the provided boundaries from the list of boundaries if it exists.

Note: Boundary::Custom variants are never considered equal due to function pointer comparison limitations, so they cannot be removed using this method. Recall that the default boundaries include no custom enumerations.

let conv = Converter::new()
    .remove_boundaries(&Boundary::digits())
    .to_case(Case::Snake);
assert_eq!(conv.convert("C04 S03 Path Finding.pdf"), "c04_s03_path_finding.pdf");
Source

pub fn set_pattern(self, p: Pattern) -> Converter

Sets a single pattern, replacing any existing patterns.

let conv = Converter::new()
    .set_delimiter("_")
    .set_pattern(Pattern::Sentence);
assert_eq!(conv.convert("BJARNE CASE"), "Bjarne_case");
Source

pub fn set_patterns(self, ps: &[Pattern]) -> Converter

Sets the patterns to those provided, replacing any existing patterns. An empty slice means no mutation (words pass through unchanged).

let conv = Converter::new()
    .set_delimiter("_")
    .set_patterns(&[Pattern::Sentence]);
assert_eq!(conv.convert("BJARNE CASE"), "Bjarne_case");
Source

pub fn add_pattern(self, p: Pattern) -> Converter

Adds a pattern to the end of the pattern list. Patterns are applied in order, so this pattern will be applied last.

let conv = Converter::new()
    .from_case(Case::Kebab)
    .add_pattern(Pattern::RemoveEmpty)
    .add_pattern(Pattern::Camel);
assert_eq!(conv.convert("--leading-delims"), "leadingDelims");
Source

pub fn add_patterns(self, ps: &[Pattern]) -> Converter

Adds multiple patterns to the end of the pattern list.

let conv = Converter::new()
    .add_patterns(&[Pattern::RemoveEmpty, Pattern::Lowercase]);
Source

pub fn remove_pattern(self, p: Pattern) -> Converter

Removes a pattern from the list if it exists.

Note: Pattern::Custom variants are never considered equal due to function pointer comparison limitations, so they cannot be removed using this method.

let conv = Converter::new()
    .set_boundaries(&[Boundary::Space])
    .to_case(Case::Snake)
    .remove_pattern(Pattern::Lowercase);
assert_eq!(conv.convert("HeLLo WoRLD"), "HeLLo_WoRLD");
Source

pub fn remove_patterns(self, ps: &[Pattern]) -> Converter

Removes all specified patterns from the list.

Note: Pattern::Custom variants are never considered equal due to function pointer comparison limitations, so they cannot be removed using this method.

let conv = Converter::new()
    .set_patterns(&[Pattern::RemoveEmpty, Pattern::Lowercase, Pattern::Capital])
    .remove_patterns(&[Pattern::Lowercase, Pattern::Capital]);
// Only RemoveEmpty remains
Source

pub fn set_delimiter<T>(self, d: T) -> Converter
where T: ToString,

Sets the delimiter.

let conv = Converter::new()
    .to_case(Case::Snake)
    .set_delimiter(".");
assert_eq!(conv.convert("LowerWithDots"), "lower.with.dots");

Trait Implementations§

Source§

impl Default for Converter

Source§

fn default() -> Converter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &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)

Converts &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> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.