convert_case

Enum Boundary

Source
pub enum Boundary {
    Hyphen,
    Underscore,
    Space,
    UpperLower,
    LowerUpper,
    DigitUpper,
    UpperDigit,
    DigitLower,
    LowerDigit,
    Acronym,
}
Expand description

A boundary defines how a string is split into words. Some boundaries, Hyphen, Underscore, and Space, consume the character they split on, whereas the other boundaries do not.

The struct offers methods that return Vecs containing useful groups of boundaries. It also contains the list_from method which will generate a list of boundaries based on a string slice.

Note that all boundaries are distinct and do not share functionality. That is, there is no such DigitLetter variant, because that would be equivalent to the current DigitUpper and DigitLower variants. For common functionality, consider using some provided functions that return a list of boundaries.

use convert_case::{Boundary, Case, Casing, Converter};

assert_eq!(
    "transformations_in_3d",
    "TransformationsIn3D"
        .from_case(Case::Camel)
        .without_boundaries(&Boundary::digit_letter())
        .to_case(Case::Snake)
);

let conv = Converter::new()
    .set_boundaries(&Boundary::list_from("aA "))
    .to_case(Case::Title);
assert_eq!("7empest By Tool", conv.convert("7empest byTool"));

Variants§

§

Hyphen

Splits on -, consuming the character on segmentation.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::Hyphen],
    Boundary::list_from("-")
);
§

Underscore

Splits on _, consuming the character on segmentation.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::Underscore],
    Boundary::list_from("_")
);
§

Space

Splits on space, consuming the character on segmentation.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::Space],
    Boundary::list_from(" ")
);
§

UpperLower

Splits where an uppercase letter is followed by a lowercase letter. This is seldom used, and is not included in the defaults.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::UpperLower],
    Boundary::list_from("Aa")
);
§

LowerUpper

Splits where a lowercase letter is followed by an uppercase letter.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::LowerUpper],
    Boundary::list_from("aA")
);
§

DigitUpper

Splits where digit is followed by an uppercase letter.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::DigitUpper],
    Boundary::list_from("1A")
);
§

UpperDigit

Splits where an uppercase letter is followed by a digit.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::UpperDigit],
    Boundary::list_from("A1")
);
§

DigitLower

Splits where digit is followed by a lowercase letter.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::DigitLower],
    Boundary::list_from("1a")
);
§

LowerDigit

Splits where a lowercase letter is followed by a digit.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::LowerDigit],
    Boundary::list_from("a1")
);
§

Acronym

Acronyms are identified by two uppercase letters followed by a lowercase letter. The word boundary is between the two uppercase letters. For example, “HTTPRequest” would have an acronym boundary identified at “PRe” and split into “HTTP” and “Request”.

use convert_case::Boundary;
assert_eq!(
    vec![Boundary::Acronym],
    Boundary::list_from("AAa")
);

Implementations§

Source§

impl Boundary

Source

pub fn list_from(s: &str) -> Vec<Self>

Returns a list of all boundaries that are identified within the given string. Could be a short of writing out all the boundaries in a list directly. This will not identify boundary UpperLower if it also used as part of Acronym.

If you want to be very explicit and not overlap boundaries, it is recommended to use a colon character.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![Hyphen, Space, LowerUpper, UpperDigit, DigitLower],
    Boundary::list_from("aA8a -")
);
assert_eq!(
    vec![Underscore, LowerUpper, DigitUpper, Acronym],
    Boundary::list_from("bD:0B:_:AAa")
);
Source

pub fn defaults() -> Vec<Self>

The default list of boundaries used when Casing::to_case is called directly and in a Converter generated from Converter::new(). This includes all the boundaries except the UpperLower boundary.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![
        Underscore, Hyphen, Space, LowerUpper, UpperDigit, 
        DigitUpper, DigitLower, LowerDigit, Acronym,
    ],
    Boundary::defaults()
);
Source

pub fn delims() -> Vec<Self>

Returns the boundaries that split around single characters: Hyphen, Underscore, and Space.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![Hyphen, Underscore, Space],
    Boundary::delims()
);
Source

pub fn digits() -> Vec<Self>

Returns the boundaries that involve digits: DigitUpper, DigitLower, UpperDigit, and LowerDigit.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![DigitUpper, UpperDigit, DigitLower, LowerDigit],
    Boundary::digits()
);
Source

pub fn letter_digit() -> Vec<Self>

Returns the boundaries that are letters followed by digits: UpperDigit and LowerDigit.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![UpperDigit, LowerDigit],
    Boundary::letter_digit()
);
Source

pub fn digit_letter() -> Vec<Self>

Returns the boundaries that are digits followed by letters: DigitUpper and DigitLower.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![DigitUpper, DigitLower],
    Boundary::digit_letter()
);
Source

pub fn all() -> Vec<Self>

Returns all boundaries. Note that this includes the UpperLower variant which might be unhelpful. Please look at Boundary::defaults.

use convert_case::Boundary;
use Boundary::*;
assert_eq!(
    vec![
        Hyphen, Underscore, Space, LowerUpper, UpperLower, DigitUpper,
        UpperDigit, DigitLower, LowerDigit, Acronym,
    ],
    Boundary::all()
);

Trait Implementations§

Source§

impl Clone for Boundary

Source§

fn clone(&self) -> Boundary

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 Boundary

Source§

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

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

impl PartialEq for Boundary

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Boundary

Source§

impl Eq for Boundary

Source§

impl StructuralPartialEq for Boundary

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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,

Source§

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

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.