capnproto-rust icon indicating copy to clipboard operation
capnproto-rust copied to clipboard

Distinguishing FooBar and fooBar when converting to snake case module name

Open daniestevez opened this issue 1 year ago • 2 comments

I have a small patch for the module_name function in capnpc that inserts a leading _ in the module name when the camel case name begins by a lower case letter. This is intended to prevent name clashes between the module generated for something like a struct FooBar and the module generated from something like annotation fooBar (these cases occur in the capnp codebase I am using). Would this patch be something that could be contributed upstream, perhaps as an option rather than as default behaviour? I'm willing to do all the heavy lifting to submit a PR that is mergeable. I'm mostly asking for direction about what would be the preferred approach.

daniestevez avatar Feb 07 '25 08:02 daniestevez

What if we updated the name annotation in rust.capnp so that it could be used to annotate annotations? https://github.com/capnproto/capnproto-rust/blob/ffcb213b6223d0d25ee5f622c9842b3c5a1f78ba/capnpc/rust.capnp#L9-L15

Would that work for you?

dwrensha avatar Feb 08 '25 00:02 dwrensha

That might work but it does not seem a great solution for my use case, mainly because of two reasons:

  • The capnp code base is rather large and I would have to go through it manually adding these annotations where needed. We also have C++ and Python tools making use of these capnp files, and I would need to check that this introduces no conflicts. For simplicity, I would rather implement a solution that doesn't involve modifying the capnp files.

  • According to the comment in the code you linked, the name that is given is the capnp name, rather than the Rust name, so this does not seem to allow me to rename an annotation fooBar into _foo_bar. I guess I could rename the annotation fooBar into something like fooBarAnnotation, so that its Rust module ends up being foo_bar_annotation instead of foo_bar, which would clash with struct FooBar. However, the problem is that I also have Rust code generated in build.rs that needs to be able to predict the names of the Rust types and modules generated by capnpc based on the names in the capnp files. Allowing renames introduces an extra layer of complexity.

daniestevez avatar Feb 08 '25 09:02 daniestevez