scsys_utils/casing/
mod.rs1#![cfg(feature = "alloc")]
9#[doc(inline)]
10pub use self::{kind::*, utils::*};
11
12mod kind;
13
14pub(crate) mod prelude {
15 pub use super::kind::CaseType;
16}
17
18mod utils {
19 #[cfg(feature = "alloc")]
20 use alloc::string::String;
21
22 pub fn to_snakecase(s: &str) -> String {
24 s.chars()
25 .fold(String::new(), |mut acc, c| {
26 if c.is_uppercase() {
27 if !acc.is_empty() {
28 acc.push('_');
29 }
30 acc.push(c.to_lowercase().next().unwrap());
31 } else {
32 acc.push(c);
33 }
34 acc
35 })
36 .to_lowercase()
37 }
38
39 pub fn to_camelcase(s: &str) -> String {
41 let mut chars = s.chars();
42 let first = chars.next().unwrap();
43 let rest = chars.collect::<String>();
44 format!("{}{}", first.to_lowercase(), rest)
45 }
46
47 pub fn to_pascalcase(s: &str) -> String {
49 let mut chars = s.chars();
50 let first = chars.next().unwrap();
51 let rest = chars.collect::<String>();
52 format!("{}{}", first.to_uppercase(), rest)
53 }
54
55 pub fn to_kebabcase(s: &str) -> String {
57 s.chars()
58 .fold(String::new(), |mut acc, c| {
59 if c.is_uppercase() {
60 if !acc.is_empty() {
61 acc.push('-');
62 }
63 acc.push(c.to_lowercase().next().unwrap());
64 } else {
65 acc.push(c);
66 }
67 acc
68 })
69 .to_lowercase()
70 }
71
72 pub fn to_screaming_snakecase(s: &str) -> String {
74 s.chars().fold(String::new(), |mut acc, c| {
75 if c.is_uppercase() {
76 if !acc.is_empty() {
77 acc.push('_');
78 }
79 acc.push(c);
80 } else {
81 acc.push(c.to_uppercase().next().unwrap());
82 }
83 acc
84 })
85 }
86}