Closed cwerner87 closed 7 months ago
Since that mapping isn't 1-to-1 (for example Rust strings can convert to many Postgres types), you'd probably need to specify the types explicitly in your macro.
Good point - I was planning on having a default that could be overridden (e.g. the default for String might be TEXT, but the caller could override this in the struct definition to be VARCHAR). I guess what I'm asking is: is there any way to leverage existing functionality in this package or do you think that a match is the best solution?
One other option you could try is to move that mapping from a big match in your macro out into the trait system. Something like
trait DefaultPostgresType {
const TYPE: Type;
}
impl DefaultPostgresType for i32 {
const TYPE: Type = Type::INT4;
}
// etc...
And then have your macro-generated code look like <#ty as DefaultPostgresType>::TYPE
if the user didn't override it.
Excellent suggestion. That's what I'll do. Thank you!
Hello,
I'm working on a macro to generate boilerplate to annotate structs for the purpose of copying into a Postgres table via BinaryCopyInWriter.
I have some of it completed but I'm struggling a bit on constructing the array of postgres_types::Type for instantiating the writer. I had seen in https://github.com/sfackler/rust-postgres/blob/master/postgres-types/src/lib.rs the table that looks like:
And I had incorrectly concluded that there'd be some sort of a match statement that did this mapping. Now that I've been digging into it, it seems that there are macros that generate the to_sql blocks for the various Rust types.
Specifically, in my macro what I'm hoping to do is something like this:
Where the macro would then be able to generate the boilerplate for BinaryCopyInWriter and would generate the array of types of [Type::INT4, Type::TEXT] as its second argument.
Does anyone have any advice on how to go about mapping a Rust struct's types to Postgres types? My best idea at this point is to do my own matching, but perhaps there's a superior solution.