-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
mod.rs
620 lines (571 loc) · 25.7 KB
/
mod.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
/*!
`#[pg_extern]` related macro expansion for Rust to SQL translation
> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal**
to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution.
*/
mod argument;
mod attribute;
mod cast;
pub mod entity;
mod operator;
mod returning;
mod search_path;
pub use argument::PgExternArgument;
pub use cast::PgCast;
pub use operator::PgOperator;
pub use returning::NameMacro;
use crate::fmt::ErrHarder;
use crate::ToSqlConfig;
pub(crate) use attribute::Attribute;
use operator::{PgrxOperatorAttributeWithIdent, PgrxOperatorOpName};
use search_path::SearchPathList;
use crate::enrich::CodeEnrichment;
use crate::enrich::ToEntityGraphTokens;
use crate::enrich::ToRustCodeTokens;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::{Parse, ParseStream, Parser};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Meta, Token};
use self::returning::Returning;
use super::UsedType;
/// A parsed `#[pg_extern]` item.
///
/// It should be used with [`syn::parse::Parse`] functions.
///
/// Using [`quote::ToTokens`] will output the declaration for a [`PgExternEntity`][crate::PgExternEntity].
///
/// ```rust
/// use syn::{Macro, parse::Parse, parse_quote, parse};
/// use quote::{quote, ToTokens};
/// use pgrx_sql_entity_graph::PgExtern;
///
/// # fn main() -> eyre::Result<()> {
/// use pgrx_sql_entity_graph::CodeEnrichment;
/// let parsed: CodeEnrichment<PgExtern> = parse_quote! {
/// fn example(x: Option<str>) -> Option<&'a str> {
/// unimplemented!()
/// }
/// };
/// let sql_graph_entity_tokens = parsed.to_token_stream();
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct PgExtern {
attrs: Vec<Attribute>,
func: syn::ItemFn,
to_sql_config: ToSqlConfig,
operator: Option<PgOperator>,
cast: Option<PgCast>,
search_path: Option<SearchPathList>,
inputs: Vec<PgExternArgument>,
input_types: Vec<syn::Type>,
returns: Returning,
}
impl PgExtern {
#[track_caller]
pub fn new(attr: TokenStream2, item: TokenStream2) -> Result<CodeEnrichment<Self>, syn::Error> {
let mut attrs = Vec::new();
let mut to_sql_config: Option<ToSqlConfig> = None;
let parser = Punctuated::<Attribute, Token![,]>::parse_terminated;
let attr_ts = attr.clone();
let punctuated_attrs = parser
.parse2(attr)
.more_error(lazy_err!(attr_ts, "failed parsing pg_extern arguments"))?;
for pair in punctuated_attrs.into_pairs() {
match pair.into_value() {
Attribute::Sql(config) => to_sql_config = to_sql_config.or(Some(config)),
attr => attrs.push(attr),
}
}
let mut to_sql_config = to_sql_config.unwrap_or_default();
let func = syn::parse2::<syn::ItemFn>(item)?;
if let Some(ref mut content) = to_sql_config.content {
let value = content.value();
let updated_value =
value.replace("@FUNCTION_NAME@", &(func.sig.ident.to_string() + "_wrapper")) + "\n";
*content = syn::LitStr::new(&updated_value, Span::call_site());
}
if !to_sql_config.overrides_default() {
crate::ident_is_acceptable_to_postgres(&func.sig.ident)?;
}
let operator = Self::operator(&func)?;
let search_path = Self::search_path(&func)?;
let inputs = Self::inputs(&func)?;
let input_types = Self::input_types(&func)?;
let returns = Returning::try_from(&func.sig.output)?;
Ok(CodeEnrichment(Self {
attrs,
func,
to_sql_config,
operator,
cast: None,
search_path,
inputs,
input_types,
returns,
}))
}
/// Returns a new instance of this `PgExtern` with `cast` overwritten to `pg_cast`.
pub fn as_cast(&self, pg_cast: PgCast) -> PgExtern {
let mut result = self.clone();
result.cast = Some(pg_cast);
result
}
#[track_caller]
fn input_types(func: &syn::ItemFn) -> syn::Result<Vec<syn::Type>> {
func.sig
.inputs
.iter()
.filter_map(|v| -> Option<syn::Result<syn::Type>> {
match v {
syn::FnArg::Receiver(_) => None,
syn::FnArg::Typed(pat_ty) => {
let ty = match UsedType::new(*pat_ty.ty.clone()) {
Ok(v) => v.resolved_ty,
Err(e) => return Some(Err(e)),
};
Some(Ok(ty))
}
}
})
.collect()
}
fn name(&self) -> String {
self.attrs
.iter()
.find_map(|a| match a {
Attribute::Name(name) => Some(name.value()),
_ => None,
})
.unwrap_or_else(|| self.func.sig.ident.to_string())
}
fn schema(&self) -> Option<String> {
self.attrs.iter().find_map(|a| match a {
Attribute::Schema(name) => Some(name.value()),
_ => None,
})
}
pub fn extern_attrs(&self) -> &[Attribute] {
self.attrs.as_slice()
}
#[track_caller]
fn overridden(&self) -> Option<syn::LitStr> {
let mut span = None;
let mut retval = None;
let mut in_commented_sql_block = false;
for meta in self.func.attrs.iter().filter_map(|attr| {
if attr.meta.path().is_ident("doc") {
Some(attr.meta.clone())
} else {
None
}
}) {
let Meta::NameValue(syn::MetaNameValue { ref value, .. }) = meta else { continue };
let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(inner), .. }) = value else {
continue;
};
span.get_or_insert(value.span());
if !in_commented_sql_block && inner.value().trim() == "```pgrxsql" {
in_commented_sql_block = true;
} else if in_commented_sql_block && inner.value().trim() == "```" {
in_commented_sql_block = false;
} else if in_commented_sql_block {
let line = inner
.value()
.trim_start()
.replace("@FUNCTION_NAME@", &(self.func.sig.ident.to_string() + "_wrapper"))
+ "\n";
retval.get_or_insert_with(String::default).push_str(&line);
}
}
retval.map(|s| syn::LitStr::new(s.as_ref(), span.unwrap()))
}
#[track_caller]
fn operator(func: &syn::ItemFn) -> syn::Result<Option<PgOperator>> {
let mut skel = Option::<PgOperator>::default();
for attr in &func.attrs {
let last_segment = attr.path().segments.last().unwrap();
match last_segment.ident.to_string().as_str() {
s @ "opname" => {
// we've been accepting strings of tokens for a while now
let attr = attr
.parse_args::<PgrxOperatorOpName>()
.more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
skel.get_or_insert_with(Default::default).opname.get_or_insert(attr);
}
s @ "commutator" => {
let attr: PgrxOperatorAttributeWithIdent = attr
.parse_args()
.more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
skel.get_or_insert_with(Default::default).commutator.get_or_insert(attr);
}
s @ "negator" => {
let attr: PgrxOperatorAttributeWithIdent = attr
.parse_args()
.more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
skel.get_or_insert_with(Default::default).negator.get_or_insert(attr);
}
s @ "join" => {
let attr: PgrxOperatorAttributeWithIdent = attr
.parse_args()
.more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
skel.get_or_insert_with(Default::default).join.get_or_insert(attr);
}
s @ "restrict" => {
let attr: PgrxOperatorAttributeWithIdent = attr
.parse_args()
.more_error(lazy_err!(attr.clone(), "bad parse of {s}?"))?;
skel.get_or_insert_with(Default::default).restrict.get_or_insert(attr);
}
"hashes" => {
skel.get_or_insert_with(Default::default).hashes = true;
}
"merges" => {
skel.get_or_insert_with(Default::default).merges = true;
}
_ => (),
}
}
Ok(skel)
}
fn search_path(func: &syn::ItemFn) -> syn::Result<Option<SearchPathList>> {
func.attrs
.iter()
.find(|f| {
f.path()
.segments
.first()
.map(|f| f.ident == Ident::new("search_path", Span::call_site()))
.unwrap_or_default()
})
.map(|attr| attr.parse_args::<SearchPathList>())
.transpose()
}
fn inputs(func: &syn::ItemFn) -> syn::Result<Vec<PgExternArgument>> {
let mut args = Vec::default();
for input in &func.sig.inputs {
let arg = PgExternArgument::build(input.clone())?;
args.push(arg);
}
Ok(args)
}
fn entity_tokens(&self) -> TokenStream2 {
let ident = &self.func.sig.ident;
let name = self.name();
let unsafety = &self.func.sig.unsafety;
let schema = self.schema();
let schema_iter = schema.iter();
let extern_attrs = self
.attrs
.iter()
.map(|attr| attr.to_sql_entity_graph_tokens())
.collect::<Punctuated<_, Token![,]>>();
let search_path = self.search_path.clone().into_iter();
let inputs = &self.inputs;
let inputs_iter = inputs.iter().map(|v| v.entity_tokens());
let input_types = self.input_types.iter().cloned();
let returns = &self.returns;
let return_type = match &self.func.sig.output {
syn::ReturnType::Default => None,
syn::ReturnType::Type(arrow, ty) => {
let ty = ty.clone();
Some(syn::ReturnType::Type(*arrow, ty))
}
};
let operator = self.operator.clone().into_iter();
let cast = self.cast.clone().into_iter();
let to_sql_config = match self.overridden() {
None => self.to_sql_config.clone(),
Some(content) => ToSqlConfig { content: Some(content), ..self.to_sql_config.clone() },
};
let lifetimes = self
.func
.sig
.generics
.params
.iter()
.filter_map(|generic| match generic {
// syn::GenericParam::Type(_) => todo!(),
syn::GenericParam::Lifetime(lt) => Some(lt),
_ => None, // syn::GenericParam::Const(_) => todo!(),
})
.collect::<Vec<_>>();
let hrtb = if lifetimes.is_empty() { None } else { Some(quote! { for<#(#lifetimes),*> }) };
let sql_graph_entity_fn_name =
syn::Ident::new(&format!("__pgrx_internals_fn_{}", ident), Span::call_site());
quote_spanned! { self.func.sig.span() =>
#[no_mangle]
#[doc(hidden)]
#[allow(unknown_lints, clippy::no_mangle_with_rust_abi)]
pub extern "Rust" fn #sql_graph_entity_fn_name() -> ::pgrx::pgrx_sql_entity_graph::SqlGraphEntity {
extern crate alloc;
#[allow(unused_imports)]
use alloc::{vec, vec::Vec};
type FunctionPointer = #hrtb #unsafety fn(#( #input_types ),*) #return_type;
let submission = ::pgrx::pgrx_sql_entity_graph::PgExternEntity {
name: #name,
unaliased_name: stringify!(#ident),
module_path: core::module_path!(),
full_path: concat!(core::module_path!(), "::", stringify!(#ident)),
metadata: <FunctionPointer as ::pgrx::pgrx_sql_entity_graph::metadata::FunctionMetadata::<_>>::entity(),
fn_args: vec![#(#inputs_iter),*],
fn_return: #returns,
#[allow(clippy::or_fun_call)]
schema: None #( .unwrap_or_else(|| Some(#schema_iter)) )*,
file: file!(),
line: line!(),
extern_attrs: vec![#extern_attrs],
#[allow(clippy::or_fun_call)]
search_path: None #( .unwrap_or_else(|| Some(vec![#search_path])) )*,
#[allow(clippy::or_fun_call)]
operator: None #( .unwrap_or_else(|| Some(#operator)) )*,
cast: None #( .unwrap_or_else(|| Some(#cast)) )*,
to_sql_config: #to_sql_config,
};
::pgrx::pgrx_sql_entity_graph::SqlGraphEntity::Function(submission)
}
}
}
fn finfo_tokens(&self) -> TokenStream2 {
let finfo_name = syn::Ident::new(
&format!("pg_finfo_{}_wrapper", self.func.sig.ident),
Span::call_site(),
);
quote_spanned! { self.func.sig.span() =>
#[no_mangle]
#[doc(hidden)]
pub extern "C" fn #finfo_name() -> &'static ::pgrx::pg_sys::Pg_finfo_record {
const V1_API: ::pgrx::pg_sys::Pg_finfo_record = ::pgrx::pg_sys::Pg_finfo_record { api_version: 1 };
&V1_API
}
}
}
pub fn wrapper_func(&self) -> TokenStream2 {
let func_name = &self.func.sig.ident;
let func_name_wrapper = Ident::new(
&format!("{}_wrapper", &self.func.sig.ident.to_string()),
self.func.sig.ident.span(),
);
let func_generics = &self.func.sig.generics;
let is_raw = self.extern_attrs().contains(&Attribute::Raw);
// We use a `_` prefix to make functions with no args more satisfied during linting.
let fcinfo_ident = syn::Ident::new("_fcinfo", self.func.sig.ident.span());
let args = &self.inputs;
let arg_pats = args
.iter()
.map(|v| syn::Ident::new(&format!("{}_", &v.pat), self.func.sig.span()))
.collect::<Vec<_>>();
let arg_fetches = args.iter().enumerate().map(|(idx, arg)| {
let pat = &arg_pats[idx];
let resolved_ty = &arg.used_ty.resolved_ty;
if arg.used_ty.resolved_ty.to_token_stream().to_string() == quote!(pgrx::pg_sys::FunctionCallInfo).to_token_stream().to_string()
|| arg.used_ty.resolved_ty.to_token_stream().to_string() == quote!(pg_sys::FunctionCallInfo).to_token_stream().to_string()
|| arg.used_ty.resolved_ty.to_token_stream().to_string() == quote!(::pgrx::pg_sys::FunctionCallInfo).to_token_stream().to_string()
{
quote_spanned! {pat.span()=>
let #pat = #fcinfo_ident;
}
} else if arg.used_ty.resolved_ty.to_token_stream().to_string() == quote!(()).to_token_stream().to_string() {
quote_spanned! {pat.span()=>
debug_assert!(unsafe { ::pgrx::fcinfo::pg_getarg::<()>(#fcinfo_ident, #idx).is_none() }, "A `()` argument should always receive `NULL`");
let #pat = ();
}
} else {
match (is_raw, &arg.used_ty.optional) {
(true, None) | (true, Some(_)) => quote_spanned! { pat.span() =>
let #pat = unsafe { ::pgrx::fcinfo::pg_getarg_datum_raw(#fcinfo_ident, #idx) as #resolved_ty };
},
(false, None) => quote_spanned! { pat.span() =>
let #pat = unsafe { ::pgrx::fcinfo::pg_getarg::<#resolved_ty>(#fcinfo_ident, #idx).unwrap_or_else(|| panic!("{} is null", stringify!{#pat})) };
},
(false, Some(inner)) => quote_spanned! { pat.span() =>
let #pat = unsafe { ::pgrx::fcinfo::pg_getarg::<#inner>(#fcinfo_ident, #idx) };
},
}
}
});
// Iterators require fancy handling for their retvals
let emit_result_handler = |span: Span, optional: bool, result: bool| {
let mut ret_expr = quote! { #func_name(#(#arg_pats),*) };
if result {
// If it's a result, we need to report it.
ret_expr = quote! { #ret_expr.unwrap_or_report() };
}
if !optional {
// If it's not already an option, we need to wrap it.
ret_expr = quote! { Some(#ret_expr) };
}
let import = result.then(|| quote! { use ::pgrx::pg_sys::panic::ErrorReportable; });
quote_spanned! { span =>
#import
#ret_expr
}
};
// This is the generic wrapper fn that everything needs
let extern_c_wrapper =
|span, returns_datum: bool, wrapped_contents: proc_macro2::TokenStream| {
let return_ty = returns_datum.then(|| quote! { -> ::pgrx::pg_sys::Datum });
quote_spanned! { span=>
#[no_mangle]
#[doc(hidden)]
#[::pgrx::pgrx_macros::pg_guard]
pub unsafe extern "C" fn #func_name_wrapper #func_generics(#fcinfo_ident: ::pgrx::pg_sys::FunctionCallInfo) #return_ty {
#wrapped_contents
}
}
};
match &self.returns {
Returning::None => {
let fn_contents = quote! {
#(#arg_fetches)*
#[allow(unused_unsafe)]
unsafe { #func_name(#(#arg_pats),*) }
};
extern_c_wrapper(self.func.sig.span(), false, fn_contents)
}
Returning::Type(retval_ty) => {
let result_ident = syn::Ident::new("result", self.func.sig.span());
let retval_transform = if retval_ty.resolved_ty == syn::parse_quote!(()) {
quote_spanned! { self.func.sig.output.span() =>
unsafe { ::pgrx::fcinfo::pg_return_void() }
}
} else if retval_ty.result {
if retval_ty.optional.is_some() {
// returning `Result<Option<T>>`
quote_spanned! {
self.func.sig.output.span() =>
match ::pgrx::datum::IntoDatum::into_datum(#result_ident) {
Some(datum) => datum,
None => unsafe { ::pgrx::fcinfo::pg_return_null(#fcinfo_ident) },
}
}
} else {
// returning Result<T>
quote_spanned! {
self.func.sig.output.span() =>
::pgrx::datum::IntoDatum::into_datum(#result_ident).unwrap_or_else(|| panic!("returned Datum was NULL"))
}
}
} else if retval_ty.resolved_ty == syn::parse_quote!(pg_sys::Datum)
|| retval_ty.resolved_ty == syn::parse_quote!(pgrx::pg_sys::Datum)
|| retval_ty.resolved_ty == syn::parse_quote!(::pgrx::pg_sys::Datum)
{
quote_spanned! { self.func.sig.output.span() =>
#result_ident
}
} else if retval_ty.optional.is_some() {
quote_spanned! { self.func.sig.output.span() =>
match #result_ident {
Some(result) => {
::pgrx::datum::IntoDatum::into_datum(result).unwrap_or_else(|| panic!("returned Option<T> was NULL"))
},
None => unsafe { ::pgrx::fcinfo::pg_return_null(#fcinfo_ident) }
}
}
} else {
quote_spanned! { self.func.sig.output.span() =>
::pgrx::datum::IntoDatum::into_datum(#result_ident).unwrap_or_else(|| panic!("returned Datum was NULL"))
}
};
let fn_contents = quote! {
#(#arg_fetches)*
#[allow(unused_unsafe)] // unwrapped fn might be unsafe
let #result_ident = unsafe { #func_name(#(#arg_pats),*) };
#retval_transform
};
extern_c_wrapper(self.func.sig.span(), true, fn_contents)
}
Returning::SetOf { ty: _retval_ty, optional, result } => {
let result_handler = emit_result_handler(self.func.sig.span(), *optional, *result);
let setof_closure = quote! {
#[allow(unused_unsafe)]
unsafe {
// SAFETY: the caller has asserted that `fcinfo` is a valid FunctionCallInfo pointer, allocated by Postgres
// with all its fields properly setup. Unless the user is calling this wrapper function directly, this
// will always be the case
::pgrx::iter::SetOfIterator::srf_next(#fcinfo_ident, || {
#( #arg_fetches )*
#result_handler
})
}
};
extern_c_wrapper(self.func.sig.span(), true, setof_closure)
}
Returning::Iterated { tys: retval_tys, optional, result } => {
let result_handler = emit_result_handler(self.func.sig.span(), *optional, *result);
let iter_closure = if retval_tys.len() == 1 {
// Postgres considers functions returning a 1-field table (`RETURNS TABLE (T)`) to be
// a function that `RETURNS SETOF T`. So we write a different wrapper implementation
// that transparently transforms the `TableIterator` returned by the user into a `SetOfIterator`
quote! {
#[allow(unused_unsafe)]
unsafe {
// SAFETY: the caller has asserted that `fcinfo` is a valid FunctionCallInfo pointer, allocated by Postgres
// with all its fields properly setup. Unless the user is calling this wrapper function directly, this
// will always be the case
::pgrx::iter::SetOfIterator::srf_next(#fcinfo_ident, || {
#( #arg_fetches )*
let table_iterator = { #result_handler };
// we need to convert the 1-field `TableIterator` provided by the user
// into a SetOfIterator in order to properly handle the case of `RETURNS TABLE (T)`,
// which is a table that returns only 1 field.
table_iterator.map(|i| ::pgrx::iter::SetOfIterator::new(i.into_iter().map(|(v,)| v)))
})
}
}
} else {
quote! {
#[allow(unused_unsafe)]
unsafe {
// SAFETY: the caller has asserted that `fcinfo` is a valid FunctionCallInfo pointer, allocated by Postgres
// with all its fields properly setup. Unless the user is calling this wrapper function directly, this
// will always be the case
::pgrx::iter::TableIterator::srf_next(#fcinfo_ident, || {
#( #arg_fetches )*
#result_handler
})
}
}
};
extern_c_wrapper(self.func.sig.span(), true, iter_closure)
}
}
}
}
impl ToEntityGraphTokens for PgExtern {
fn to_entity_graph_tokens(&self) -> TokenStream2 {
self.entity_tokens()
}
}
impl ToRustCodeTokens for PgExtern {
fn to_rust_code_tokens(&self) -> TokenStream2 {
let original_func = &self.func;
let wrapper_func = self.wrapper_func();
let finfo_tokens = self.finfo_tokens();
quote_spanned! { self.func.sig.span() =>
#original_func
#wrapper_func
#finfo_tokens
}
}
}
impl Parse for CodeEnrichment<PgExtern> {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let parser = Punctuated::<Attribute, Token![,]>::parse_terminated;
let punctuated_attrs = input.call(parser).ok().unwrap_or_default();
let attrs = punctuated_attrs.into_pairs().map(|pair| pair.into_value());
PgExtern::new(quote! {#(#attrs)*}, input.parse()?)
}
}