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
// --------------------------------------------------
// mods
// --------------------------------------------------
pub(crate) mod default;
pub(crate) mod keylen;
pub(crate) mod sentinel;
pub(crate) mod stream;
// --------------------------------------------------
// local
// --------------------------------------------------
use crate::symbol;
use crate::Ctxt;
use default::DefaultXcoder;
use keylen::Xcoder;
use sentinel::Sentinel;
use stream::Stream;
// --------------------------------------------------
// external
// --------------------------------------------------
use quote::ToTokens;
use std::collections::HashMap;
use syn::punctuated::Punctuated;
use syn::Token;
#[derive(Debug)]
/// Represents struct attribute information
pub(crate) struct Container {
pub stream: Option<syn::Type>,
pub sentinel: Option<syn::Lit>,
pub key: Option<Xcoder>,
pub len: Option<Xcoder>,
pub defaults: HashMap<syn::Type, DefaultXcoder>,
pub debug: Option<syn::Path>,
pub deny_unknown_keys: Option<syn::Path>,
pub allow_unimplemented_decode: Option<syn::Path>,
pub allow_unimplemented_encode: Option<syn::Path>,
pub trait_fallback: Option<syn::Path>,
}
/// [`Container`] implementation
impl Container {
/// Extract out the `#[klv(...)]` attributes from a container.
///
/// Only container implemented is struct.
pub fn from_ast(cx: &Ctxt, item: &syn::DeriveInput) -> Self {
// --------------------------------------------------
// init
// --------------------------------------------------
let mut stream = None;
let mut sentinel = None;
let mut key = None;
let mut len = None;
let mut defaults: HashMap<syn::Type, DefaultXcoder> = HashMap::new();
let mut debug = None;
let mut deny_unknown_keys = None;
let mut allow_unimplemented_decode = None;
let mut allow_unimplemented_encode = None;
let mut trait_fallback = None;
// --------------------------------------------------
// loop through attrs
// --------------------------------------------------
for attr in &item.attrs {
// --------------------------------------------------
// only look for klv attr(s)
// --------------------------------------------------
if attr.path() != symbol::KLV_ATTR {
continue;
}
// --------------------------------------------------
// split using comma
// --------------------------------------------------
let nested =
match attr.parse_args_with(Punctuated::<syn::Meta, Token![,]>::parse_terminated) {
Ok(nested) => nested,
Err(err) => {
cx.syn_error(err);
continue;
}
};
// --------------------------------------------------
// loop through comma blocks
// --------------------------------------------------
for meta in nested {
match meta {
// --------------------------------------------------
// handle all: `syn::MetaList`
// e.g. `key(enc = <..>, dec = <..>),`
// --------------------------------------------------
syn::Meta::List(list) => match symbol::Symbol::from(&list.path) {
symbol::KEY => match key {
Some(_) => cx.error_spanned_by(&list, err!(DuplicateKey)),
None => {
key = Xcoder::try_from(&list)
.map_err(|err| cx.syn_error(err))
.ok()
}
},
symbol::LENGTH => match len {
Some(_) => cx.error_spanned_by(&list.path, err!(DuplicateLength)),
None => {
len = Xcoder::try_from(&list)
.map_err(|err| cx.syn_error(err))
.ok()
}
},
symbol::DEFAULT => {
let new = DefaultXcoder::from(&list);
if let Some(err) = new.errors {
cx.syn_error(err);
continue;
}
// this should always be `Some` otherwise `new.errors` would have
// been `Some` and caught above. but I don't want to unwrap
let Some(new_typ) = new.typ.clone() else {
continue;
};
match defaults.get_mut(&new_typ) {
Some(old) => {
match (&old.dec, &new.dec) {
(Some(_), Some(dec_path)) => cx.error_spanned_by(
dec_path,
err!(DuplicateDefault(new.typ; "decoder")),
),
(None, _) => old.dec = new.dec,
_ => (),
}
match (&old.enc, &new.enc) {
(Some(_), Some(enc_path)) => cx.error_spanned_by(
enc_path,
err!(DuplicateDefault(new.typ; "encoder")),
),
(None, _) => old.enc = new.enc,
_ => (),
}
}
None => {
let _ = defaults.insert(new_typ, new);
}
}
}
// --------------------------------------------------
// non lists
// --------------------------------------------------
symbol::STREAM => cx.error_spanned_by(
&list.path,
err!(ExpectedAsNameValue(symbol::STREAM)),
),
symbol::SENTINEL => cx.error_spanned_by(
&list.path,
err!(ExpectedAsNameValue(symbol::SENTINEL)),
),
symbol::DEBUG => {
cx.error_spanned_by(&list.path, err!(ExpectedAsPath(symbol::DEBUG)))
}
symbol::DENY_UNKNOWN_KEYS => cx.error_spanned_by(
&list.path,
err!(ExpectedAsPath(symbol::DENY_UNKNOWN_KEYS)),
),
symbol::ALLOW_UNIMPLEMENTED_DECODE => cx.error_spanned_by(
&list.path,
err!(ExpectedAsPath(symbol::ALLOW_UNIMPLEMENTED_DECODE)),
),
symbol::ALLOW_UNIMPLEMENTED_ENCODE => cx.error_spanned_by(
&list.path,
err!(ExpectedAsPath(symbol::ALLOW_UNIMPLEMENTED_ENCODE)),
),
symbol::TRAIT_FALLBACK => cx.error_spanned_by(
&list.path,
err!(ExpectedAsPath(symbol::TRAIT_FALLBACK)),
),
_ => cx.error_spanned_by(
&list.path,
err!(UnknownContainerListAttribute(list.path)),
),
},
// --------------------------------------------------
// handle all: `syn::MetaNameValue`
// e.g. `sentinel = <value>,`
// --------------------------------------------------
syn::Meta::NameValue(nv) => match symbol::Symbol::from(&nv.path) {
symbol::STREAM => match stream {
Some(_) => cx.error_spanned_by(&nv, err!(DuplicateStream)),
None => {
stream = Stream::try_from(&nv).map_err(|err| cx.syn_error(err)).ok()
}
},
symbol::SENTINEL => match sentinel {
Some(_) => cx.error_spanned_by(&nv, err!(DuplicateSentinel)),
None => {
sentinel = Sentinel::try_from(&nv)
.map_err(|err| cx.syn_error(err))
.ok()
}
},
// --------------------------------------------------
// non name-values
// --------------------------------------------------
symbol::KEY => {
cx.error_spanned_by(&nv.path, err!(ExpectedAsList(symbol::KEY)))
}
symbol::LENGTH => {
cx.error_spanned_by(&nv.path, err!(ExpectedAsList(symbol::LENGTH)))
}
symbol::DEFAULT => {
cx.error_spanned_by(&nv.path, err!(ExpectedAsList(symbol::DEFAULT)))
}
symbol::DEBUG => {
cx.error_spanned_by(&nv.path, err!(ExpectedAsPath(symbol::DEBUG)))
}
symbol::DENY_UNKNOWN_KEYS => cx.error_spanned_by(
&nv.path,
err!(ExpectedAsPath(symbol::DENY_UNKNOWN_KEYS)),
),
symbol::ALLOW_UNIMPLEMENTED_DECODE => cx.error_spanned_by(
&nv.path,
err!(ExpectedAsPath(symbol::ALLOW_UNIMPLEMENTED_DECODE)),
),
symbol::ALLOW_UNIMPLEMENTED_ENCODE => cx.error_spanned_by(
&nv.path,
err!(ExpectedAsPath(symbol::ALLOW_UNIMPLEMENTED_ENCODE)),
),
symbol::TRAIT_FALLBACK => cx.error_spanned_by(
&nv.path,
err!(ExpectedAsPath(symbol::TRAIT_FALLBACK)),
),
_ => cx.error_spanned_by(
&nv.path,
err!(UnknownContainerNameValueAttribute(nv.path)),
),
},
// --------------------------------------------------
// handle all: `syn::Path` (basically idents/flags)
// e.g. `allow_unimplemented_decode,`
// --------------------------------------------------
syn::Meta::Path(path) => match symbol::Symbol::from(&path) {
symbol::DEBUG => debug = Some(path),
symbol::DENY_UNKNOWN_KEYS => deny_unknown_keys = Some(path),
symbol::ALLOW_UNIMPLEMENTED_DECODE => {
allow_unimplemented_decode = Some(path)
}
symbol::ALLOW_UNIMPLEMENTED_ENCODE => {
allow_unimplemented_encode = Some(path)
}
symbol::TRAIT_FALLBACK => trait_fallback = Some(path),
// --------------------------------------------------
// non paths
// --------------------------------------------------
symbol::KEY => {
cx.error_spanned_by(&path, err!(ExpectedAsList(symbol::KEY)))
}
symbol::LENGTH => {
cx.error_spanned_by(&path, err!(ExpectedAsList(symbol::LENGTH)))
}
symbol::DEFAULT => {
cx.error_spanned_by(&path, err!(ExpectedAsList(symbol::DEFAULT)))
}
symbol::STREAM => {
cx.error_spanned_by(&path, err!(ExpectedAsNameValue(symbol::STREAM)))
}
symbol::SENTINEL => {
cx.error_spanned_by(&path, err!(ExpectedAsNameValue(symbol::SENTINEL)))
}
_ => cx.error_spanned_by(&path, err!(UnknownContainerAttribute(path))),
},
}
}
}
// --------------------------------------------------
// unimplemented encode error
// --------------------------------------------------
if allow_unimplemented_encode.is_none() {
if let Some(Xcoder { enc: None, .. }) = key {
cx.error_spanned_by(&item.ident, err!(MissingEncInKeyLen(symbol::KEY)));
}
if let Some(Xcoder { enc: None, .. }) = len {
cx.error_spanned_by(&item.ident, err!(MissingEncInKeyLen(symbol::LENGTH)));
}
}
// --------------------------------------------------
// unimplemented decode error
// --------------------------------------------------
if allow_unimplemented_decode.is_none() {
if let Some(Xcoder { dec: None, .. }) = key {
cx.error_spanned_by(&item.ident, err!(MissingDecInKeyLen(symbol::KEY)));
}
if let Some(Xcoder { dec: None, .. }) = len {
cx.error_spanned_by(&item.ident, err!(MissingDecInKeyLen(symbol::LENGTH)));
}
}
// --------------------------------------------------
// return
// --------------------------------------------------
Container {
stream: stream.and_then(|x| x.0),
sentinel: sentinel.and_then(|x| x.0),
key,
len,
defaults,
debug,
deny_unknown_keys,
allow_unimplemented_decode,
allow_unimplemented_encode,
trait_fallback,
}
}
}
/// A parsed container
///
/// * `_allow_unimplemented_decode`
/// * `_allow_unimplemented_encode`
/// * `trait_fallback`
///
/// are currently not used at this stage, but the paths are kept for potential
/// future docs/debugging during expansion.
pub(crate) struct ContainerParsed {
pub stream: Option<syn::Type>,
pub sentinel: Option<syn::Lit>,
pub key: Xcoder,
pub len: Xcoder,
pub debug: Option<syn::Path>,
pub deny_unknown_keys: Option<syn::Path>,
pub _allow_unimplemented_decode: Option<syn::Path>,
pub _allow_unimplemented_encode: Option<syn::Path>,
pub _trait_fallback: Option<syn::Path>,
}
/// [`ContainerParsed`] implementation
impl ContainerParsed {
pub fn from_cont(cx: &Ctxt, name: &syn::Ident, cont: Container) -> Option<Self> {
// --------------------------------------------------
// check for required fields
// --------------------------------------------------
let (key, len) = match (cont.key, cont.len) {
(Some(k), Some(l)) => (k, l),
(Some(_), None) => {
cx.error_spanned_by(name, err!(MissingLength));
return None;
}
(None, Some(_)) => {
cx.error_spanned_by(name, err!(MissingKey));
return None;
}
(None, None) => {
cx.error_spanned_by(name, err!(MissingKey));
cx.error_spanned_by(name, err!(MissingLength));
return None;
}
};
// --------------------------------------------------
// return parsed container
// --------------------------------------------------
Some(ContainerParsed {
stream: cont.stream,
sentinel: cont.sentinel,
key,
len,
debug: cont.debug,
deny_unknown_keys: cont.deny_unknown_keys,
_allow_unimplemented_decode: cont.allow_unimplemented_decode,
_allow_unimplemented_encode: cont.allow_unimplemented_encode,
_trait_fallback: cont.trait_fallback,
})
}
}