refix_codegen/
case_converter.rs1pub fn snake_case(name: &str) -> String {
9 let mut converted = String::with_capacity(name.len() + 4);
10 let mut chars = name.chars().peekable();
11 let mut previous: Option<char> = None;
12
13 while let Some(current) = chars.next() {
14 if current.is_ascii_uppercase() {
15 let after_word = previous.is_some_and(|p| p.is_ascii_lowercase() || p.is_ascii_digit());
16 let ends_acronym = previous.is_some_and(|p| p.is_ascii_uppercase())
17 && chars.peek().is_some_and(|n| n.is_ascii_lowercase())
18 && !pluralizes(chars.clone());
19 if after_word || ends_acronym {
20 converted.push('_');
21 }
22 converted.push(current.to_ascii_lowercase());
23 } else {
24 converted.push(current);
25 }
26 previous = Some(current);
27 }
28
29 converted
30}
31
32fn pluralizes(mut rest: impl Iterator<Item = char>) -> bool {
35 rest.next() == Some('s') && !rest.next().is_some_and(|c| c.is_ascii_lowercase())
36}
37
38pub fn pascal_case(name: &str) -> String {
45 let mut converted = String::with_capacity(name.len());
46
47 for segment in name.split('_').filter(|segment| !segment.is_empty()) {
48 let mut chars = segment.chars();
49 if let Some(first) = chars.next() {
50 converted.push(first.to_ascii_uppercase());
51 }
52 for rest in chars {
53 converted.push(rest.to_ascii_lowercase());
54 }
55 }
56
57 converted
58}
59
60#[cfg(test)]
61mod tests {
62 use super::{pascal_case, snake_case};
63
64 #[test]
65 fn lowercases_a_single_word() {
66 assert_eq!(snake_case("Account"), "account");
67 }
68
69 #[test]
70 fn splits_words_at_case_changes() {
71 assert_eq!(snake_case("OrderQty"), "order_qty");
72 }
73
74 #[test]
75 fn keeps_a_trailing_acronym_together() {
76 assert_eq!(snake_case("ClOrdID"), "cl_ord_id");
77 }
78
79 #[test]
80 fn keeps_a_name_that_is_one_acronym_run_together() {
81 assert_eq!(snake_case("IOIID"), "ioiid");
82 }
83
84 #[test]
85 fn splits_a_leading_acronym_before_its_last_capital() {
86 assert_eq!(snake_case("MDEntryPx"), "md_entry_px");
87 }
88
89 #[test]
90 fn splits_an_acronym_in_the_middle_of_a_name() {
91 assert_eq!(snake_case("NoMDEntries"), "no_md_entries");
92 }
93
94 #[test]
95 fn splits_after_an_acronym_at_the_start() {
96 assert_eq!(snake_case("XMLData"), "xml_data");
97 }
98
99 #[test]
100 fn keeps_a_plural_acronym_together() {
101 assert_eq!(snake_case("NoPartyIDs"), "no_party_ids");
102 }
103
104 #[test]
105 fn splits_a_plural_acronym_from_the_next_word() {
106 assert_eq!(snake_case("PartyIDsSource"), "party_ids_source");
107 }
108
109 #[test]
110 fn attaches_digits_to_the_preceding_word() {
111 assert_eq!(snake_case("Nested2PartyID"), "nested2_party_id");
112 }
113
114 #[test]
115 fn capitalizes_a_single_word() {
116 assert_eq!(pascal_case("BUY"), "Buy");
117 }
118
119 #[test]
120 fn joins_words_at_underscores() {
121 assert_eq!(pascal_case("GOOD_TILL_CANCEL"), "GoodTillCancel");
122 }
123
124 #[test]
125 fn flattens_acronym_words() {
126 assert_eq!(pascal_case("IOI_QTY"), "IoiQty");
127 }
128
129 #[test]
130 fn keeps_digits_within_words() {
131 assert_eq!(pascal_case("FILL_OR_KILL_4"), "FillOrKill4");
132 }
133
134 #[test]
135 fn passes_digit_only_words_through() {
136 assert_eq!(pascal_case("FIX_4_4"), "Fix44");
137 }
138
139 #[test]
140 fn skips_empty_segments() {
141 assert_eq!(pascal_case("AT__THE_OPENING"), "AtTheOpening");
142 }
143
144 #[test]
145 fn normalizes_lowercase_input() {
146 assert_eq!(pascal_case("buy_minus"), "BuyMinus");
147 }
148}