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
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
use syn;
use Bindings;
#[derive(Debug, Clone)]
pub struct BuilderField<'a> {
pub field_ident: &'a syn::Ident,
pub field_type: &'a syn::Type,
pub field_enabled: bool,
pub field_visibility: syn::Visibility,
pub attrs: &'a [syn::Attribute],
pub bindings: Bindings,
}
impl<'a> ToTokens for BuilderField<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if self.field_enabled {
trace!("Deriving builder field for `{}`.", self.field_ident);
let vis = &self.field_visibility;
let ident = self.field_ident;
let ty = self.field_type;
let attrs = self.attrs;
let option = self.bindings.option_ty();
tokens.append_all(quote!(
#(#attrs)* #vis #ident: #option<#ty>,
));
} else {
trace!(
"Skipping builder field for `{}`, fallback to PhantomData.",
self.field_ident
);
let ident = self.field_ident;
let ty = self.field_type;
let attrs = self.attrs;
let phantom_data = self.bindings.phantom_data_ty();
tokens.append_all(quote!(
#(#attrs)* #ident: #phantom_data<#ty>,
));
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! default_builder_field {
() => {{
BuilderField {
field_ident: &syn::Ident::new("foo", ::proc_macro2::Span::call_site()),
field_type: &syn::parse_str("String").unwrap(),
field_enabled: true,
field_visibility: syn::parse_str("pub").unwrap(),
attrs: &[parse_quote!(#[some_attr])],
bindings: Default::default(),
}
}};
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn setter_enabled() {
let field = default_builder_field!();
assert_eq!(
quote!(#field).to_string(),
quote!(
#[some_attr] pub foo: ::std::option::Option<String>,
)
.to_string()
);
}
#[test]
fn setter_disabled() {
let mut field = default_builder_field!();
field.field_enabled = false;
assert_eq!(
quote!(#field).to_string(),
quote!(
#[some_attr]
foo: ::std::marker::PhantomData<String>,
)
.to_string()
);
}
#[test]
fn no_std_setter_enabled() {
let mut field = default_builder_field!();
field.bindings.no_std = true;
assert_eq!(
quote!(#field).to_string(),
quote!(
#[some_attr] pub foo: ::core::option::Option<String>,
)
.to_string()
);
}
#[test]
fn no_std_setter_disabled() {
let mut field = default_builder_field!();
field.bindings.no_std = true;
field.field_enabled = false;
assert_eq!(
quote!(#field).to_string(),
quote!(
#[some_attr]
foo: ::core::marker::PhantomData<String>,
)
.to_string()
);
}
#[test]
fn private_field() {
let private = syn::Visibility::Inherited;
let mut field = default_builder_field!();
field.field_visibility = private;
assert_eq!(
quote!(#field).to_string(),
quote!(
#[some_attr]
foo: ::std::option::Option<String>,
)
.to_string()
);
}
}