Boundary

Enum Boundary 

Source
pub enum Boundary {
    Custom {
        condition: fn(&[&str]) -> bool,
        start: usize,
        len: usize,
    },
    Hyphen,
    Underscore,
    Space,
    UpperLower,
    LowerUpper,
    DigitUpper,
    UpperDigit,
    DigitLower,
    LowerDigit,
    Acronym,
}
Expand description

Conditions for splitting an identifier 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 delim_boundary macro or directly instantiate Boundary for complex boundary conditions.

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

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

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

§Example

For more complex boundaries, such as splitting based on the first character being a certain symbol and the second is lowercase, you can instantiate a boundary directly.

let at_then_letter = Boundary::Custom {
    condition: |s| {
        s.get(0).map(|c| *c == "@") == Some(true)
            && s.get(1).map(|c| *c == c.to_lowercase()) == Some(true)
    },
    start: 1,
    len: 0,
};
assert_eq!(
    "name@domain"
        .set_boundaries(&[at_then_letter])
        .to_case(Case::Title),
    "Name@ Domain",
)

Variants§

§

Custom

Fields

§condition: fn(&[&str]) -> bool

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

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

§

Hyphen

Splits on -, consuming the character on segmentation.

assert_eq!(
    Boundary::defaults_from("-"),
    vec![Boundary::Hyphen],
);
§

Underscore

Splits on _, consuming the character on segmentation.

assert_eq!(
    Boundary::defaults_from("_"),
    vec![Boundary::Underscore],
);
§

Space

Splits on space, consuming the character on segmentation.

assert_eq!(
    Boundary::defaults_from(" "),
    vec![Boundary::Space],
);
§

UpperLower

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
);
§

LowerUpper

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

assert_eq!(
    Boundary::defaults_from("aA"),
    vec![Boundary::LowerUpper],
);
§

DigitUpper

Splits where digit is followed by an uppercase letter.

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

UpperDigit

Splits where an uppercase letter is followed by a digit.

assert_eq!(
    Boundary::defaults_from("A1"),
    vec![Boundary::UpperDigit],
);
§

DigitLower

Splits where digit is followed by a lowercase letter.

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

LowerDigit

Splits where a lowercase letter is followed by a digit.

assert_eq!(
    Boundary::defaults_from("a1"),
    vec![Boundary::LowerDigit],
);
§

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

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

Implementations§

Source§

impl Boundary

Source

pub fn matches(self, s: &[&str]) -> bool

Source

pub fn len(self) -> usize

The number of graphemes consumed when splitting at the boundary.

Source

pub fn start(self) -> usize

The index of the character to split at.

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::defaults(),
    [
        Boundary::Underscore,
        Boundary::Hyphen,
        Boundary::Space,
        Boundary::LowerUpper,
        Boundary::LowerDigit,
        Boundary::UpperDigit,
        Boundary::DigitLower,
        Boundary::DigitUpper,
        Boundary::Acronym,
    ],
);
Source

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

Returns the boundaries that involve digits.

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

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

Returns the boundaries that are letters followed by digits.

assert_eq!(
    Boundary::letter_digit(),
    [
        Boundary::LowerDigit,
        Boundary::UpperDigit,
    ],
);
Source

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

Returns the boundaries that are digits followed by letters.

assert_eq!(
    Boundary::digit_letter(),
    [
        Boundary::DigitLower,
        Boundary::DigitUpper
    ],
);
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!(
    Boundary::defaults_from("aA8a -"),
    vec![
        Boundary::Hyphen,
        Boundary::Space,
        Boundary::LowerUpper,
        Boundary::UpperDigit,
        Boundary::DigitLower,
    ],
);
assert_eq!(
    Boundary::defaults_from("bD:0B:_:AAa"),
    vec![
        Boundary::Underscore,
        Boundary::LowerUpper,
        Boundary::DigitUpper,
        Boundary::Acronym,
    ],
);

Trait Implementations§

Source§

impl Clone for Boundary

Source§

fn clone(&self) -> Boundary

Returns a duplicate 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: &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, dest: *mut u8)

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