capnproto / capnproto-rust

Cap'n Proto for Rust
MIT License
2.06k stars 222 forks source link

c++.capnp compiles to unusable module name #478

Open quartox opened 10 months ago

quartox commented 10 months ago

I have a c++.capnp schema file and when I build my project the generated code includes references to crate::c++_capnp. Is it possible to change the name in the rust build so that the generated code uses a different name (e.g. cpp_capnp) or is there some other workaround?

dwrensha commented 10 months ago

Does #412 help?

quartox commented 10 months ago

I made an external crate called capnp-crate mimicking this code and then added crate_provides("capnp_crate", [0xabcdef01234]) to my main crate's build.rs.

When I try to compile I still get errors in the generated code referencing capnp_crate::c++_capnp and it complains about the + in the name.

dwrensha commented 10 months ago

I'm assuming the c++.capnp schema file is this one from the main capnproto repo: https://github.com/capnproto/capnproto/blob/v2/c%2B%2B/src/capnp/c%2B%2B.capnp

Is there a reason that you need to generate Rust code for it at all?

Note that the capnproto-rust addressbook example refers to c++.capnp but does not generate Rust code for it, and this works without a problem.

quartox commented 10 months ago

There is also an Enum added to that file to create a new type of annotation. The compiler fails on the enum specifically. The annotation created from that enum is used widely in the other schemas that I need to use directly.

dwrensha commented 10 months ago

Ah, and unfortunately Rust raw identifiers don't support the '+' character either.

Some options:

  1. We could allow the Rust.name annotation to change the name of files: https://github.com/capnproto/capnproto-rust/blob/b85e041a1852cd4b3a7d7860ef4fbe16369c73e9/capnpc/rust.capnp#L9 Then you could add a $Rust.name("cpp"); annotation in your c++.capnp file and the name would be avoided. This is nice in that it would use an existing mechanism, but maybe not nice in that it requires c++.capnp to import rust.capnp.
  2. We could come up with an escaping scheme, so that the "+" deterministically gets mapped to something else in Rust code.
  3. We could add an option to capnpc::CompilerCommand so that you could rename files in your build.rs.
  4. You could rename c++.capnp to cpp.capnp in your project.
quartox commented 9 months ago

I am talking to the team that owns the schema files to see if we can change the name. If we are the only ones running into this and the change doesn't cause big problems (which is a risk) then option 4 seems like the simplest solution.