forked from librasn/rasn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
335 lines (301 loc) · 11.4 KB
/
config.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
use quote::ToTokens;
use syn::Path;
use crate::tag::Tag;
#[derive(Debug)]
pub struct Config {
pub crate_root: Path,
pub enumerated: bool,
pub choice: bool,
pub automatic_tags: bool,
pub option_type: OptionalEnum,
pub delegate: bool,
pub tag: Option<Tag>,
}
impl Config {
pub fn from_attributes(input: &syn::DeriveInput) -> Self {
let mut choice = false;
let mut crate_root = None;
let mut enumerated = false;
let mut automatic_tags = false;
let mut tag = None;
let mut option = None;
let mut delegate = false;
let mut iter = input
.attrs
.iter()
.filter(|a| a.path.is_ident(crate::CRATE_NAME))
.map(|a| a.parse_meta().unwrap());
while let Some(syn::Meta::List(list)) = iter.next() {
for item in list.nested.iter().filter_map(|n| match n {
syn::NestedMeta::Meta(m) => Some(m),
_ => None,
}) {
let path = item.path();
if path.is_ident("crate_root") {
if let syn::Meta::NameValue(nv) = item {
crate_root = match &nv.lit {
syn::Lit::Str(s) => s.parse::<syn::Path>().ok(),
_ => None,
};
}
} else if path.is_ident("enumerated") {
enumerated = true;
} else if path.is_ident("choice") {
choice = true;
} else if path.is_ident("automatic_tags") {
automatic_tags = true;
} else if path.is_ident("option_type") {
if let syn::Meta::List(list) = item {
let filter_into_paths = |nm: &_| match nm {
syn::NestedMeta::Meta(meta) => Some(meta.path().clone()),
_ => None,
};
let mut iter = list
.nested
.iter()
.take(3)
.filter_map(filter_into_paths)
.fuse();
let path = iter.next();
let some_variant = iter.next();
let none_variant = iter.next();
option = Some((path, some_variant, none_variant));
}
} else if path.is_ident("tag") {
tag = Tag::from_meta(item);
} else if path.is_ident("delegate") {
delegate = true;
} else {
panic!("unknown input provided: {}", path.to_token_stream());
}
}
}
let is_enum = match input.data {
syn::Data::Enum(_) => true,
_ => false,
};
if is_enum && ((choice && enumerated) || (!choice && !enumerated)) {
panic!("A Rust enum must be marked either `choice` OR `enumerated`")
}
let mut invalid_delegate = false;
if is_enum && delegate {
invalid_delegate = true;
} else if delegate {
match &input.data {
syn::Data::Struct(data) => invalid_delegate = data.fields.len() != 1,
_ => (),
}
}
if invalid_delegate {
panic!("`#[rasn(delegate)]` is only valid on single-unit structs.");
}
let option_type = {
let (path, some_variant, none_variant) = option.unwrap_or((None, None, None));
OptionalEnum {
path: syn::TypePath {
path: path.unwrap_or_else(|| {
Path::from(syn::Ident::new("Option", proc_macro2::Span::call_site()))
}),
qself: None,
}
.into(),
some_variant: syn::TypePath {
path: some_variant.unwrap_or_else(|| {
Path::from(syn::Ident::new("Some", proc_macro2::Span::call_site()))
}),
qself: None,
}
.into(),
none_variant: syn::TypePath {
path: none_variant.unwrap_or_else(|| {
Path::from(syn::Ident::new("None", proc_macro2::Span::call_site()))
}),
qself: None,
}
.into(),
}
};
Self {
automatic_tags,
choice,
enumerated,
tag,
option_type,
delegate,
crate_root: crate_root.unwrap_or_else(|| {
syn::LitStr::new(crate::CRATE_NAME, proc_macro2::Span::call_site())
.parse()
.unwrap()
}),
}
}
fn tag_tree_for_ty(&self, ty: &syn::Type) -> proc_macro2::TokenStream {
let crate_root = &self.crate_root;
quote!(if !<#ty as #crate_root::AsnType>::TAG.const_eq(&#crate_root::Tag::EOC) {
#crate_root::TagTree::Leaf(<#ty as #crate_root::AsnType>::TAG)
} else {
<#ty as #crate_root::AsnType>::TAG_TREE
})
}
}
#[derive(Debug)]
pub struct OptionalEnum {
pub path: syn::Type,
pub some_variant: syn::Type,
pub none_variant: syn::Type,
}
pub struct VariantConfig<'a> {
variant: &'a syn::Variant,
container_config: &'a Config,
pub tag: Option<Tag>,
}
impl<'a> VariantConfig<'a> {
pub fn new(variant: &'a syn::Variant, container_config: &'a Config) -> Self {
let mut tag = None;
let mut iter = variant
.attrs
.iter()
.filter_map(|a| a.parse_meta().ok())
.filter(|m| m.path().is_ident(crate::CRATE_NAME));
while let Some(syn::Meta::List(list)) = iter.next() {
for item in list.nested.iter().filter_map(|n| match n {
syn::NestedMeta::Meta(m) => Some(m),
_ => None,
}) {
let path = item.path();
if path.is_ident("tag") {
tag = Tag::from_meta(item);
}
}
}
Self {
variant,
container_config,
tag,
}
}
pub fn tag(&self, context: usize) -> proc_macro2::TokenStream {
let crate_root = &self.container_config.crate_root;
if let Some(Tag { class, value }) = &self.tag {
quote!(#crate_root::Tag::new(#class, #value))
} else if self.container_config.automatic_tags {
quote!(#crate_root::Tag::new(#crate_root::types::Class::Context, #context as u32))
} else {
Tag::from_fields(&self.variant.fields, crate_root)
}
}
pub fn tag_tree(&self, context: usize) -> proc_macro2::TokenStream {
let crate_root = &self.container_config.crate_root;
if self.tag.is_some() || self.container_config.automatic_tags {
let tag = self.tag(context);
quote!(#crate_root::TagTree::Leaf(#tag),)
} else {
let field_tags = self
.variant
.fields
.iter()
.enumerate()
.map(|(i, f)| FieldConfig::new(f, self.container_config).tag_tree(i));
match self.variant.fields {
syn::Fields::Unit => {
quote!(#crate_root::TagTree::Leaf(<() as #crate_root::AsnType>::TAG),)
}
syn::Fields::Named(_) => {
quote!({
const FIELD_TAG_TREE: &'static [#crate_root::TagTree] = &[#(#field_tags,)*];
#crate_root::sa::const_assert!(#crate_root::TagTree::is_unique(FIELD_TAG_TREE));
#crate_root::TagTree::Leaf(#crate_root::Tag::SEQUENCE)
},)
}
syn::Fields::Unnamed(_) => {
if self.variant.fields.iter().count() != 1 {
panic!("Tuple-style enum variants must contain only a single field, switch to struct-style variants for multiple fields.");
} else {
let ty = &self.variant.fields.iter().next().unwrap().ty;
quote!(<#ty as #crate_root::AsnType>::TAG_TREE,)
}
}
}
}
}
}
#[derive(Debug)]
pub struct FieldConfig<'a> {
pub field: &'a syn::Field,
pub container_config: &'a Config,
pub tag: Option<Tag>,
pub default: Option<Option<syn::Path>>,
}
impl<'a> FieldConfig<'a> {
pub fn new(field: &'a syn::Field, container_config: &'a Config) -> Self {
let mut default = None;
let mut tag = None;
let mut iter = field
.attrs
.iter()
.filter_map(|a| a.parse_meta().ok())
.filter(|m| m.path().is_ident(crate::CRATE_NAME));
while let Some(syn::Meta::List(list)) = iter.next() {
for item in list.nested.iter().filter_map(|n| match n {
syn::NestedMeta::Meta(m) => Some(m),
_ => None,
}) {
let path = item.path();
if path.is_ident("tag") {
tag = Tag::from_meta(item);
} else if path.is_ident("default") {
default = Some(match item {
syn::Meta::List(list) => list
.nested
.iter()
.cloned()
.filter_map(unested_meta)
.map(|m| m.path().clone())
.next(),
_ => None,
});
}
}
}
Self {
field,
container_config,
tag,
default,
}
}
pub fn tag(&self, context: usize) -> proc_macro2::TokenStream {
let crate_root = &self.container_config.crate_root;
if let Some(Tag { class, value }) = &self.tag {
if self.container_config.automatic_tags {
panic!("You can't use the `#[rasn(tag)]` with `#[rasn(automatic_tags)]`")
}
quote!(#crate_root::Tag::new(#class, #value))
} else if self.container_config.automatic_tags {
quote!(#crate_root::Tag::new(#crate_root::types::Class::Context, #context as u32))
} else {
let ty = &self.field.ty;
quote!(<#ty as #crate_root::AsnType>::TAG)
}
}
pub fn tag_tree(&self, context: usize) -> proc_macro2::TokenStream {
let crate_root = &self.container_config.crate_root;
let ty = &self.field.ty;
if let Some(Tag { class, value }) = &self.tag {
if self.container_config.automatic_tags {
panic!("You can't use the `#[rasn(tag)]` with `#[rasn(automatic_tags)]`")
}
quote!(#crate_root::TagTree::Leaf(#crate_root::Tag::new(#class, #value)))
} else if self.container_config.automatic_tags {
quote!(#crate_root::TagTree::Leaf(#crate_root::Tag::new(#crate_root::types::Class::Context, #context as u32)))
} else {
self.container_config.tag_tree_for_ty(ty)
}
}
}
fn unested_meta(nm: syn::NestedMeta) -> Option<syn::Meta> {
match nm {
syn::NestedMeta::Meta(m) => Some(m),
_ => None,
}
}