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
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use syn::spanned::Spanned;
use syn::{Ident, PathArguments, Type};
use crate::field::Field;
pub struct MethodBody<'a> {
fields: &'a [Field],
dummy: &'a Ident,
}
impl<'a> MethodBody<'a> {
pub fn new(fields: &'a [Field], dummy: &'a Ident) -> Self {
MethodBody { fields, dummy }
}
}
impl<'a> ToTokens for MethodBody<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let dummy = &self.dummy;
let (this, signer, ck, opts) = (
quote! { #dummy.0 },
quote! { #dummy.1 },
quote! { #dummy.2 },
quote! { #dummy.3 },
);
let mut ready = false;
for f in self.fields {
let ident = &f.ident;
f.with_renamed(|name| {
if f.meta.skip {
return;
}
if **name > *"oauth_" && !ready {
quote!(
let mut #dummy = (
#this,
#signer.oauth_parameters(#ck, #opts),
);
)
.to_tokens(tokens);
ready = true;
}
let ty_is_option = f.meta.option.get().map(|v| **v).unwrap_or_else(|| {
if let Type::Path(ref ty_path) = f.ty {
let path = &ty_path.path;
path.leading_colon.is_none()
&& path.segments.len() == 1
&& path.segments[0].ident == "Option"
&& match path.segments[0].arguments {
PathArguments::AngleBracketed(ref args) => args.args.len() == 1,
PathArguments::None | PathArguments::Parenthesized(_) => false,
}
} else {
false
}
});
let value = if ty_is_option {
quote_spanned! {f.ty.span()=> {
let value = &#this.#ident;
::std::option::Option::as_ref(value).unwrap()
}}
} else {
quote! { &#this.#ident }
};
let display = if let Some(fmt) = f.meta.fmt.get() {
quote! {
{
use std::fmt::{Display, Formatter, Result};
// We can't just use `f.ty` instead of `T` because doing so would lead
// to E0412/E0261 if `f.ty` contains lifetime/type parameters.
struct Adapter<'a, T: 'a + ?Sized, F>(&'a T, F);
impl<'a, T: 'a + ?Sized, F> Display for Adapter<'a, T, F>
where
F: Fn(&T, &mut Formatter<'_>) -> Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
self.1(self.0, f)
}
}
// A helper to make deref coertion from `&#f.ty` to `&T` work properly.
struct MakeAdapter<F>(F);
impl<F> MakeAdapter<F> {
fn make_adapter<T: ?Sized>(self, t: &T) -> Adapter<'_, T, F>
where
for<'a> Adapter<'a, T, F>: Display,
{
Adapter(t, self.0)
}
}
MakeAdapter
}({
let fmt: fn(&_, &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result =
#fmt;
fmt
})
.make_adapter(#value)
}
} else {
value.clone()
};
let mut stmt = if f.meta.encoded {
quote_spanned! {f.ty.span()=>
#signer.parameter_encoded(#name, #display);
}
} else {
quote_spanned! {f.ty.span()=>
#signer.parameter(#name, #display);
}
};
if let Some(skip_if) = f.meta.skip_if.get() {
stmt = quote! {
if !{
let skip_if: fn(&_) -> bool = #skip_if;
skip_if
}(#value)
{
#stmt
}
};
}
if ty_is_option {
stmt = quote_spanned! {f.ty.span()=>
if {
let value = &#this.#ident;
::std::option::Option::is_some(value)
} {
#stmt
}
};
}
stmt.to_tokens(tokens);
});
}
if ready {
quote! {
#signer.finish()
}
} else {
quote! {
#signer.finish(#ck, #opts)
}
}
.to_tokens(tokens);
}
}