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
pub mod formats;
pub mod guesser;
pub mod naming_conventions;
pub mod objects;
pub mod traits;
pub use objects::{Token, Tokens};
pub use traits::Case;
#[derive(Debug)]
pub struct CaseStyle {
pub tokens: Tokens,
pub original: String,
pub case_info: Option<String>,
}
macro_rules! impl_case {
($from_name:ident, $to_name:ident, $input_type:ty, $type_name:expr) => {
impl CaseStyle {
#[doc = "uses"]
#[doc = $type_name]
#[doc = "Default trait to convert to CaseStyle"]
pub fn $from_name<S: AsRef<str>>(input: S) -> CaseStyle {
Self::decode(&<$input_type>::default(), input)
}
#[doc = "uses"]
#[doc = $type_name]
#[doc = "Default trait to convert to String"]
pub fn $to_name(self) -> String {
Self::encode(&<$input_type>::default(), self)
}
}
};
($from_name:ident, $to_name:ident, $input_type:ty) => {
impl_case!($from_name, $to_name, $input_type, stringify!($input_type));
};
}
impl_case!(from_snakecase, to_snakecase, formats::SnakeCase);
impl_case!(from_camelcase, to_camelcase, formats::CamelCase);
impl_case!(from_kebabcase, to_kebabcase, formats::KebabCase);
impl_case!(
from_lowercase_spacecase,
to_lowercase_spacecase,
formats::LowercaseSpace
);
impl_case!(from_pascalcase, to_pascalcase, formats::PascalCase);
impl_case!(from_constantcase, to_constantcase, formats::ConstantCase);
impl_case!(from_sentencecase, to_sentencecase, formats::SentenceCase);
impl CaseStyle {
pub fn decode<T: Case, S: AsRef<str>>(case: &T, input: S) -> CaseStyle {
case.parse_str(input.as_ref())
}
pub fn encode<T: Case>(case: &T, input: CaseStyle) -> String {
case.build_string(input)
}
pub fn guess<S: AsRef<str>>(input: S) -> Result<CaseStyle, ()> {
let case_type = guesser::guess_type(input.as_ref())?;
Ok(case_type.parse_str(input.as_ref()))
}
}
#[cfg(test)]
mod invertable {
use crate::CaseStyle;
#[test]
fn snake() {
assert_eq!(
CaseStyle::from_snakecase("snake_case").to_snakecase(),
"snake_case"
);
}
#[test]
fn kebab() {
assert_eq!(
CaseStyle::from_kebabcase("kebab-case").to_kebabcase(),
"kebab-case"
);
}
#[test]
fn camel() {
assert_eq!(
CaseStyle::from_camelcase("camelCase").to_camelcase(),
"camelCase"
);
}
#[test]
fn lowercase_space() {
assert_eq!(
CaseStyle::from_lowercase_spacecase("lowercase space").to_lowercase_spacecase(),
"lowercase space"
);
}
#[test]
fn pascalcasee() {
assert_eq!(
CaseStyle::from_pascalcase("PascalCase").to_pascalcase(),
"PascalCase"
);
}
#[test]
fn constant() {
assert_eq!(
CaseStyle::from_constantcase("CONSTANT_CASE").to_constantcase(),
"CONSTANT_CASE"
);
}
#[test]
fn sentence() {
assert_eq!(
CaseStyle::from_sentencecase("This is a test sentence.").to_sentencecase(),
"This is a test sentence."
);
}
}
#[test]
fn guess_test() {
let s = "snake-case";
let t = CaseStyle::guess(s).unwrap();
let x = t.to_camelcase();
assert_eq!(x, "snakeCase")
}