Skip to main content

smart_format/case/
mod.rs

1mod capitalize;
2mod lowercase;
3mod uppercase;
4
5pub use self::capitalize::*;
6pub use self::lowercase::*;
7pub use self::uppercase::*;
8
9/// Extension trait for case transformations on `Display` values.
10///
11/// # Design note
12///
13/// Case transforms are locale-insensitive — they use Rust's `char::to_uppercase()` /
14/// `char::to_lowercase()`, which implement Unicode Default Case Conversion (not
15/// locale-specific rules like Turkish dotted-I). This is the right default for
16/// programmatic text (identifiers, HTML, protocols). For locale-aware case conversion,
17/// use a dedicated i18n library.
18pub trait SmartFormatCase {
19    fn capitalize(self) -> Capitalize<Self>
20    where
21        Self: Sized + core::fmt::Display;
22
23    fn lowercase(self) -> Lowercase<Self>
24    where
25        Self: Sized + core::fmt::Display;
26
27    fn uppercase(self) -> Uppercase<Self>
28    where
29        Self: Sized + core::fmt::Display;
30}
31
32impl<T: core::fmt::Display> SmartFormatCase for T {
33    fn capitalize(self) -> Capitalize<Self>
34    where
35        Self: Sized + core::fmt::Display,
36    {
37        Capitalize(self)
38    }
39
40    fn lowercase(self) -> Lowercase<Self>
41    where
42        Self: Sized + core::fmt::Display,
43    {
44        Lowercase(self)
45    }
46
47    fn uppercase(self) -> Uppercase<Self>
48    where
49        Self: Sized + core::fmt::Display,
50    {
51        Uppercase(self)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    pub static WORD_CASES: &[&str] = &["word", "Word", "WoRd", "wOrD", "WORD"];
60
61    #[test]
62    fn it_capitalizes() {
63        fn test_case(expected: &str, sample: &str) {
64            assert_eq!(expected, sample.capitalize().to_string());
65        }
66
67        for &word in WORD_CASES {
68            test_case("Word", word);
69        }
70    }
71
72    #[test]
73    fn it_lowers_case() {
74        fn test_case(expected: &str, sample: &str) {
75            assert_eq!(expected, sample.lowercase().to_string());
76        }
77
78        for &word in WORD_CASES {
79            test_case("word", word);
80        }
81    }
82
83    #[test]
84    fn it_uppers_case() {
85        fn test_case(expected: &str, sample: &str) {
86            assert_eq!(expected, sample.uppercase().to_string());
87        }
88
89        for &word in WORD_CASES {
90            test_case("WORD", word);
91        }
92    }
93}