Template engine for source generation

Hello there,

I have a question if someone can maybe point me to some rust template engine(s) that can be used specifically for code generation (so essentially text output).

Preferably something that can be fed with e.g. structs, do conditionals like if/else/match, for-loops/iterations and alike over data in a type-safe way.

An example what I am looking for (the example here is for java) would be something like: https://github.com/fizzed/rocker/blob/master/docs/SYNTAX.md. The template language itself is very close to the normal java syntax, and works type-safe with java objects.

I don't mind whether the input for it is in separate files or embedded in some macros. As long as it's easy and simple to use and type-safe I would be happy. :slight_smile:

Thanks!

Are you looking to generate Rust code? If so, quote may be useful to you.

In rust-analyzer, we started with using tera (which is just a template language for text) but then switched to quote! + rustfmt. It works well enough for our moderate needs. See

https://github.com/rust-analyzer/rust-analyzer/blob/104fad65daaa6ab103ba8815244afa8243421594/xtask/src/codegen/gen_syntax.rs#L1

for our implementation.

Hey there,

Thank you both for your reply. Both seem like good suggestions, so I will have to try and see how they'd best work for me.

Cheers!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.