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
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
impl Boundary
pub fn matches(self, s: &[&str]) -> bool
Sourcepub const fn defaults() -> [Boundary; 9]
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,
],
);Sourcepub const fn digits() -> [Boundary; 4]
pub const fn digits() -> [Boundary; 4]
Returns the boundaries that involve digits.
assert_eq!(
Boundary::digits(),
[
Boundary::LowerDigit,
Boundary::UpperDigit,
Boundary::DigitLower,
Boundary::DigitUpper,
],
);Sourcepub const fn letter_digit() -> [Boundary; 2]
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,
],
);Sourcepub const fn digit_letter() -> [Boundary; 2]
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
],
);Sourcepub fn defaults_from(pattern: &str) -> Vec<Boundary>
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,
],
);