(The question is not really specific to Rust. Asking here since a quite a few FMs here seem to be familiar with PL programming and, anyways, the tooling/platform used in this case is Rust.)
I am attempting to write a small DSL which emits (compiles to?) SQL DDL statements. I am using pest-parser to generate the parser. This parser is used to transform code in the DSL into Rust data-structures (structs). The data in these structs is then used to emit the target code using askama templates which are pre-defined.
The approach is working fine but it is getting tedious with lots of if, else branching and looping as I extend the language with more capabilities.
What are ways in which I can better handle code-generation keeping maintainability and ability to extend in perspective?
It will be great if you can elaborate? Just to be clear: neither the source nor the target language is Rust. Rust is the "glue language" in that it reads the source language and transforms it to the target language.
Thanks! This is almost what I am doing as of now. The main difference being the authors of sqlparse have used the write! macro whereas I am using a full blown string template library.
And not surprisingly, the authors there have similar problem as noted in the comment at the top of the function linked in your post.
// Clippy thinks this function is too complicated, but it is painful to
// split up without extracting structs for each Statement variant.
Nevertheless, will investigate further into transforming my structs into their AST.
@17cupsofcoffee Thanks! It turned out be a solid suggestion. I just need to transform between trees. This is cleaner and way less prone to bugs than custom conditionals or templates.
The flow now looks like:
DSL is parsed into a local AST used for semantic validation
Then all that is needed is format! to generate the query
A shout-out to the people (not sure if they hangout here) behind the library sqlparser as well. It's pretty neat and structured in a way that enables such a use case very elegantly even though it's not the main goal of the library.