technocreatives / dbc-codegen

Generate Rust structs for messages from a dbc (CAN bus definition) file.
Apache License 2.0
43 stars 21 forks source link

Switching to proc macros for code generation? #70

Open projectgus opened 1 month ago

projectgus commented 1 month ago

I suspect you folks have thought about this, so I'm curious what your take is on using a proc macro mechanism such as quote to generate the output code instead of writeln!?

If you think such a thing could potentially work, would you be amenable to merging a PR that switched over (without changing the generated code semantics), if someone was to put one together(*)?

(*) EDIT: With the obvious caveat that it would have to not add heinous amounts of complexity!

killercup commented 1 month ago

What advantages do you see for this?

I remember the main reason I didn't go with anything more complex like quote initially was that this was supposed to be a quick one-off codegen thing. I've used quote and friends before a bunch for proc macros but I didn't see the value of doing it here. This is a separate build step, we don't even parse the input file ourselves so we don't benefit from tracking spans or anything like that.

projectgus commented 1 month ago

That's a very reasonable question.

Mostly around readability of the generation code in lib.rs. Reading back the code generation stages it can be a little hard to find which piece of code generates a particular line of output, because it all kind of "flattens" into sequences of writenln!s. What I ended up doing was to open a generated messages.rs side by side and find a particular line that way.

That said, I've never used quote before so I've almost certainly got blind spots around the extra complexity of switching to it.

If keeping it this way fits with the approach & values you intend for the project then I don't think there's any reason to change, really. :)

rdelfin commented 1 month ago

Another upside of using proc macros is you can provide the macros as a library and let people implement their own custom structs if they can't (or don't want to) use the code generation