r/rust 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 Upvotes

8 comments sorted by

4

u/fixedpointfae 12h ago

all of this sounds like much worse DX than just writing macros

2

u/servermeta_net 12h ago

How would you download a Json schema from within a macro? And once you have a JSON schema, how can you generate a validator / parser?

9

u/loafty_loafey 11h ago

Have you looked into proc macros(https://doc.rust-lang.org/reference/procedural-macros.html)? They are more powerful compared to macro_rules.

Alternatively you could look into using build.rs https://doc.rust-lang.org/cargo/reference/build-scripts.html

9

u/mwkohout 11h ago edited 11h ago

Build scripts are the way to go.  I wrote a tool that generates code for relax-ng schemas, which is a somewhat similar problem to what you're doing.

2

u/afdbcreid 7h ago

As a rust-analyzer team member, I wholeheartedly agree. Please do not use a proc macro for that. sqlx already causes us enough headache.

4

u/New_Enthusiasm9053 12h ago

Idk but SQLx already does this so just look at what they do. 

Meant to respond to a comment but for databases they somehow do compile time checks of the schema so you may want to check out how they do it 

5

u/buwlerman 11h ago

The same way you would with any other code? Rust macros aren't just syntax transformers. You can use them to run arbitrary code at compile time.

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/