Debian Codegraph @

src/tosql.rs

raw symbols SCIP
1 use proc_macro2rust-analyzer cargo proc-macro2 1.0.106 crate/::{Spanrust-analyzer cargo proc-macro2 1.0.106 Span#, TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#};
2 use quoterust-analyzer cargo quote 1.0.45 crate/::quoterust-analyzer cargo quote 1.0.45 quote!;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/;
4 use synrust-analyzer cargo syn 2.0.117 crate/::{
5 Datarust-analyzer cargo syn 2.0.117 derive/Data#, DataStructrust-analyzer cargo syn 2.0.117 derive/DataStruct#, DeriveInputrust-analyzer cargo syn 2.0.117 derive/DeriveInput#, Errorrust-analyzer cargo syn 2.0.117 error/Error#, Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#, Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, TraitBoundrust-analyzer cargo syn 2.0.117 generics/TraitBound#, TraitBoundModifierrust-analyzer cargo syn 2.0.117 generics/TraitBoundModifier#,
6 TypeParamBoundrust-analyzer cargo syn 2.0.117 generics/TypeParamBound#,
7 };
8
9 use craterust-analyzer cargo postgres-derive 0.4.9 crate/::acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/;
10 use craterust-analyzer cargo postgres-derive 0.4.9 crate/::compositesrust-analyzer cargo postgres-derive 0.4.9 composites/::Fieldrust-analyzer cargo postgres-derive 0.4.9 composites/Field#;
11 use craterust-analyzer cargo postgres-derive 0.4.9 crate/::compositesrust-analyzer cargo postgres-derive 0.4.9 composites/::{append_generic_boundrust-analyzer cargo postgres-derive 0.4.9 composites/append_generic_bound()., new_derive_pathrust-analyzer cargo postgres-derive 0.4.9 composites/new_derive_path().};
12 use craterust-analyzer cargo postgres-derive 0.4.9 crate/::enumsrust-analyzer cargo postgres-derive 0.4.9 enums/::Variantrust-analyzer cargo postgres-derive 0.4.9 enums/Variant#;
13 use craterust-analyzer cargo postgres-derive 0.4.9 crate/::overridesrust-analyzer cargo postgres-derive 0.4.9 overrides/::Overridesrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#;
14
15 pub fn expand_derive_tosqlrust-analyzer cargo postgres-derive 0.4.9 tosql/expand_derive_tosql().(input: DeriveInputrust-analyzer cargo syn 2.0.117 derive/DeriveInput#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#, Errorrust-analyzer cargo syn 2.0.117 error/Error#> {
16 let overrides = Overridesrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#::extractrust-analyzer cargo postgres-derive 0.4.9 overrides/impl#[Overrides]extract().(&input.attrsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#attrs., true)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
17
18 if (overrides.namerust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#name..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() || overrides.rename_allrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#rename_all..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()) && overrides.transparentrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#transparent. {
19 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
20 &input,
21 "#[postgres(transparent)] is not allowed with #[postgres(name = \"...\")] or #[postgres(rename_all = \"...\")]",
22 ));
23 }
24
25 let name = overrides
26 .namerust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#name.
27 .clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().()
28 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
29
30 let (accepts_body, to_sql_body) = if overrides.transparentrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#transparent. {
31 match input.datarust-analyzer cargo syn 2.0.117 derive/DeriveInput#data. {
32 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Structrust-analyzer cargo syn 2.0.117 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.117 derive/DataStruct# {
33 fieldsrust-analyzer cargo syn 2.0.117 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(ref fields),
34 ..
35 }) if fields.unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed..lenrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 => {
36 let field = fields.unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed..firstrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]first().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
37
38 (acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/::transparent_bodyrust-analyzer cargo postgres-derive 0.4.9 accepts/transparent_body().(field), transparent_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/transparent_body().())
39 }
40 _ => {
41 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
42 input,
43 "#[postgres(transparent)] may only be applied to single field tuple structs",
44 ));
45 }
46 }
47 } else if overrides.allow_mismatchrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#allow_mismatch. {
48 match input.datarust-analyzer cargo syn 2.0.117 derive/DeriveInput#data. {
49 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Enumrust-analyzer cargo syn 2.0.117 derive/Data#Enum#(ref data) => {
50 let variants = data
51 .variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants.
52 .iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().()
53 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|variant| Variantrust-analyzer cargo postgres-derive 0.4.9 enums/Variant#::parserust-analyzer cargo postgres-derive 0.4.9 enums/impl#[Variant]parse().(variant, overrides.rename_allrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#rename_all.))
54 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
55 (
56 acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/::enum_bodyrust-analyzer cargo postgres-derive 0.4.9 accepts/enum_body().(&name, &variants, overrides.allow_mismatchrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#allow_mismatch.),
57 enum_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/enum_body().(&input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident., &variants),
58 )
59 }
60 _ => {
61 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
62 input,
63 "#[postgres(allow_mismatch)] may only be applied to enums",
64 ));
65 }
66 }
67 } else {
68 match input.datarust-analyzer cargo syn 2.0.117 derive/DeriveInput#data. {
69 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Enumrust-analyzer cargo syn 2.0.117 derive/Data#Enum#(ref data) => {
70 let variants = data
71 .variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants.
72 .iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().()
73 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|variant| Variantrust-analyzer cargo postgres-derive 0.4.9 enums/Variant#::parserust-analyzer cargo postgres-derive 0.4.9 enums/impl#[Variant]parse().(variant, overrides.rename_allrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#rename_all.))
74 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
75 (
76 acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/::enum_bodyrust-analyzer cargo postgres-derive 0.4.9 accepts/enum_body().(&name, &variants, overrides.allow_mismatchrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#allow_mismatch.),
77 enum_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/enum_body().(&input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident., &variants),
78 )
79 }
80 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Structrust-analyzer cargo syn 2.0.117 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.117 derive/DataStruct# {
81 fieldsrust-analyzer cargo syn 2.0.117 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(ref fields),
82 ..
83 }) if fields.unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed..lenrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 => {
84 let field = fields.unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed..firstrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]first().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
85
86 (acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/::domain_bodyrust-analyzer cargo postgres-derive 0.4.9 accepts/domain_body().(&name, field), domain_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/domain_body().())
87 }
88 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Structrust-analyzer cargo syn 2.0.117 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.117 derive/DataStruct# {
89 fieldsrust-analyzer cargo syn 2.0.117 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(ref fields),
90 ..
91 }) => {
92 let fields = fields
93 .namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named.
94 .iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().()
95 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| Fieldrust-analyzer cargo postgres-derive 0.4.9 composites/Field#::parserust-analyzer cargo postgres-derive 0.4.9 composites/impl#[Field]parse().(field, overrides.rename_allrust-analyzer cargo postgres-derive 0.4.9 overrides/Overrides#rename_all.))
96 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
97 (
98 acceptsrust-analyzer cargo postgres-derive 0.4.9 accepts/::composite_bodyrust-analyzer cargo postgres-derive 0.4.9 accepts/composite_body().(&name, "ToSql", &fields),
99 composite_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/composite_body().(&fields),
100 )
101 }
102 _ => {
103 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
104 input,
105 "#[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums",
106 ));
107 }
108 }
109 };
110
111 let ident = &input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident.;
112 let generics = append_generic_boundrust-analyzer cargo postgres-derive 0.4.9 composites/append_generic_bound().(input.genericsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#generics..to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[T][ToOwned]to_owned().(), &new_tosql_boundrust-analyzer cargo postgres-derive 0.4.9 tosql/new_tosql_bound().());
113 let (impl_generics, ty_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.117 generics/impl#[Generics]split_for_impl().();
114 let out = quoterust-analyzer cargo quote 1.0.45 quote!! {
115 impl #impl_generics postgres_types::ToSql for #ident #ty_generics #where_clause {
116 fn to_sql(&self,
117 _type: &postgres_types::Type,
118 buf: &mut postgres_types::private::BytesMut)
119 -> std::result::Result<postgres_types::IsNull,
120 std::boxed::Box<dyn std::error::Error +
121 std::marker::Sync +
122 std::marker::Send>> {
123 #to_sql_body
124 }
125
126 fn accepts(type_: &postgres_types::Type) -> bool {
127 #accepts_body
128 }
129
130 postgres_types::to_sql_checked!();
131 }
132 };
133
134 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(out)
135 }
136
137 fn transparent_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/transparent_body().() -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# {
138 quoterust-analyzer cargo quote 1.0.45 quote!! {
139 postgres_types::ToSql::to_sql(&self.0, _type, buf)
140 }
141 }
142
143 fn enum_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/enum_body().(ident: &Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, variants: &[Variantrust-analyzer cargo postgres-derive 0.4.9 enums/Variant#]) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# {
144 let idents = iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat/repeat().(ident);
145 let variant_idents = variants.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.identrust-analyzer cargo postgres-derive 0.4.9 enums/Variant#ident.);
146 let variant_names = variants.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.namerust-analyzer cargo postgres-derive 0.4.9 enums/Variant#name.);
147
148 quoterust-analyzer cargo quote 1.0.45 quote!! {
149 let s = match *self {
150 #(
151 #idents::#variant_idents => #variant_names,
152 )*
153 };
154
155 buf.extend_from_slice(s.as_bytes());
156 std::result::Result::Ok(postgres_types::IsNull::No)
157 }
158 }
159
160 fn domain_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/domain_body().() -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# {
161 quoterust-analyzer cargo quote 1.0.45 quote!! {
162 let type_ = match *_type.kind() {
163 postgres_types::Kind::Domain(ref type_) => type_,
164 _ => unreachable!(),
165 };
166
167 postgres_types::ToSql::to_sql(&self.0, type_, buf)
168 }
169 }
170
171 fn composite_bodyrust-analyzer cargo postgres-derive 0.4.9 tosql/composite_body().(fields: &[Fieldrust-analyzer cargo postgres-derive 0.4.9 composites/Field#]) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# {
172 let field_names = fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.namerust-analyzer cargo postgres-derive 0.4.9 composites/Field#name.);
173 let field_idents = fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.identrust-analyzer cargo postgres-derive 0.4.9 composites/Field#ident.);
174
175 quoterust-analyzer cargo quote 1.0.45 quote!! {
176 let fields = match *_type.kind() {
177 postgres_types::Kind::Composite(ref fields) => fields,
178 _ => unreachable!(),
179 };
180
181 buf.extend_from_slice(&(fields.len() as i32).to_be_bytes());
182
183 for field in fields {
184 buf.extend_from_slice(&field.type_().oid().to_be_bytes());
185
186 let base = buf.len();
187 buf.extend_from_slice(&[0; 4]);
188 let r = match field.name() {
189 #(
190 #field_names => postgres_types::ToSql::to_sql(&self.#field_idents, field.type_(), buf),
191 )*
192 _ => unreachable!(),
193 };
194
195 let count = match r? {
196 postgres_types::IsNull::Yes => -1,
197 postgres_types::IsNull::No => {
198 let len = buf.len() - base - 4;
199 if len > i32::MAX as usize {
200 return std::result::Result::Err(
201 std::convert::Into::into("value too large to transmit"));
202 }
203 len as i32
204 }
205 };
206
207 buf[base..base + 4].copy_from_slice(&count.to_be_bytes());
208 }
209
210 std::result::Result::Ok(postgres_types::IsNull::No)
211 }
212 }
213
214 fn new_tosql_boundrust-analyzer cargo postgres-derive 0.4.9 tosql/new_tosql_bound().() -> TypeParamBoundrust-analyzer cargo syn 2.0.117 generics/TypeParamBound# {
215 TypeParamBoundrust-analyzer cargo syn 2.0.117 generics/TypeParamBound#::Traitrust-analyzer cargo syn 2.0.117 generics/TypeParamBound#Trait#(TraitBoundrust-analyzer cargo syn 2.0.117 generics/TraitBound# {
216 lifetimesrust-analyzer cargo syn 2.0.117 generics/TraitBound#lifetimes.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
217 modifierrust-analyzer cargo syn 2.0.117 generics/TraitBound#modifier.: TraitBoundModifierrust-analyzer cargo syn 2.0.117 generics/TraitBoundModifier#::Nonerust-analyzer cargo syn 2.0.117 generics/TraitBoundModifier#None#,
218 paren_tokenrust-analyzer cargo syn 2.0.117 generics/TraitBound#paren_token.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
219 pathrust-analyzer cargo syn 2.0.117 generics/TraitBound#path.: new_derive_pathrust-analyzer cargo postgres-derive 0.4.9 composites/new_derive_path().(Identrust-analyzer cargo proc-macro2 1.0.106 Ident#::newrust-analyzer cargo proc-macro2 1.0.106 impl#[Ident]new().("ToSql", Spanrust-analyzer cargo proc-macro2 1.0.106 Span#::call_siterust-analyzer cargo proc-macro2 1.0.106 impl#[Span]call_site().()).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
220 })
221 }