Skip to content

fix(generator): use single underscore in kebab to snake case conversion#93

Merged
6d7a merged 1 commit intolibrasn:mainfrom
DavidLeeds:leeds/no-double-underscore
Mar 7, 2025
Merged

fix(generator): use single underscore in kebab to snake case conversion#93
6d7a merged 1 commit intolibrasn:mainfrom
DavidLeeds:leeds/no-double-underscore

Conversation

@DavidLeeds
Copy link
Copy Markdown
Contributor

Addresses the case where "Kebab-Case" was converted to "kebab__case". After the change, we get "kebab_case".

issue: #88

Addresses the case where "Kebab-Case" was converted to
"kebab__case". After the change, we get "kebab_case".

issue: #88
Copy link
Copy Markdown
Member

@6d7a 6d7a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this can lead to problematic edge cases. Consider the hypothetical ASN.1 type:

TestType ::= SEQUENCE {
   doner-kebab BOOLEAN,
   doner_kebab INTEGER
}

If we do not have a name transformation that retains distinction between - and _, the bindings for the type above will not compile. However, given that this is such an outrageously exotic edge case, I'd be up for introducing a config parameter to the Rasn backend, so that the user can decide how to handle name transformation.

@DavidLeeds
Copy link
Copy Markdown
Contributor Author

DavidLeeds commented Mar 7, 2025

@6d7a this is a valid concern, but I noted that the snake case conversion already makes no guarantees that the output will be unique for similar inputs. The following four existing test cases output the same result:

assert_eq!(generator.to_rust_snake_case("HelloWorld"), "hello_world");
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("HelloWORLD"), "hello_world");

My change simply makes the outlier case consistent with the established behavior.

If schemas are using minor whitespace and punctuation changes to differentiate definitions, perhaps the code generator will be the forcing function to drive more responsible naming decisions.

Copy link
Copy Markdown
Member

@6d7a 6d7a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, a valid fix indeed. Thank you and sorry for the confusion.

@6d7a 6d7a merged commit 4c4c019 into librasn:main Mar 7, 2025
1 check passed
@DavidLeeds DavidLeeds deleted the leeds/no-double-underscore branch March 8, 2025 02:35
jp-marcotte pushed a commit to jp-marcotte/compiler that referenced this pull request Feb 10, 2026
Extends to_rust_snake_case() to detect acronym-end boundaries when an
uppercase letter is followed by a lowercase letter and preceded by
another uppercase letter. This correctly handles acronym-prefixed names:

  PEDefinitions  -> pe_definitions  (was: pedefinitions)
  SGP32Definitions -> sgp32_definitions (was: sgp32definitions)
  RSPDefinitions -> rsp_definitions (was: rspdefinitions)
  EUICCInfo1     -> euicc_info1     (was: euiccinfo1)

Extends the fix from PR librasn#93 (librasn#88) which handled
double-underscores in kebab-case names but missed acronym boundaries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants