forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.rs
233 lines (217 loc) · 8.91 KB
/
style.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
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
/// Find the first index in the string which separates a lowercase character from an uppercase
/// character. Used for splitting words in a CamelCase style identifier.
fn find_camel_case_word_boundary(name: &str) -> Option<usize> {
let mut previous_char_was_lowercase = false;
for (index, c) in name.char_indices() {
if c.is_uppercase() && previous_char_was_lowercase {
return Some(index);
}
previous_char_was_lowercase = c.is_lowercase();
}
None
}
/// Split a CamelCase style identifier into words.
fn camel_case_split_words(mut name: &str) -> impl Iterator<Item = &str> {
std::iter::from_fn(move || {
if name.is_empty() {
return None;
}
let index = find_camel_case_word_boundary(name).unwrap_or(name.len());
let word = &name[..index];
name = &name[index..];
Some(word)
})
}
/// Split a string of unknown style into words.
fn split_words(name: &str) -> impl Iterator<Item = &str> {
name.split('_').flat_map(camel_case_split_words)
}
/// Detect whether an name is written in snake_case.
pub fn is_snake_case(name: &str) -> bool {
let trimmed = name.trim_start_matches('_');
!trimmed.contains("__") && !trimmed.contains(char::is_uppercase)
}
/// Detect whether a name is written in SCREAMING_SNAKE_CASE.
pub fn is_screaming_snake_case(name: &str) -> bool {
let trimmed = name.trim_start_matches('_');
!trimmed.contains("__") && !trimmed.contains(char::is_lowercase)
}
/// Detect whether a name is written in UpperCamelCase.
pub fn is_upper_camel_case(name: &str) -> bool {
let trimmed = name.trim_start_matches('_');
!trimmed.contains('_') && !trimmed.starts_with(char::is_lowercase)
}
/// Convert an identifier into snake_case. This is a best-guess at what the name would look
/// like if it were expressed in the correct style.
pub fn to_snake_case(name: &str) -> String {
let mut ret = String::with_capacity(name.len());
let (leading_underscores, trimmed) =
name.split_at(name.find(|c| c != '_').unwrap_or(name.len()));
ret.push_str(leading_underscores);
let mut words = split_words(trimmed);
if let Some(word) = words.next() {
ret.extend(word.chars().flat_map(char::to_lowercase));
for word in words {
ret.push('_');
ret.extend(word.chars().flat_map(char::to_lowercase));
}
}
ret
}
/// Convert a name into SCREAMING_SNAKE_CASE. This is a best-guess at what the name
/// would look like if it were expressed in the correct style.
pub fn to_screaming_snake_case(name: &str) -> String {
let mut ret = String::with_capacity(name.len());
let (leading_underscores, trimmed) =
name.split_at(name.find(|c| c != '_').unwrap_or(name.len()));
ret.push_str(leading_underscores);
let mut words = split_words(trimmed);
if let Some(word) = words.next() {
ret.extend(word.chars().flat_map(char::to_uppercase));
for word in words {
ret.push('_');
ret.extend(word.chars().flat_map(char::to_uppercase));
}
}
ret
}
/// Convert an identifier into UpperCamelCase. This is a best-guess at what the identifier would
/// look like if it were expressed in the correct style.
pub fn to_upper_camel_case(name: &str) -> String {
let mut ret = String::with_capacity(name.len());
let (leading_underscores, trimmed) =
name.split_at(name.find(|c| c != '_').unwrap_or(name.len()));
ret.push_str(leading_underscores);
for word in split_words(trimmed) {
let mut chars = word.chars();
if let Some(c) = chars.next() {
ret.extend(c.to_uppercase());
ret.extend(chars.flat_map(char::to_lowercase));
}
}
ret
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn detect_styles() {
let snake_case_idents = [
"hello",
"__hello",
"blah32",
"some_words_here",
"___some_words_here",
];
let screaming_snake_case_idents = ["SOME_WORDS_HERE", "___SOME_WORDS_HERE"];
let upper_camel_case_idents = [
"Hello",
"__Hello",
"Blah32",
"SomeWordsHere",
"___SomeWordsHere",
];
let screaming_snake_case_or_upper_camel_case_idents = ["HELLO", "__HELLO", "BLAH32"];
let styleless_idents = ["Mix_Of_Things", "__Mix_Of_Things", "FooBar_123"];
for ident in &snake_case_idents {
assert!(is_snake_case(ident));
assert!(!is_screaming_snake_case(ident));
assert!(!is_upper_camel_case(ident));
}
for ident in &screaming_snake_case_idents {
assert!(!is_snake_case(ident));
assert!(is_screaming_snake_case(ident));
assert!(!is_upper_camel_case(ident));
}
for ident in &upper_camel_case_idents {
assert!(!is_snake_case(ident));
assert!(!is_screaming_snake_case(ident));
assert!(is_upper_camel_case(ident));
}
for ident in &screaming_snake_case_or_upper_camel_case_idents {
assert!(!is_snake_case(ident));
assert!(is_screaming_snake_case(ident));
assert!(is_upper_camel_case(ident));
}
for ident in &styleless_idents {
assert!(!is_snake_case(ident));
assert!(!is_screaming_snake_case(ident));
assert!(!is_upper_camel_case(ident));
}
}
#[test]
fn convert_to_snake_case() {
assert_eq!("hello", to_snake_case("HELLO"));
assert_eq!("___hello", to_snake_case("___HELLO"));
assert_eq!("blah32", to_snake_case("BLAH32"));
assert_eq!("some_words_here", to_snake_case("SOME_WORDS_HERE"));
assert_eq!("___some_words_here", to_snake_case("___SOME_WORDS_HERE"));
assert_eq!("hello", to_snake_case("Hello"));
assert_eq!("___hello", to_snake_case("___Hello"));
assert_eq!("blah32", to_snake_case("Blah32"));
assert_eq!("some_words_here", to_snake_case("SomeWordsHere"));
assert_eq!("___some_words_here", to_snake_case("___SomeWordsHere"));
assert_eq!("some_words_here", to_snake_case("someWordsHere"));
assert_eq!("___some_words_here", to_snake_case("___someWordsHere"));
assert_eq!("mix_of_things", to_snake_case("Mix_Of_Things"));
assert_eq!("__mix_of_things", to_snake_case("__Mix_Of_Things"));
assert_eq!("foo_bar_123", to_snake_case("FooBar_123"));
}
#[test]
fn convert_to_screaming_snake_case() {
assert_eq!("HELLO", to_screaming_snake_case("hello"));
assert_eq!("___HELLO", to_screaming_snake_case("___hello"));
assert_eq!("BLAH32", to_screaming_snake_case("blah32"));
assert_eq!(
"SOME_WORDS_HERE",
to_screaming_snake_case("some_words_here")
);
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___some_words_here")
);
assert_eq!("HELLO", to_screaming_snake_case("Hello"));
assert_eq!("___HELLO", to_screaming_snake_case("___Hello"));
assert_eq!("BLAH32", to_screaming_snake_case("Blah32"));
assert_eq!("SOME_WORDS_HERE", to_screaming_snake_case("SomeWordsHere"));
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___SomeWordsHere")
);
assert_eq!("SOME_WORDS_HERE", to_screaming_snake_case("someWordsHere"));
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___someWordsHere")
);
assert_eq!("MIX_OF_THINGS", to_screaming_snake_case("Mix_Of_Things"));
assert_eq!(
"__MIX_OF_THINGS",
to_screaming_snake_case("__Mix_Of_Things")
);
assert_eq!("FOO_BAR_123", to_screaming_snake_case("FooBar_123"));
}
#[test]
fn convert_to_upper_camel_case() {
assert_eq!("Hello", to_upper_camel_case("hello"));
assert_eq!("___Hello", to_upper_camel_case("___hello"));
assert_eq!("Blah32", to_upper_camel_case("blah32"));
assert_eq!("SomeWordsHere", to_upper_camel_case("some_words_here"));
assert_eq!(
"___SomeWordsHere",
to_upper_camel_case("___some_words_here")
);
assert_eq!("Hello", to_upper_camel_case("HELLO"));
assert_eq!("___Hello", to_upper_camel_case("___HELLO"));
assert_eq!("Blah32", to_upper_camel_case("BLAH32"));
assert_eq!("SomeWordsHere", to_upper_camel_case("SOME_WORDS_HERE"));
assert_eq!(
"___SomeWordsHere",
to_upper_camel_case("___SOME_WORDS_HERE")
);
assert_eq!("SomeWordsHere", to_upper_camel_case("someWordsHere"));
assert_eq!("___SomeWordsHere", to_upper_camel_case("___someWordsHere"));
assert_eq!("MixOfThings", to_upper_camel_case("Mix_Of_Things"));
assert_eq!("__MixOfThings", to_upper_camel_case("__Mix_Of_Things"));
assert_eq!("FooBar123", to_upper_camel_case("FooBar_123"));
}
}