From ec78faa1b06de455d1f69da3365c5567f5440b54 Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Tue, 26 Dec 2023 04:35:51 +0900 Subject: [PATCH 1/6] Handle runs of uppercase letters for name generation --- src/utils.rs | 13 ++++++++++--- tests/core/input.cddl | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 53c6515..12816a3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,7 +15,9 @@ pub fn cbor_type_code_str(cbor_type: cbor_event::Type) -> &'static str { pub fn convert_to_snake_case(ident: &str) -> String { let mut snake_case = String::new(); - for c in ident.chars() { + + let mut iter = ident.chars().peekable(); + while let Some(c) = iter.next() { match c { '-' => { snake_case.push('_'); @@ -24,8 +26,13 @@ pub fn convert_to_snake_case(ident: &str) -> String { // ignored } c => { - if c.is_ascii_uppercase() && !snake_case.is_empty() { - snake_case.push('_') + if !snake_case.is_empty() && c.is_ascii_uppercase() { + if let Some(next) = iter.peek() { + // IPAddress but not I64 + if next.is_ascii_lowercase() { + snake_case.push('_'); + } + } } snake_case.push(c.to_ascii_lowercase()); } diff --git a/tests/core/input.cddl b/tests/core/input.cddl index eb9ed93..0a8a1d2 100644 --- a/tests/core/input.cddl +++ b/tests/core/input.cddl @@ -191,3 +191,10 @@ enum_opt_embed_fields = [ ; @name eg 1, ? overlapping_inlined, #6.13(13) ] + +casing_test = [ + ; @name NFT + 1 // + ; @name IPAddress + 2 +] \ No newline at end of file From 7a343eaada666dbc3f8c8ad2d34034e005a58ad6 Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Tue, 26 Dec 2023 04:39:21 +0900 Subject: [PATCH 2/6] Check casing in test --- tests/core/tests.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/core/tests.rs b/tests/core/tests.rs index 256dbae..4251047 100644 --- a/tests/core/tests.rs +++ b/tests/core/tests.rs @@ -427,4 +427,10 @@ mod tests { let g2 = EnumOptEmbedFields::new_eg(None); deser_test(&g2); } + + #[test] + fn casing_test() { + CasingTest::new_nft(); + CasingTest::new_ip_address(); + } } From b3d0109878636cdf8a4ef27663c37baaec388dcb Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Tue, 26 Dec 2023 04:42:27 +0900 Subject: [PATCH 3/6] Inline more examples in code comments --- src/utils.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index 12816a3..c4d7aee 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,6 +26,8 @@ pub fn convert_to_snake_case(ident: &str) -> String { // ignored } c => { + // NFT -> nft + // IPAddress -> ip_address if !snake_case.is_empty() && c.is_ascii_uppercase() { if let Some(next) = iter.peek() { // IPAddress but not I64 From 1b97e6308859ffd3f608c3fb734ada463c2f5b28 Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Tue, 26 Dec 2023 19:48:21 +0900 Subject: [PATCH 4/6] Fix clippy error --- src/test.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test.rs b/src/test.rs index 72cbe7b..e462f17 100644 --- a/src/test.rs +++ b/src/test.rs @@ -45,7 +45,6 @@ fn run_test( assert!(cargo_run_result.status.success()); // copy tests into generated code let mut lib_rs = std::fs::OpenOptions::new() - .write(true) .append(true) .open(test_path.join(format!("{export_path}/rust/src/lib.rs"))) .unwrap(); @@ -97,7 +96,6 @@ fn run_test( if let Some(external_wasm_file_path) = external_wasm_file_path { println!("trying to open: {external_wasm_file_path:?}"); let mut wasm_lib_rs = std::fs::OpenOptions::new() - .write(true) .append(true) .open(test_path.join(format!("{export_path}/wasm/src/lib.rs"))) .unwrap(); From 9c048cbcb261cd8e1652b2df31e55ac114bbbe02 Mon Sep 17 00:00:00 2001 From: Sebastien Guillemot Date: Tue, 26 Dec 2023 19:48:57 +0900 Subject: [PATCH 5/6] Add missing WASM_BINDGEN_WEAKREF in package --- static/package.json | 7 ++++--- static/package_json_schemas.json | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/static/package.json b/static/package.json index a44998c..d3f7ae4 100644 --- a/static/package.json +++ b/static/package.json @@ -3,10 +3,11 @@ "version": "0.0.1", "description": "cddl-codegen generated library", "scripts": { - "rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=nodejs; wasm-pack pack", - "rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; wasm-pack build --target=browser; wasm-pack pack" + "rust:build-nodejs": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; wasm-pack pack", + "rust:build-browser": "rimraf ./rust/wasm/pkg && cd rust/wasm; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; wasm-pack pack" }, "devDependencies": { - "rimraf": "3.0.2" + "rimraf": "3.0.2", + "cross-env": "^7.0.3" } } diff --git a/static/package_json_schemas.json b/static/package_json_schemas.json index af90d65..b613c56 100644 --- a/static/package_json_schemas.json +++ b/static/package_json_schemas.json @@ -3,12 +3,13 @@ "version": "0.0.1", "description": "cddl-codegen generated library", "scripts": { - "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", - "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", + "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", + "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", "js:ts-json-gen": "cd rust/json-gen && cargo +stable run && cd ../.. && node ./scripts/run-json2ts.js && node ./scripts/json-ts-types.js" }, "devDependencies": { "json-schema-to-typescript": "^10.1.5", - "rimraf": "3.0.2" + "rimraf": "3.0.2", + "cross-env": "^7.0.3" } } From fb3b630a519eee6f673d318e219d7313e353c0ea Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Mon, 1 Jul 2024 11:32:16 -0700 Subject: [PATCH 6/6] Fixes for handling other cases e.g. `shelley_ma` --- src/utils.rs | 24 ++++++++++++++++++------ tests/core/input.cddl | 6 +++++- tests/core/tests.rs | 11 +++++++++-- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index c4d7aee..bc3e34a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,7 +15,7 @@ pub fn cbor_type_code_str(cbor_type: cbor_event::Type) -> &'static str { pub fn convert_to_snake_case(ident: &str) -> String { let mut snake_case = String::new(); - + let mut in_uppercase_run = false; let mut iter = ident.chars().peekable(); while let Some(c) = iter.next() { match c { @@ -28,13 +28,25 @@ pub fn convert_to_snake_case(ident: &str) -> String { c => { // NFT -> nft // IPAddress -> ip_address - if !snake_case.is_empty() && c.is_ascii_uppercase() { - if let Some(next) = iter.peek() { - // IPAddress but not I64 - if next.is_ascii_lowercase() { - snake_case.push('_'); + // shelley_MA -> shelley_ma + if in_uppercase_run { + if c.is_ascii_uppercase() { + if let Some(next) = iter.peek() { + if next.is_ascii_lowercase() { + if !snake_case.is_empty() { + snake_case.push('_'); + } + in_uppercase_run = false; + } } + } else { + in_uppercase_run = false; + } + } else if c.is_ascii_uppercase() { + if !snake_case.is_empty() { + snake_case.push('_'); } + in_uppercase_run = true; } snake_case.push(c.to_ascii_lowercase()); } diff --git a/tests/core/input.cddl b/tests/core/input.cddl index 3c6dd1b..5174219 100644 --- a/tests/core/input.cddl +++ b/tests/core/input.cddl @@ -260,7 +260,11 @@ casing_test = [ ; @name NFT 1 // ; @name IPAddress - 2 + 2 // + ; @name ShelleyMA + 3 // + ; @name VRF_vkey + 4 ] custom_bytes = bytes ; @custom_serialize custom_serialize_bytes @custom_deserialize custom_deserialize_bytes diff --git a/tests/core/tests.rs b/tests/core/tests.rs index 200e77c..837d9d5 100644 --- a/tests/core/tests.rs +++ b/tests/core/tests.rs @@ -509,8 +509,15 @@ mod tests { #[test] fn casing_test() { - CasingTest::new_nft(); - CasingTest::new_ip_address(); + // these are just testing that these exist under these names + let _ = CasingTest::new_nft(); + let _ = CasingTest::NFT; + let _ = CasingTest::new_ip_address(); + let _ = CasingTest::IPAddress; + let _ = CasingTest::new_shelley_ma(); + let _ = CasingTest::ShelleyMA; + let _ = CasingTest::new_vrf_vkey(); + let _ = CasingTest::VRFVkey; } fn custom_serialization() {