Enum Boundary

pub enum Boundary

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.

# use convert_case::{Boundary, Case, Casing};
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 { condition: fn(&[&str]) -> bool, start: usize, len: usize }
Hyphen

Splits on -, consuming the character on segmentation.

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("-"),
    vec![Boundary::Hyphen],
);
Underscore

Splits on _, consuming the character on segmentation.

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("_"),
    vec![Boundary::Underscore],
);
Space

Splits on space, consuming the character on segmentation.

# use convert_case::Boundary;
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.

# use convert_case::Boundary;
assert!(
    Boundary::defaults_from("Aa").len() == 0
);
LowerUpper

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

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("aA"),
    vec![Boundary::LowerUpper],
);
DigitUpper

Splits where digit is followed by an uppercase letter.

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("1A"),
    vec![Boundary::DigitUpper],
);
UpperDigit

Splits where an uppercase letter is followed by a digit.

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("A1"),
    vec![Boundary::UpperDigit],
);
DigitLower

Splits where digit is followed by a lowercase letter.

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults_from("1a"),
    vec![Boundary::DigitLower],
);
LowerDigit

Splits where a lowercase letter is followed by a digit.

# use convert_case::Boundary;
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".

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

Implementations

impl Boundary

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

The number of graphemes consumed when splitting at the boundary.

fn start(self) -> usize

The index of the character to split at.

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().

# use convert_case::Boundary;
assert_eq!(
    Boundary::defaults(),
    [
        Boundary::Underscore,
        Boundary::Hyphen,
        Boundary::Space,
        Boundary::LowerUpper,
        Boundary::LowerDigit,
        Boundary::UpperDigit,
        Boundary::DigitLower,
        Boundary::DigitUpper,
        Boundary::Acronym,
    ],
);
const fn digits() -> [Boundary; 4]

Returns the boundaries that involve digits.

# use convert_case::Boundary;
assert_eq!(
    Boundary::digits(),
    [
        Boundary::LowerDigit,
        Boundary::UpperDigit,
        Boundary::DigitLower,
        Boundary::DigitUpper,
    ],
);
const fn letter_digit() -> [Boundary; 2]

Returns the boundaries that are letters followed by digits.

# use convert_case::Boundary;
assert_eq!(
    Boundary::letter_digit(),
    [
        Boundary::LowerDigit,
        Boundary::UpperDigit,
    ],
);
const fn digit_letter() -> [Boundary; 2]

Returns the boundaries that are digits followed by letters.

# use convert_case::Boundary;
assert_eq!(
    Boundary::digit_letter(),
    [
        Boundary::DigitLower,
        Boundary::DigitUpper
    ],
);
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.

# use convert_case::Boundary;
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

impl Clone for Boundary

fn clone(&self) -> Boundary

impl Copy for Boundary

impl Debug for Boundary

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

impl Eq for Boundary

impl Hash for Boundary

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

impl PartialEq for Boundary

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

impl StructuralPartialEq for Boundary

Auto Trait Implementations

impl Freeze for Boundary

impl RefUnwindSafe for Boundary

impl Send for Boundary

impl Sync for Boundary

impl Unpin for Boundary

impl UnsafeUnpin for Boundary

impl UnwindSafe for Boundary

Blanket Implementations

impl<T> Any for Boundary where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Boundary where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Boundary where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Boundary where T: Clone,

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

impl<T> From<T> for Boundary

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Boundary where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Boundary where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

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

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for Boundary where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>