I'm in need of getting builders for a lot of structs from an external crate. I know I could do myself by hand (not using external libs like derive_builder and typed_builder), however, since there are many many structs, I'd like to write a macro to do that work for me.
I'm not too confident that is possible, as far as I know, macros operate on token streams (or some form of pattern matching with macro_rules!), so they have limited semantic information?
Ideally, I'd love to be able to do something along the lines of:
builder!(SomeStruct)
// or even better, although certainly impossible x)
builder!(some_crate)
Macros operate on a token stream and can only access to whatever you pass them. builder(SomeStruct) gets SomeStruct as input and nothing else. remote-derivie gets passed a type implementation that must match remote type and solves a slightly different problem.
Yeah, that's what I thought. But I wonder, how does remote derive assert the local type matches the remote one? It only gets a string as well (#[serde(remote = "Type")]).
it simply uses the field names it gets from the DurationDef struct, and uses them to generate code pointing to the type specified in the remote = "Duration" part. If you give fields in DurationDef that don't exist in Duration, it fails to compile simply because the fields don't exist, and the generated code is invalid because it's trying to reference fields that don't exist. There's no magic assertions going on.
Ahh I see, that makes sense. It's just spitting code using the local field names in the hopes it matches the remote type.
So yeah, there's no way to achieve what I was thinking of, quite a shame
Adding builders to the external crate itself is not really an option, so what would you suggest doing? Writing the builders by hand (or writing something to generate the code for me; build script maybe?)