pub enum Boundary {
Hyphen,
Underscore,
Space,
UpperLower,
LowerUpper,
DigitUpper,
UpperDigit,
DigitLower,
LowerDigit,
Acronym,
}
Expand description
A boundary defines how a string is split into words. Some boundaries, Hyphen
, Underscore
,
and Space
, consume the character they split on, whereas the other boundaries
do not.
The struct offers methods that return Vec
s containing useful groups of boundaries. It also
contains the list_from
method which will generate a list of boundaries
based on a string slice.
Note that all boundaries are distinct and do not share functionality. That is, there is no
such DigitLetter variant, because that would be equivalent to the current DigitUpper
and
DigitLower
variants. For common functionality, consider using
some provided functions that return a list of boundaries.
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::list_from("aA "))
.to_case(Case::Title);
assert_eq!("7empest By Tool", conv.convert("7empest byTool"));
Variants§
Hyphen
Splits on -
, consuming the character on segmentation.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::Hyphen],
Boundary::list_from("-")
);
Underscore
Splits on _
, consuming the character on segmentation.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::Underscore],
Boundary::list_from("_")
);
Space
Splits on space, consuming the character on segmentation.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::Space],
Boundary::list_from(" ")
);
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_eq!(
vec![Boundary::UpperLower],
Boundary::list_from("Aa")
);
LowerUpper
Splits where a lowercase letter is followed by an uppercase letter.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::LowerUpper],
Boundary::list_from("aA")
);
DigitUpper
Splits where digit is followed by an uppercase letter.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::DigitUpper],
Boundary::list_from("1A")
);
UpperDigit
Splits where an uppercase letter is followed by a digit.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::UpperDigit],
Boundary::list_from("A1")
);
DigitLower
Splits where digit is followed by a lowercase letter.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::DigitLower],
Boundary::list_from("1a")
);
LowerDigit
Splits where a lowercase letter is followed by a digit.
use convert_case::Boundary;
assert_eq!(
vec![Boundary::LowerDigit],
Boundary::list_from("a1")
);
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!(
vec![Boundary::Acronym],
Boundary::list_from("AAa")
);
Implementations§
Source§impl Boundary
impl Boundary
Sourcepub fn list_from(s: &str) -> Vec<Self>
pub fn list_from(s: &str) -> Vec<Self>
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;
use Boundary::*;
assert_eq!(
vec![Hyphen, Space, LowerUpper, UpperDigit, DigitLower],
Boundary::list_from("aA8a -")
);
assert_eq!(
vec![Underscore, LowerUpper, DigitUpper, Acronym],
Boundary::list_from("bD:0B:_:AAa")
);
Sourcepub fn defaults() -> Vec<Self>
pub fn defaults() -> Vec<Self>
The default list of boundaries used when Casing::to_case
is called directly
and in a Converter
generated from Converter::new()
. This includes
all the boundaries except the UpperLower
boundary.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![
Underscore, Hyphen, Space, LowerUpper, UpperDigit,
DigitUpper, DigitLower, LowerDigit, Acronym,
],
Boundary::defaults()
);
Sourcepub fn delims() -> Vec<Self>
pub fn delims() -> Vec<Self>
Returns the boundaries that split around single characters: Hyphen
,
Underscore
, and Space
.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![Hyphen, Underscore, Space],
Boundary::delims()
);
Sourcepub fn digits() -> Vec<Self>
pub fn digits() -> Vec<Self>
Returns the boundaries that involve digits: DigitUpper
, DigitLower
, UpperDigit
, and
LowerDigit
.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![DigitUpper, UpperDigit, DigitLower, LowerDigit],
Boundary::digits()
);
Sourcepub fn letter_digit() -> Vec<Self>
pub fn letter_digit() -> Vec<Self>
Returns the boundaries that are letters followed by digits: UpperDigit
and LowerDigit
.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![UpperDigit, LowerDigit],
Boundary::letter_digit()
);
Sourcepub fn digit_letter() -> Vec<Self>
pub fn digit_letter() -> Vec<Self>
Returns the boundaries that are digits followed by letters: DigitUpper
and
DigitLower
.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![DigitUpper, DigitLower],
Boundary::digit_letter()
);
Sourcepub fn all() -> Vec<Self>
pub fn all() -> Vec<Self>
Returns all boundaries. Note that this includes the UpperLower
variant which
might be unhelpful. Please look at Boundary::defaults
.
use convert_case::Boundary;
use Boundary::*;
assert_eq!(
vec![
Hyphen, Underscore, Space, LowerUpper, UpperLower, DigitUpper,
UpperDigit, DigitLower, LowerDigit, Acronym,
],
Boundary::all()
);