Skip to content

Commit ddc1a63

Browse files
committed
sanitize
1 parent 874eacb commit ddc1a63

File tree

1 file changed

+20
-53
lines changed

1 file changed

+20
-53
lines changed

src/util.rs

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,6 @@ pub enum Case {
6969
}
7070

7171
impl Case {
72-
pub fn to_case<'a>(&self, s: &'a str) -> Cow<'a, str> {
73-
match self {
74-
Self::Constant => {
75-
if s.is_constant_case() {
76-
s.into()
77-
} else {
78-
s.to_constant_case().into()
79-
}
80-
}
81-
Self::Pascal => {
82-
if s.is_pascal_case() {
83-
s.into()
84-
} else {
85-
s.to_pascal_case().into()
86-
}
87-
}
88-
Self::Snake => {
89-
if s.is_snake_case() {
90-
s.into()
91-
} else {
92-
s.to_snake_case().into()
93-
}
94-
}
95-
}
96-
}
9772
pub fn cow_to_case<'a>(&self, cow: Cow<'a, str>) -> Cow<'a, str> {
9873
match self {
9974
Self::Constant => match cow {
@@ -110,6 +85,15 @@ impl Case {
11085
},
11186
}
11287
}
88+
pub fn sanitize<'a>(&self, s: &'a str) -> Cow<'a, str> {
89+
let s = if s.contains(BLACKLIST_CHARS) {
90+
Cow::Owned(s.replace(BLACKLIST_CHARS, ""))
91+
} else {
92+
s.into()
93+
};
94+
95+
self.cow_to_case(s)
96+
}
11397
}
11498

11599
fn current_dir() -> PathBuf {
@@ -244,48 +228,31 @@ pub trait ToSanitizedCase {
244228

245229
impl ToSanitizedCase for str {
246230
fn to_sanitized_pascal_case(&self) -> Cow<str> {
247-
let s = if self.contains(BLACKLIST_CHARS) {
248-
Cow::Owned(self.replace(BLACKLIST_CHARS, ""))
249-
} else {
250-
self.into()
251-
};
252-
231+
let s = Case::Pascal.sanitize(self);
253232
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
254-
Cow::from(format!("_{}", Case::Pascal.to_case(&s)))
233+
Cow::from(format!("_{}", s))
255234
} else {
256-
Case::Pascal.cow_to_case(s)
235+
s
257236
}
258237
}
259238
fn to_sanitized_constant_case(&self) -> Cow<str> {
260-
let s = if self.contains(BLACKLIST_CHARS) {
261-
Cow::Owned(self.replace(BLACKLIST_CHARS, ""))
262-
} else {
263-
self.into()
264-
};
265-
239+
let s = Case::Constant.sanitize(self);
266240
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
267-
Cow::from(format!("_{}", Case::Constant.to_case(&s)))
241+
Cow::from(format!("_{}", s))
268242
} else {
269-
Case::Constant.cow_to_case(s)
243+
s
270244
}
271245
}
272246
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str> {
273247
const INTERNALS: [&str; 4] = ["set_bit", "clear_bit", "bit", "bits"];
274248

275-
let s = if self.contains(BLACKLIST_CHARS) {
276-
Cow::Owned(self.replace(BLACKLIST_CHARS, ""))
277-
} else {
278-
self.into()
279-
};
249+
let s = Case::Snake.sanitize(self);
280250
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
281-
format!("_{}", Case::Snake.to_case(&s)).into()
251+
format!("_{}", s).into()
252+
} else if INTERNALS.contains(&s.as_ref()) {
253+
s + "_"
282254
} else {
283-
let s = Case::Snake.cow_to_case(s);
284-
if INTERNALS.contains(&s.as_ref()) {
285-
s + "_"
286-
} else {
287-
s
288-
}
255+
s
289256
}
290257
}
291258
}

0 commit comments

Comments
 (0)