instant-labs / instant-xml

11 stars 3 forks source link

Codegen from XSD #57

Open nrempel opened 3 months ago

nrempel commented 3 months ago

It would be convenient to be able to generate Rust structs with the relevant instant-xml decorations from xsd schema definitions. Do you think this could be considered in scope for this repo?

djc commented 3 months ago

Yes, I think this would be a great complement! Was thinking about this myself at some point. Probably in a separate crate in the workspace?

djc commented 3 months ago

I wonder if the mapping of the macros is sufficient already to be able to express XSD itself in it...

nrempel commented 3 months ago

It's worth a shot! I'll think about it.

nrempel commented 3 months ago

Do you have any suggestions for crates to look at for managing the codegen step?

djc commented 3 months ago

Not sure what you mean? Look at the gcp stuff in bolt?

djc commented 3 months ago

Thinking about this more, if you mean how to best generate Rust code, there are basically two viable approaches:

For the latter, you can try to generate somewhat formatted code (which takes a little bit more work but isn't that hard -- Askama does this), or you can just run the code through rustfmt (or the prettyplease crate) for formatting.

nrempel commented 3 months ago

Thanks! Yeah I experimented a bit with quote which works well.

I'm generating roughly the right shape but mapping xml types to Rust is a bit tricky since all xml schemas seem to define their own types which inherit other types. I need to recursively trace the inheritance back to the base type--most likely across multiple schema definitions!

djc commented 3 months ago

Ah yeah, that does sound tricky! Feel free to open a draft PR if you'd like a second pair of eyes.