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
impl Boundary
Sourcepub const SPACE: Boundary
pub const SPACE: Boundary
Splits on space, consuming the character on segmentation.
assert_eq!(
vec![Boundary::SPACE],
Boundary::defaults_from(" ")
);
Sourcepub const HYPHEN: Boundary
pub const HYPHEN: Boundary
Splits on -
, consuming the character on segmentation.
assert_eq!(
vec![Boundary::HYPHEN],
Boundary::defaults_from("-")
);
Sourcepub const UNDERSCORE: Boundary
pub const UNDERSCORE: Boundary
Splits on _
, consuming the character on segmentation.
assert_eq!(
vec![Boundary::UNDERSCORE],
Boundary::defaults_from("_")
);
Sourcepub const LOWER_UPPER: Boundary
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")
);
Sourcepub const UPPER_LOWER: Boundary
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
);
Sourcepub const ACRONYM: Boundary
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")
);
Sourcepub const LOWER_DIGIT: Boundary
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")
);
Sourcepub const UPPER_DIGIT: Boundary
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")
);
Sourcepub const DIGIT_LOWER: Boundary
pub const DIGIT_LOWER: Boundary
Splits where digit is followed by a lowercase letter.
assert_eq!(
vec![Boundary::DIGIT_LOWER],
Boundary::defaults_from("1a")
);
Sourcepub const DIGIT_UPPER: Boundary
pub const DIGIT_UPPER: Boundary
Splits where digit is followed by an uppercase letter.
assert_eq!(
vec![Boundary::DIGIT_UPPER],
Boundary::defaults_from("1A")
);
Sourcepub const fn from_delim(delim: &'static str) -> Boundary
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")
)
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::SPACE,
Boundary::HYPHEN,
Boundary::UNDERSCORE,
Boundary::LOWER_UPPER,
Boundary::ACRONYM,
Boundary::LOWER_DIGIT,
Boundary::UPPER_DIGIT,
Boundary::DIGIT_LOWER,
Boundary::DIGIT_UPPER,
],
Boundary::defaults()
);
Sourcepub const fn digits() -> [Boundary; 4]
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()
);
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::LOWER_DIGIT,
Boundary::UPPER_DIGIT,
],
Boundary::letter_digit()
);
Sourcepub fn digit_letter() -> [Boundary; 2]
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()
);
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!(
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§
impl Copy for Boundary
impl Eq 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 UnwindSafe for Boundary
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 56 bytes