convert_case

Struct Boundary

Source
pub struct Boundary {
    pub name: &'static str,
    pub condition: fn(_: &[&str], _: Option<&'static str>) -> bool,
    pub arg: Option<&'static str>,
    pub start: usize,
    pub len: usize,
}
Expand description

How an identifier is split into words.

Some boundaries, HYPHEN, UNDERSCORE, and SPACE, consume the character they split on, whereas the other boundaries do not.

Boundary includes methods that return useful groups of boundaries. It also contains the defaults_from method which will generate a subset of default boundaries based on the boundaries present in a string.

You can also create custom delimiter boundaries using the from_delim method or directly instantiate Boundary for complex boundary conditions.

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::defaults_from("aA "))
    .to_case(Case::Title);
assert_eq!("7empest By Tool", conv.convert("7empest byTool"));

Fields§

§name: &'static str

A unique name used for comparison.

§condition: fn(_: &[&str], _: Option<&'static str>) -> bool

A function that determines if this boundary is present at the start of the string. Second argument is the arg field.

§arg: Option<&'static str>

An optional string passed to condition at runtime. Used internally for Boundary::from_delim method.

§start: usize

Where the beginning of the boundary is.

§len: usize

The length of the boundary. This is the number of graphemes that are removed when splitting.

Implementations§

Source§

impl Boundary

Source

pub const SPACE: Boundary = _

Splits on space, consuming the character on segmentation.

assert_eq!(
    vec![Boundary::SPACE],
    Boundary::defaults_from(" ")
);
Source

pub const HYPHEN: Boundary = _

Splits on -, consuming the character on segmentation.

assert_eq!(
    vec![Boundary::HYPHEN],
    Boundary::defaults_from("-")
);
Source

pub const UNDERSCORE: Boundary = _

Splits on _, consuming the character on segmentation.

assert_eq!(
    vec![Boundary::UNDERSCORE],
    Boundary::defaults_from("_")
);
Source

pub const LOWER_UPPER: Boundary = _

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

assert_eq!(
    vec![Boundary::LOWER_UPPER],
    Boundary::defaults_from("aA")
);
Source

pub const UPPER_LOWER: Boundary = _

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

assert!(
    Boundary::defaults_from("Aa").len() == 0
);
Source

pub const ACRONYM: Boundary = _

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”.

assert_eq!(
    vec![Boundary::ACRONYM],
    Boundary::defaults_from("AAa")
);
Source

pub const LOWER_DIGIT: Boundary = _

Splits where a lowercase letter is followed by a digit.

assert_eq!(
    vec![Boundary::LOWER_DIGIT],
    Boundary::defaults_from("a1")
);
Source

pub const UPPER_DIGIT: Boundary = _

Splits where an uppercase letter is followed by a digit.

assert_eq!(
    vec![Boundary::UPPER_DIGIT],
    Boundary::defaults_from("A1")
);
Source

pub const DIGIT_LOWER: Boundary = _

Splits where digit is followed by a lowercase letter.

assert_eq!(
    vec![Boundary::DIGIT_LOWER],
    Boundary::defaults_from("1a")
);
Source

pub const DIGIT_UPPER: Boundary = _

Splits where digit is followed by an uppercase letter.

assert_eq!(
    vec![Boundary::DIGIT_UPPER],
    Boundary::defaults_from("1A")
);
Source

pub const fn from_delim(delim: &'static str) -> Boundary

Create a new boundary based on a delimiter.

let conv = Converter::new()
    .set_boundaries(&[Boundary::from_delim("::")])
    .to_case(Case::Camel);
assert_eq!(
    "myVarName",
    conv.convert("my::var::name")
)
Source

pub const fn defaults() -> [Boundary; 9]

The default list of boundaries used when Casing::to_case is called directly and in a Converter generated from Converter::new().

assert_eq!(
    [
        Boundary::SPACE,
        Boundary::HYPHEN,
        Boundary::UNDERSCORE,
        Boundary::LOWER_UPPER,
        Boundary::ACRONYM,
        Boundary::LOWER_DIGIT,
        Boundary::UPPER_DIGIT,
        Boundary::DIGIT_LOWER,
        Boundary::DIGIT_UPPER,
    ],
    Boundary::defaults()
);
Source

pub const fn digits() -> [Boundary; 4]

Returns the boundaries that involve digits. LowerDigit.

assert_eq!(
    [
        Boundary::LOWER_DIGIT,
        Boundary::UPPER_DIGIT,
        Boundary::DIGIT_LOWER,
        Boundary::DIGIT_UPPER,
    ],
    Boundary::digits()
);
Source

pub const fn letter_digit() -> [Boundary; 2]

Returns the boundaries that are letters followed by digits.

assert_eq!(
    [
        Boundary::LOWER_DIGIT,
        Boundary::UPPER_DIGIT,
    ],
    Boundary::letter_digit()
);
Source

pub fn digit_letter() -> [Boundary; 2]

Returns the boundaries that are digits followed by letters.

assert_eq!(
    [
        Boundary::DIGIT_LOWER,
        Boundary::DIGIT_UPPER
    ],
    Boundary::digit_letter()
);
Source

pub fn defaults_from(pattern: &str) -> Vec<Boundary>

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.

assert_eq!(
    vec![
        Boundary::SPACE,
        Boundary::HYPHEN,
        Boundary::LOWER_UPPER,
        Boundary::UPPER_DIGIT,
        Boundary::DIGIT_LOWER,
    ],
    Boundary::defaults_from("aA8a -")
);
assert_eq!(
    vec![
        Boundary::UNDERSCORE,
        Boundary::LOWER_UPPER,
        Boundary::ACRONYM,
        Boundary::DIGIT_UPPER,
    ],
    Boundary::defaults_from("bD:0B:_:AAa")
);

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 Hash for Boundary

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Boundary

Source§

fn eq(&self, other: &Self) -> 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

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.