use-case 0.1.0

Composable string casing primitives for RustUse.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

use std::error::Error;
use std::fmt;

/// Describes a detected or requested text case.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TextCase {
    /// Empty or whitespace-only input.
    Empty,
    /// Lowercase text without separators.
    Lower,
    /// Uppercase text without separators.
    Upper,
    /// Lowercase text separated by underscores.
    Snake,
    /// Lowercase text separated by hyphens.
    Kebab,
    /// Upper camel case such as `RustUse`.
    Pascal,
    /// Lower camel case such as `rustUse`.
    Camel,
    /// Title case separated by whitespace.
    Title,
    /// Uppercase text separated by underscores.
    Constant,
    /// Text that does not match a supported case shape.
    Mixed,
}

/// A reusable case-conversion descriptor.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CaseConversion {
    source: TextCase,
    target: TextCase,
}

impl CaseConversion {
    /// Creates a new conversion descriptor.
    pub const fn new(source: TextCase, target: TextCase) -> Self {
        Self { source, target }
    }

    /// Returns the declared source case.
    pub const fn source(self) -> TextCase {
        self.source
    }

    /// Returns the target case.
    pub const fn target(self) -> TextCase {
        self.target
    }

    /// Applies the conversion to the provided input.
    pub fn apply(self, input: &str) -> Result<String, CaseError> {
        convert_to_case(input, self.target)
    }
}

/// Errors returned by [`CaseConversion`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CaseError {
    /// The requested target case is descriptive rather than convertible.
    UnsupportedTarget(TextCase),
}

impl fmt::Display for CaseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedTarget(case) => {
                write!(formatter, "unsupported conversion target: {case:?}")
            },
        }
    }
}

impl Error for CaseError {}

/// Converts input into `snake_case`.
pub fn to_snake_case(input: &str) -> String {
    join_words(&normalized_words(input), "_")
}

/// Converts input into `kebab-case`.
pub fn to_kebab_case(input: &str) -> String {
    join_words(&normalized_words(input), "-")
}

/// Converts input into `PascalCase`.
pub fn to_pascal_case(input: &str) -> String {
    split_words(input)
        .into_iter()
        .map(|word| titlecase_word(&word))
        .collect()
}

/// Converts input into `camelCase`.
pub fn to_camel_case(input: &str) -> String {
    let words = normalized_words(input);
    let Some((first, rest)) = words.split_first() else {
        return String::new();
    };

    let mut output = first.clone();
    for word in rest {
        output.push_str(&titlecase_word(word));
    }
    output
}

/// Converts input into title case separated by spaces.
pub fn to_title_case(input: &str) -> String {
    split_words(input)
        .into_iter()
        .map(|word| titlecase_word(&word))
        .collect::<Vec<_>>()
        .join(" ")
}

/// Converts input into `CONSTANT_CASE`.
pub fn to_constant_case(input: &str) -> String {
    split_words(input)
        .into_iter()
        .map(|word| uppercase_word(&word))
        .collect::<Vec<_>>()
        .join("_")
}

/// Detects the most practical case shape for the input.
pub fn detect_case(input: &str) -> TextCase {
    let trimmed = input.trim();

    if trimmed.is_empty() {
        return TextCase::Empty;
    }

    if is_constant_case(trimmed) {
        return TextCase::Constant;
    }

    if is_snake_case(trimmed) {
        return TextCase::Snake;
    }

    if is_kebab_case(trimmed) {
        return TextCase::Kebab;
    }

    if is_title_case(trimmed) {
        return TextCase::Title;
    }

    if is_pascal_case(trimmed) {
        return TextCase::Pascal;
    }

    if is_camel_case(trimmed) {
        return TextCase::Camel;
    }

    if is_upper_case(trimmed) {
        return TextCase::Upper;
    }

    if is_lower_case(trimmed) {
        return TextCase::Lower;
    }

    TextCase::Mixed
}

fn convert_to_case(input: &str, target: TextCase) -> Result<String, CaseError> {
    let output = match target {
        TextCase::Empty => return Ok(String::new()),
        TextCase::Lower => lowercase_word(input.trim()),
        TextCase::Upper => uppercase_word(input.trim()),
        TextCase::Snake => to_snake_case(input),
        TextCase::Kebab => to_kebab_case(input),
        TextCase::Pascal => to_pascal_case(input),
        TextCase::Camel => to_camel_case(input),
        TextCase::Title => to_title_case(input),
        TextCase::Constant => to_constant_case(input),
        TextCase::Mixed => return Err(CaseError::UnsupportedTarget(TextCase::Mixed)),
    };

    Ok(output)
}

fn split_words(input: &str) -> Vec<String> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Vec::new();
    }

    let mut words = Vec::new();
    let mut chunk = String::new();

    for character in trimmed.chars() {
        if character.is_alphanumeric() {
            chunk.push(character);
        } else if !chunk.is_empty() {
            extend_chunk_words(&chunk, &mut words);
            chunk.clear();
        }
    }

    if !chunk.is_empty() {
        extend_chunk_words(&chunk, &mut words);
    }

    words
}

fn extend_chunk_words(chunk: &str, words: &mut Vec<String>) {
    let characters: Vec<char> = chunk.chars().collect();
    if characters.is_empty() {
        return;
    }

    let mut start = 0;
    for index in 1..characters.len() {
        let previous = characters[index - 1];
        let current = characters[index];
        let next = characters.get(index + 1).copied();

        if is_chunk_boundary(previous, current, next) {
            words.push(characters[start..index].iter().collect());
            start = index;
        }
    }

    words.push(characters[start..].iter().collect());
}

fn is_chunk_boundary(previous: char, current: char, next: Option<char>) -> bool {
    (previous.is_lowercase() && current.is_uppercase())
        || (previous.is_alphabetic() && current.is_numeric())
        || (previous.is_numeric() && current.is_alphabetic())
        || (previous.is_uppercase()
            && current.is_uppercase()
            && next.is_some_and(|candidate| candidate.is_lowercase()))
}

fn normalized_words(input: &str) -> Vec<String> {
    split_words(input)
        .into_iter()
        .map(|word| lowercase_word(&word))
        .collect()
}

fn join_words(words: &[String], separator: &str) -> String {
    words.join(separator)
}

fn lowercase_word(word: &str) -> String {
    word.chars().flat_map(char::to_lowercase).collect()
}

fn uppercase_word(word: &str) -> String {
    word.chars().flat_map(char::to_uppercase).collect()
}

fn titlecase_word(word: &str) -> String {
    let mut characters = word.chars();
    let Some(first) = characters.next() else {
        return String::new();
    };

    first
        .to_uppercase()
        .chain(characters.flat_map(char::to_lowercase))
        .collect()
}

fn is_snake_case(input: &str) -> bool {
    input.contains('_')
        && !input.starts_with('_')
        && !input.ends_with('_')
        && !input.contains("__")
        && input.chars().all(|character| {
            character.is_ascii_lowercase() || character.is_ascii_digit() || character == '_'
        })
}

fn is_constant_case(input: &str) -> bool {
    input.contains('_')
        && !input.starts_with('_')
        && !input.ends_with('_')
        && !input.contains("__")
        && input.chars().all(|character| {
            character.is_ascii_uppercase() || character.is_ascii_digit() || character == '_'
        })
}

fn is_kebab_case(input: &str) -> bool {
    input.contains('-')
        && !input.starts_with('-')
        && !input.ends_with('-')
        && !input.contains("--")
        && input.chars().all(|character| {
            character.is_ascii_lowercase() || character.is_ascii_digit() || character == '-'
        })
}

fn is_title_case(input: &str) -> bool {
    input.contains(char::is_whitespace)
        && input.split_whitespace().all(|word| {
            let mut characters = word.chars();
            let Some(first) = characters.next() else {
                return false;
            };

            first.is_uppercase()
                && characters
                    .all(|character| !character.is_alphabetic() || character.is_lowercase())
        })
}

fn is_pascal_case(input: &str) -> bool {
    !input.contains(|character: char| !character.is_alphanumeric())
        && input.chars().next().is_some_and(char::is_uppercase)
        && !is_upper_case(input)
}

fn is_camel_case(input: &str) -> bool {
    !input.contains(|character: char| !character.is_alphanumeric())
        && input.chars().next().is_some_and(char::is_lowercase)
        && input.chars().any(char::is_uppercase)
}

fn is_lower_case(input: &str) -> bool {
    !input.contains(|character: char| !character.is_alphanumeric())
        && input
            .chars()
            .all(|character| !character.is_alphabetic() || character.is_lowercase())
}

fn is_upper_case(input: &str) -> bool {
    !input.contains(|character: char| !character.is_alphanumeric())
        && input
            .chars()
            .all(|character| !character.is_alphabetic() || character.is_uppercase())
}

#[cfg(test)]
mod tests {
    use super::{
        CaseConversion, CaseError, TextCase, detect_case, to_camel_case, to_constant_case,
        to_kebab_case, to_pascal_case, to_snake_case, to_title_case,
    };

    #[test]
    fn handles_empty_and_whitespace_only_input() {
        assert_eq!(to_snake_case(""), "");
        assert_eq!(to_kebab_case("   \t"), "");
        assert_eq!(detect_case("   \n"), TextCase::Empty);
    }

    #[test]
    fn converts_ascii_and_mixed_case_inputs() {
        assert_eq!(to_snake_case("HTTPServerError"), "http_server_error");
        assert_eq!(to_kebab_case("userProfile"), "user-profile");
        assert_eq!(to_pascal_case("user_profile"), "UserProfile");
        assert_eq!(to_camel_case("user-profile"), "userProfile");
        assert_eq!(to_title_case("user_profile id"), "User Profile Id");
        assert_eq!(to_constant_case("userProfile42"), "USER_PROFILE_42");
    }

    #[test]
    fn collapses_punctuation_and_repeated_separators() {
        assert_eq!(to_snake_case("hello---world"), "hello_world");
        assert_eq!(to_kebab_case("hello___world"), "hello-world");
        assert_eq!(
            to_title_case("  release...candidate  "),
            "Release Candidate"
        );
    }

    #[test]
    fn supports_unicode_case_conversions_conservatively() {
        assert_eq!(to_title_case("élan vital"), "Élan Vital");
        assert_eq!(to_snake_case("Straße Mode"), "straße_mode");
    }

    #[test]
    fn detects_supported_cases() {
        assert_eq!(detect_case("snake_case"), TextCase::Snake);
        assert_eq!(detect_case("kebab-case"), TextCase::Kebab);
        assert_eq!(detect_case("Title Case"), TextCase::Title);
        assert_eq!(detect_case("CamelCase"), TextCase::Pascal);
        assert_eq!(detect_case("camelCase"), TextCase::Camel);
        assert_eq!(detect_case("MIXED_case-value"), TextCase::Mixed);
    }

    #[test]
    fn case_conversion_reports_unsupported_targets() {
        let conversion = CaseConversion::new(TextCase::Snake, TextCase::Mixed);
        assert_eq!(
            conversion.apply("hello_world"),
            Err(CaseError::UnsupportedTarget(TextCase::Mixed))
        );
    }

    #[test]
    fn case_conversion_applies_supported_targets() {
        let conversion = CaseConversion::new(TextCase::Snake, TextCase::Pascal);
        assert_eq!(
            conversion.apply("hello_world"),
            Ok(String::from("HelloWorld"))
        );
        assert_eq!(conversion.source(), TextCase::Snake);
        assert_eq!(conversion.target(), TextCase::Pascal);
    }
}