From 6e9d195d26ad1f144eb2388f659f43a2341deac8 Mon Sep 17 00:00:00 2001 From: David Leeds Date: Thu, 6 Mar 2025 14:50:39 -0800 Subject: [PATCH] fix(generator): use single underscore in kebab to snake case conversion Addresses the case where "Kebab-Case" was converted to "kebab__case". After the change, we get "kebab_case". issue: #88 --- rasn-compiler-tests/tests/parameterization.rs | 12 ++++++------ rasn-compiler/src/generator/rasn/utils.rs | 9 +++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/rasn-compiler-tests/tests/parameterization.rs b/rasn-compiler-tests/tests/parameterization.rs index 0def266..2bdb9aa 100644 --- a/rasn-compiler-tests/tests/parameterization.rs +++ b/rasn-compiler-tests/tests/parameterization.rs @@ -113,18 +113,18 @@ e2e_pdu! { #[non_exhaustive] pub struct A2XPC5FlowBitRates { #[rasn(identifier = "a2X-GuaranteedFlowBitRate")] - pub a2_x__guaranteed_flow_bit_rate: bool, + pub a2_x_guaranteed_flow_bit_rate: bool, #[rasn(identifier = "iE-Extensions")] - pub i_e__extensions: Option, + pub i_e_extensions: Option, } impl A2XPC5FlowBitRates { pub fn new( - a2_x__guaranteed_flow_bit_rate: bool, - i_e__extensions: Option, + a2_x_guaranteed_flow_bit_rate: bool, + i_e_extensions: Option, ) -> Self { Self { - a2_x__guaranteed_flow_bit_rate, - i_e__extensions, + a2_x_guaranteed_flow_bit_rate, + i_e_extensions, } } } diff --git a/rasn-compiler/src/generator/rasn/utils.rs b/rasn-compiler/src/generator/rasn/utils.rs index 96843dd..b984f60 100644 --- a/rasn-compiler/src/generator/rasn/utils.rs +++ b/rasn-compiler/src/generator/rasn/utils.rs @@ -1105,7 +1105,7 @@ impl Rasn { while let Some(c) = peekable.next() { if c.is_lowercase() || c == '_' || c.is_numeric() { lowercase.push(c); - if peekable.peek().map_or(false, |next| next.is_uppercase()) { + if c != '_' && peekable.peek().map_or(false, |next| next.is_uppercase()) { lowercase.push('_'); } } else { @@ -1140,10 +1140,7 @@ impl Rasn { let input = input.drain(..).fold(String::new(), |mut acc, c| { if acc.is_empty() && c.is_lowercase() { acc.push(c.to_ascii_uppercase()); - } else if acc.ends_with(|last: char| last == '_') && c.is_uppercase() { - acc.pop(); - acc.push(c); - } else if acc.ends_with(|last: char| last == '_') { + } else if acc.ends_with('_') { acc.pop(); acc.push(c.to_ascii_uppercase()); } else { @@ -1499,7 +1496,7 @@ is_recursive: false, assert_eq!(generator.to_rust_snake_case("hello-world"), "hello_world"); assert_eq!(generator.to_rust_snake_case("HELLOWORLD"), "helloworld"); assert_eq!(generator.to_rust_snake_case("HelloWORLD"), "hello_world"); - assert_eq!(generator.to_rust_snake_case("HELLO-WORLD"), "hello__world"); + assert_eq!(generator.to_rust_snake_case("HELLO-WORLD"), "hello_world"); assert_eq!(generator.to_rust_snake_case("struct"), "r_struct"); assert_eq!(generator.to_rust_snake_case("STRUCT"), "r_struct"); }