]> git.proxmox.com Git - rustc.git/blame - vendor/heck/src/mixed.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / heck / src / mixed.rs
CommitLineData
5869c6ff
XL
1use crate::{capitalize, lowercase, transform};
2
0731742a
XL
3/// This trait defines a mixed case conversion.
4///
5/// In mixedCase, word boundaries are indicated by capital letters, excepting
6/// the first word.
7///
8/// ## Example:
9///
10/// ```rust
5869c6ff 11/// use heck::MixedCase;
0731742a 12///
5869c6ff
XL
13/// let sentence = "It is we who built these palaces and cities.";
14/// assert_eq!(sentence.to_mixed_case(), "itIsWeWhoBuiltThesePalacesAndCities");
0731742a
XL
15/// ```
16pub trait MixedCase: ToOwned {
17 /// Convert this type to mixed case.
18 fn to_mixed_case(&self) -> Self::Owned;
19}
20
21impl MixedCase for str {
22 fn to_mixed_case(&self) -> String {
5869c6ff
XL
23 transform(self, |s, out| {
24 if out.is_empty() { lowercase(s, out); }
25 else { capitalize(s, out) }
0731742a
XL
26 }, |_| {})
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use super::MixedCase;
33
34 macro_rules! t {
35 ($t:ident : $s1:expr => $s2:expr) => {
36 #[test]
37 fn $t() {
38 assert_eq!($s1.to_mixed_case(), $s2)
39 }
40 }
41 }
42
43 t!(test1: "CamelCase" => "camelCase");
44 t!(test2: "This is Human case." => "thisIsHumanCase");
45 t!(test3: "MixedUP CamelCase, with some Spaces" => "mixedUpCamelCaseWithSomeSpaces");
46 t!(test4: "mixed_up_ snake_case, with some _spaces" => "mixedUpSnakeCaseWithSomeSpaces");
47 t!(test5: "kebab-case" => "kebabCase");
48 t!(test6: "SHOUTY_SNAKE_CASE" => "shoutySnakeCase");
49 t!(test7: "snake_case" => "snakeCase");
50 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "thisContainsAllKindsOfWordBoundaries");
51 t!(test9: "XΣXΣ baffle" => "xσxςBaffle");
52 t!(test10: "XMLHttpRequest" => "xmlHttpRequest");
53 // TODO unicode tests
54}