Skip to content

Commit 1ec516f

Browse files
Handle runs of uppercase letters for name generation (#218)
* Handle runs of uppercase letters for name generation * Check casing in test * Inline more examples in code comments * Fix clippy error * Add missing WASM_BINDGEN_WEAKREF in package * Fixes for handling other cases e.g. `shelley_ma` --------- Co-authored-by: rooooooooob <[email protected]> Co-authored-by: rooooooooob <[email protected]>
1 parent ac100c2 commit 1ec516f

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

src/utils.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub fn cbor_type_code_str(cbor_type: cbor_event::Type) -> &'static str {
1515

1616
pub fn convert_to_snake_case(ident: &str) -> String {
1717
let mut snake_case = String::new();
18-
for c in ident.chars() {
18+
let mut in_uppercase_run = false;
19+
let mut iter = ident.chars().peekable();
20+
while let Some(c) = iter.next() {
1921
match c {
2022
'-' => {
2123
snake_case.push('_');
@@ -24,8 +26,27 @@ pub fn convert_to_snake_case(ident: &str) -> String {
2426
// ignored
2527
}
2628
c => {
27-
if c.is_ascii_uppercase() && !snake_case.is_empty() {
28-
snake_case.push('_')
29+
// NFT -> nft
30+
// IPAddress -> ip_address
31+
// shelley_MA -> shelley_ma
32+
if in_uppercase_run {
33+
if c.is_ascii_uppercase() {
34+
if let Some(next) = iter.peek() {
35+
if next.is_ascii_lowercase() {
36+
if !snake_case.is_empty() {
37+
snake_case.push('_');
38+
}
39+
in_uppercase_run = false;
40+
}
41+
}
42+
} else {
43+
in_uppercase_run = false;
44+
}
45+
} else if c.is_ascii_uppercase() {
46+
if !snake_case.is_empty() {
47+
snake_case.push('_');
48+
}
49+
in_uppercase_run = true;
2950
}
3051
snake_case.push(c.to_ascii_lowercase());
3152
}

static/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "0.0.1",
44
"description": "cddl-codegen generated library",
55
"scripts": {
6-
"rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=nodejs; wasm-pack pack",
7-
"rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=browser; wasm-pack pack"
6+
"rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; wasm-pack pack",
7+
"rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; wasm-pack pack"
88
},
99
"devDependencies": {
10-
"rimraf": "3.0.2"
10+
"rimraf": "3.0.2",
11+
"cross-env": "^7.0.3"
1112
}
1213
}

static/package_json_schemas.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"version": "0.0.1",
44
"description": "cddl-codegen generated library",
55
"scripts": {
6-
"rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=nodejs; cd ../..; npm run js:ts-json-gen; cd rust/wasm; wasm-pack pack",
7-
"rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=browser; cd ../..; npm run js:ts-json-gen; cd rust/wasm; wasm-pack pack",
6+
"rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; cd ../..; npm run js:ts-json-gen; cd rust/wasm; wasm-pack pack",
7+
"rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; cd ../..; npm run js:ts-json-gen; cd rust/wasm; wasm-pack pack",
88
"js:ts-json-gen": "cd rust/json-gen && cargo +stable run && cd ../.. && node ./scripts/run-json2ts.js && node ./scripts/json-ts-types.js"
99
},
1010
"devDependencies": {
1111
"json-schema-to-typescript": "^10.1.5",
12-
"rimraf": "3.0.2"
12+
"rimraf": "3.0.2",
13+
"cross-env": "^7.0.3"
1314
}
1415
}

tests/core/input.cddl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,17 @@ enum_opt_embed_fields = [
256256
1, ? overlapping_inlined, #6.13(13)
257257
]
258258

259+
casing_test = [
260+
; @name NFT
261+
1 //
262+
; @name IPAddress
263+
2 //
264+
; @name ShelleyMA
265+
3 //
266+
; @name VRF_vkey
267+
4
268+
]
269+
259270
custom_bytes = bytes ; @custom_serialize custom_serialize_bytes @custom_deserialize custom_deserialize_bytes
260271

261272
struct_with_custom_serialization = [

tests/core/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,18 @@ mod tests {
508508
}
509509

510510
#[test]
511+
fn casing_test() {
512+
// these are just testing that these exist under these names
513+
let _ = CasingTest::new_nft();
514+
let _ = CasingTest::NFT;
515+
let _ = CasingTest::new_ip_address();
516+
let _ = CasingTest::IPAddress;
517+
let _ = CasingTest::new_shelley_ma();
518+
let _ = CasingTest::ShelleyMA;
519+
let _ = CasingTest::new_vrf_vkey();
520+
let _ = CasingTest::VRFVkey;
521+
}
522+
511523
fn custom_serialization() {
512524
let struct_with_custom_bytes = StructWithCustomSerialization::new(
513525
vec![0xCA, 0xFE, 0xF0, 0x0D],

0 commit comments

Comments
 (0)