valust_utils/casing.rs
1//! Case-converting utilities.
2
3pub use convert_case as case;
4pub use convert_case::Case;
5use convert_case::Casing;
6
7/// Converts a string to a specified case.
8///
9/// ```rust
10/// # use valust_utils::casing::{Case, to_case};
11/// # use valust::{Validate, Raw, error::display::ErrorDisplay};
12/// # use valust_derive::Valust;
13/// #
14/// #[derive(Debug, Valust)]
15/// struct Casing(
16/// #[trans(func(String => to_case(Case::Pascal)))]
17/// String
18/// );
19/// let case = RawCasing("snake_case".to_string());
20/// let val = Casing::validate(case);
21/// assert_eq!("SnakeCase", val.unwrap().0.as_str());
22/// ```
23pub fn to_case<A: AsRef<str>>(case: Case) -> impl Fn(A) -> String {
24 move |s| s.as_ref().to_case(case)
25}
26
27/// Converts a string to `Upper`.
28///
29/// Alias for `to_case(Case::Upper)`.
30///
31/// Uppercase strings are delimited by spaces and all characters are uppercase.
32pub fn to_upper(s: impl AsRef<str>) -> String {
33 s.as_ref().to_case(Case::Upper)
34}
35
36/// Converts a string to `Lower`.
37///
38/// Alias for `to_case(Case::Lower)`.
39///
40/// Lowercase strings are delimited by spaces and all characters are lowercase.
41pub fn to_lower(s: impl AsRef<str>) -> String {
42 s.as_ref().to_case(Case::Lower)
43}
44
45/// Converts a string to `Title`.
46///
47/// Alias for `to_case(Case::Title)`.
48///
49/// Title case strings are delimited by spaces. Only the leading character of each word is uppercase.
50pub fn to_title(s: impl AsRef<str>) -> String {
51 s.as_ref().to_case(Case::Title)
52}
53
54/// Converts a string to `Sentence`.
55///
56/// Alias for `to_case(Case::Sentence)`.
57///
58/// Sentence case strings are delimited by spaces. Only the leading character of the first word is uppercase.
59pub fn to_sentence(s: impl AsRef<str>) -> String {
60 s.as_ref().to_case(Case::Sentence)
61}
62
63/// Converts a string to `Toggle`.
64///
65/// Alias for `to_case(Case::Toggle)`.
66///
67/// Toggle case strings are delimited by spaces. All characters are uppercase except for the leading character of each word, which is lowercase.
68pub fn to_toggle(s: impl AsRef<str>) -> String {
69 s.as_ref().to_case(Case::Toggle)
70}
71
72/// Converts a string to `Camel`.
73///
74/// Alias for `to_case(Case::Camel)`.
75///
76/// Camel case strings are lowercase, but for every word except the first the first letter is capitalized.
77pub fn to_camel(s: impl AsRef<str>) -> String {
78 s.as_ref().to_case(Case::Camel)
79}
80
81/// Converts a string to `Pascal`.
82///
83/// Alias for `to_case(Case::Pascal)`.
84///
85/// Pascal case strings are lowercase, but for every word the first letter is capitalized.
86pub fn to_pascal(s: impl AsRef<str>) -> String {
87 s.as_ref().to_case(Case::Pascal)
88}
89
90/// Converts a string to `Snake`.
91///
92/// Alias for `to_case(Case::Snake)`.
93///
94/// Snake case strings are delimited by underscores `_` and are all lowercase.
95pub fn to_snake(s: impl AsRef<str>) -> String {
96 s.as_ref().to_case(Case::Snake)
97}
98
99/// Converts a string to `Constant`.
100///
101/// Alias for `to_case(Case::Constant)`.
102///
103/// Constant case strings are delimited by underscores `_` and are all uppercase.
104pub fn to_constant(s: impl AsRef<str>) -> String {
105 s.as_ref().to_case(Case::Constant)
106}
107
108/// Converts a string to `Kebab`.
109///
110/// Alias for `to_case(Case::Kebab)`.
111///
112/// Kebab case strings are delimited by hyphens `-` and are all lowercase.
113pub fn to_kebab(s: impl AsRef<str>) -> String {
114 s.as_ref().to_case(Case::Kebab)
115}
116
117/// Converts a string to `Cobol`.
118///
119/// Alias for `to_case(Case::Cobol)`.
120///
121/// Cobol case strings are delimited by hyphens `-` and are all uppercase.
122pub fn to_cobol(s: impl AsRef<str>) -> String {
123 s.as_ref().to_case(Case::Cobol)
124}
125
126/// Converts a string to `Train`.
127///
128/// Alias for `to_case(Case::Train)`.
129///
130/// Train case strings are delimited by hyphens `-`. All characters are lowercase except for the leading character of each word.
131pub fn to_train(s: impl AsRef<str>) -> String {
132 s.as_ref().to_case(Case::Train)
133}
134
135/// Converts a string to `Flat`.
136///
137/// Alias for `to_case(Case::Flat)`.
138///
139/// Flat case strings are all lowercase, with no delimiter. Note that word boundaries are lost.
140pub fn to_flat(s: impl AsRef<str>) -> String {
141 s.as_ref().to_case(Case::Flat)
142}
143
144/// Converts a string to `UpperFlat`.
145///
146/// Alias for `to_case(Case::UpperFlat)`.
147///
148/// Upper flat case strings are all uppercase, with no delimiter. Note that word boundaries are lost.
149pub fn to_upper_flat(s: impl AsRef<str>) -> String {
150 s.as_ref().to_case(Case::UpperFlat)
151}
152
153/// Converts a string to `Alternating`.
154///
155/// Alias for `to_case(Case::Alternating)`.
156///
157/// Alternating case strings are delimited by spaces. Characters alternate between uppercase and lowercase.
158pub fn to_alternating(s: impl AsRef<str>) -> String {
159 s.as_ref().to_case(Case::Alternating)
160}