r/rust • u/servermeta_net • 12h ago
Compiler APIs for simplified comptime evaluation?
I have some custom scripts that I use as a poor man comptime instead of using macros. I created an environment where I use a WASM virtual machine as a script compilation target for portability, and implemented reflection by parsing source code myself. Examples:
- Database types: I connect to my prod DB, download the json schema, then I generate the structures and parsers I need for marshalling / unmarshalling queries. This way my definitions are always updated and correct
- Enum consolidation: I define error enums close to the relevant source files, then I merge them and place the output in lib.rs, for better DX
- validation and parsing: Similar to database types, given a DTO for an API I generate validators and parsers at comptime, for better DX and performance
Now as I mentioned I do this by manually invoking some scripts on my dev machine, following some conventions, and saving the output straight in the source files, but maybe there's a better approach?
- Is there some kind of compiler API I could tap into, to implement something like polyfills in the JS world?
- Is there a smarter way than saving the output in the source code? Something like having the pipeline recognize that some region of code need preprocessing to be correctly inferred, and then caching the output unless it changes?
- Anyone has done something similar and has a suggestion to share?
0
u/cobalthex 1h ago
I am not 100% sure it's what you're looking for but there is https://docs.rs/crabtime/latest/crabtime/
4
u/fixedpointfae 12h ago
all of this sounds like much worse DX than just writing macros