diff --git a/src/utils.rs b/src/utils.rs index 53c6515..bc3e34a 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 in_uppercase_run = false; + let mut iter = ident.chars().peekable(); + while let Some(c) = iter.next() { match c { '-' => { snake_case.push('_'); @@ -24,8 +26,27 @@ pub fn convert_to_snake_case(ident: &str) -> String { // ignored } c => { - if c.is_ascii_uppercase() && !snake_case.is_empty() { - snake_case.push('_') + // NFT -> nft + // IPAddress -> ip_address + // 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/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" } } diff --git a/tests/core/input.cddl b/tests/core/input.cddl index 1c8cbd8..7fa0a8c 100644 --- a/tests/core/input.cddl +++ b/tests/core/input.cddl @@ -256,6 +256,17 @@ enum_opt_embed_fields = [ 1, ? overlapping_inlined, #6.13(13) ] +casing_test = [ + ; @name NFT + 1 // + ; @name IPAddress + 2 // + ; @name ShelleyMA + 3 // + ; @name VRF_vkey + 4 +] + custom_bytes = bytes ; @custom_serialize custom_serialize_bytes @custom_deserialize custom_deserialize_bytes struct_with_custom_serialization = [ diff --git a/tests/core/tests.rs b/tests/core/tests.rs index 444ade0..a026cde 100644 --- a/tests/core/tests.rs +++ b/tests/core/tests.rs @@ -508,6 +508,18 @@ mod tests { } #[test] + fn casing_test() { + // 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() { let struct_with_custom_bytes = StructWithCustomSerialization::new( vec![0xCA, 0xFE, 0xF0, 0x0D],