codama_nodes/shared/
camel_case_string.rs

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
use serde::{Deserialize, Serialize};
use std::ops::Deref;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub struct CamelCaseString(String);

impl CamelCaseString {
    pub fn new<T>(string: T) -> Self
    where
        T: AsRef<str>,
    {
        Self(to_camel_case(string.as_ref()))
    }
}

impl From<CamelCaseString> for String {
    fn from(val: CamelCaseString) -> Self {
        val.0
    }
}

impl From<String> for CamelCaseString {
    fn from(string: String) -> Self {
        Self::new(string)
    }
}

impl From<&str> for CamelCaseString {
    fn from(string: &str) -> Self {
        Self::new(string)
    }
}

impl Deref for CamelCaseString {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for CamelCaseString {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

fn to_camel_case(input: &str) -> String {
    let mut result = String::new();
    let mut new_word = true;

    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];

        if c.is_alphanumeric() {
            if new_word && !result.is_empty() {
                // Capitalize the first letter of each new word (except the first word)
                result.extend(c.to_uppercase());
            } else {
                // Lowercase the first letter of the first word and other letters
                result.extend(c.to_lowercase());
            }
            new_word = false;
        } else {
            new_word = true;
        }

        // Treat numbers as their own "words" to start a new word afterward
        if c.is_numeric() {
            new_word = true;
        }

        // Handle transitions from lowercase to uppercase (e.g., PascalCase)
        if i + 1 < chars.len() && c.is_lowercase() && chars[i + 1].is_uppercase() {
            new_word = true;
        }

        i += 1;
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_from_title_case() {
        let value = CamelCaseString::new(String::from("Hello This is a Long Title!"));
        assert_eq!(value.0, "helloThisIsALongTitle");
    }

    #[test]
    fn parse_from_numbers() {
        let value = CamelCaseString::new(String::from("This123 str1ng has 456n numbers"));
        assert_eq!(value.0, "this123Str1NgHas456NNumbers");
    }

    #[test]
    fn parse_from_snake_case() {
        let value = CamelCaseString::new(String::from("hello_this_is__a_snake_case"));
        assert_eq!(value.0, "helloThisIsASnakeCase");
    }

    #[test]
    fn parse_from_pascal_case() {
        let value = CamelCaseString::new(String::from("HelloThisIs7PascalCaseWords"));
        assert_eq!(value.0, "helloThisIs7PascalCaseWords");
    }

    #[test]
    fn parse_from_special_chars() {
        let value = CamelCaseString::new(String::from("crate::hello:world?*,this+is!a#test"));
        assert_eq!(value.0, "crateHelloWorldThisIsATest");
    }

    #[test]
    fn double_parse() {
        let value = to_camel_case("my_value");
        let value = to_camel_case(&value);
        assert_eq!(value, "myValue");
    }

    #[test]
    fn new_from_string() {
        let value = CamelCaseString::new(String::from("my_value"));
        assert_eq!(value.0, "myValue");
    }

    #[test]
    fn new_from_str() {
        let value = CamelCaseString::new("my_value");
        assert_eq!(value.0, "myValue");
    }

    #[test]
    fn new_from_self() {
        let value = CamelCaseString::new(CamelCaseString::new("my_value"));
        assert_eq!(value.0, "myValue");
    }

    #[test]
    fn from_string() {
        let value: CamelCaseString = String::from("my_value").into();
        assert_eq!(value.0, "myValue");
    }

    #[test]
    fn from_str() {
        let value: CamelCaseString = "my_value".into();
        assert_eq!(value.0, "myValue");
    }

    #[test]
    fn into_string() {
        let value: String = CamelCaseString::new("my_value").into();
        assert_eq!(value, "myValue");
    }

    #[test]
    fn deref() {
        let value = CamelCaseString::new("Hello World!");
        assert_eq!(*value, "helloWorld");
    }

    #[test]
    fn as_ref() {
        let value = CamelCaseString::new("Hello World!");
        assert_eq!(value.as_ref(), "helloWorld");
    }

    #[test]
    fn to_json() {
        let value = CamelCaseString::new("helloWorld");
        let json = serde_json::to_string(&value).unwrap();
        assert_eq!(json, "\"helloWorld\"");
    }

    #[test]
    fn from_json() {
        let json = "\"helloWorld\"";
        let value: CamelCaseString = serde_json::from_str(json).unwrap();
        assert_eq!(value, CamelCaseString::new("helloWorld"));
    }
}