This repository was archived by the owner on Mar 1, 2023. It is now read-only.
forked from sfackler/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtosql.rs
150 lines (129 loc) · 4.65 KB
/
tosql.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use proc_macro2::TokenStream;
use quote::quote;
use std::iter;
use syn::{Data, DataStruct, DeriveInput, Error, Fields, Ident};
use crate::accepts;
use crate::composites::Field;
use crate::enums::Variant;
use crate::overrides::Overrides;
pub fn expand_derive_tosql(input: DeriveInput) -> Result<TokenStream, Error> {
let overrides = Overrides::extract(&input.attrs)?;
let name = overrides.name.unwrap_or_else(|| input.ident.to_string());
let (accepts_body, to_sql_body) = match input.data {
Data::Enum(ref data) => {
let variants = data
.variants
.iter()
.map(Variant::parse)
.collect::<Result<Vec<_>, _>>()?;
(
accepts::enum_body(&name, &variants),
enum_body(&input.ident, &variants),
)
}
Data::Struct(DataStruct {
fields: Fields::Unnamed(ref fields),
..
}) if fields.unnamed.len() == 1 => {
let field = fields.unnamed.first().unwrap();
(accepts::domain_body(&name, &field), domain_body())
}
Data::Struct(DataStruct {
fields: Fields::Named(ref fields),
..
}) => {
let fields = fields
.named
.iter()
.map(Field::parse)
.collect::<Result<Vec<_>, _>>()?;
(
accepts::composite_body(&name, "ToSql", &fields),
composite_body(&fields),
)
}
_ => {
return Err(Error::new_spanned(
input,
"#[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums",
));
}
};
let ident = &input.ident;
let out = quote! {
impl postgres_types::ToSql for #ident {
fn to_sql(&self,
_type: &postgres_types::Type,
buf: &mut postgres_types::private::BytesMut)
-> std::result::Result<postgres_types::IsNull,
std::boxed::Box<std::error::Error +
std::marker::Sync +
std::marker::Send>> {
#to_sql_body
}
fn accepts(type_: &postgres_types::Type) -> bool {
#accepts_body
}
postgres_types::to_sql_checked!();
}
};
Ok(out)
}
fn enum_body(ident: &Ident, variants: &[Variant]) -> TokenStream {
let idents = iter::repeat(ident);
let variant_idents = variants.iter().map(|v| &v.ident);
let variant_names = variants.iter().map(|v| &v.name);
quote! {
let s = match *self {
#(
#idents::#variant_idents => #variant_names,
)*
};
buf.extend_from_slice(s.as_bytes());
std::result::Result::Ok(postgres_types::IsNull::No)
}
}
fn domain_body() -> TokenStream {
quote! {
let type_ = match *_type.kind() {
postgres_types::Kind::Domain(ref type_) => type_,
_ => unreachable!(),
};
postgres_types::ToSql::to_sql(&self.0, type_, buf)
}
}
fn composite_body(fields: &[Field]) -> TokenStream {
let field_names = fields.iter().map(|f| &f.name);
let field_idents = fields.iter().map(|f| &f.ident);
quote! {
let fields = match *_type.kind() {
postgres_types::Kind::Composite(ref fields) => fields,
_ => unreachable!(),
};
buf.extend_from_slice(&(fields.len() as i32).to_be_bytes());
for field in fields {
buf.extend_from_slice(&field.type_().oid().to_be_bytes());
let base = buf.len();
buf.extend_from_slice(&[0; 4]);
let r = match field.name() {
#(
#field_names => postgres_types::ToSql::to_sql(&self.#field_idents, field.type_(), buf),
)*
_ => unreachable!(),
};
let count = match r? {
postgres_types::IsNull::Yes => -1,
postgres_types::IsNull::No => {
let len = buf.len() - base - 4;
if len > i32::max_value() as usize {
return std::result::Result::Err(
std::convert::Into::into("value too large to transmit"));
}
len as i32
}
};
buf[base..base + 4].copy_from_slice(&count.to_be_bytes());
}
std::result::Result::Ok(postgres_types::IsNull::No)
}
}