Skip to content

Commit

Permalink
Handle runs of uppercase letters for name generation (#218)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent ac100c2 commit 1ec516f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
27 changes: 24 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('_');
Expand All @@ -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());
}
Expand Down
7 changes: 4 additions & 3 deletions static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
7 changes: 4 additions & 3 deletions static/package_json_schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
11 changes: 11 additions & 0 deletions tests/core/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
12 changes: 12 additions & 0 deletions tests/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 1ec516f

Please sign in to comment.