binrw_derive 0.15.1

Derive macro for binrw
Documentation
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
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
use syn::{
    braced, parenthesized,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    spanned::Spanned,
    token::{self, Token},
    Expr, Ident, Lit, Token, Type,
};

pub(crate) trait KeywordToken {
    type Token: Token;

    fn display() -> &'static str {
        <Self::Token as Token>::display()
    }

    fn dyn_display(&self) -> &'static str {
        Self::display()
    }

    fn keyword_span(&self) -> Span;
}

type Fields<T> = Punctuated<T, Token![,]>;

/// `MetaExpr` represents a key/expr pair
/// Takes two forms:
/// * ident(expr)
/// * ident = expr
///
/// both are always allowed
pub(crate) type MetaExpr<Keyword> = MetaValue<Keyword, Expr>;

/// `MetaType` represents a key/ty pair
/// Takes two forms:
/// * ident(ty)
/// * ident = ty
///
/// both are always allowed
pub(crate) type MetaType<Keyword> = MetaValue<Keyword, Type>;

/// `MetaIdent` represents a key/ident pair
/// Takes two forms:
/// * ident(ident)
/// * ident = ident
///
/// both are always allowed
pub(crate) type MetaIdent<Keyword> = MetaValue<Keyword, Ident>;

/// `MetaLit` represents a key/lit pair
/// Takes two forms:
/// * ident(lit)
/// * ident = lit
///
/// both are always allowed
pub(crate) type MetaLit<Keyword> = MetaValue<Keyword, Lit>;

#[derive(Debug, Clone)]
pub(crate) struct MetaValue<Keyword, Value> {
    pub(crate) ident: Keyword,
    pub(crate) value: Value,
}

impl<Keyword: Token + Spanned> KeywordToken for MetaVoid<Keyword> {
    type Token = Keyword;

    fn keyword_span(&self) -> Span {
        self.ident.span()
    }
}

impl<Keyword: Parse, Value: Parse> Parse for MetaValue<Keyword, Value> {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let ident = input.parse()?;
        let value = if input.peek(token::Paren) {
            let content;
            parenthesized!(content in input);
            content.parse()?
        } else {
            input.parse::<Token![=]>()?;
            input.parse()?
        };

        Ok(MetaValue { ident, value })
    }
}

impl<Keyword, Value: ToTokens> From<MetaValue<Keyword, Value>> for TokenStream {
    fn from(value: MetaValue<Keyword, Value>) -> Self {
        value.value.into_token_stream()
    }
}

impl<Keyword> From<MetaValue<Keyword, Ident>> for Ident {
    fn from(value: MetaValue<Keyword, Ident>) -> Self {
        value.value
    }
}

impl<Keyword, Value: ToTokens> ToTokens for MetaValue<Keyword, Value> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.value.to_tokens(tokens);
    }
}

impl<Keyword: Token + Spanned, Value> KeywordToken for MetaValue<Keyword, Value> {
    type Token = Keyword;

    fn keyword_span(&self) -> Span {
        self.ident.span()
    }
}

#[derive(Debug, Clone)]
pub(crate) struct MetaVoid<Keyword> {
    pub(crate) ident: Keyword,
}

impl<Keyword: Parse> Parse for MetaVoid<Keyword> {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        Ok(MetaVoid {
            ident: input.parse()?,
        })
    }
}

impl<Keyword> From<MetaVoid<Keyword>> for () {
    fn from(_: MetaVoid<Keyword>) -> Self {}
}

#[derive(Debug, Clone)]
pub(crate) struct MetaList<Keyword, ItemType> {
    pub(crate) ident: Keyword,
    pub(crate) fields: Fields<ItemType>,
}

impl<Keyword: Parse, ItemType: Parse> Parse for MetaList<Keyword, ItemType> {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let ident = input.parse()?;
        let content;
        parenthesized!(content in input);
        Ok(MetaList {
            ident,
            fields: content.parse_terminated(ItemType::parse, Token![,])?,
        })
    }
}

impl<Keyword: Token + Spanned, ItemType> KeywordToken for MetaList<Keyword, ItemType> {
    type Token = Keyword;

    fn keyword_span(&self) -> Span {
        self.ident.span()
    }
}

#[derive(Debug, Clone)]
pub(crate) enum Enclosure<ParenType, BraceType> {
    Paren { fields: Fields<ParenType> },
    Brace { fields: Fields<BraceType> },
}

#[derive(Debug, Clone)]
pub(crate) struct MetaEnclosedList<Keyword, ParenItemType, BraceItemType> {
    pub(crate) ident: Keyword,
    pub(crate) list: Enclosure<ParenItemType, BraceItemType>,
}

impl<Keyword, ParenItemType, BraceItemType> Parse
    for MetaEnclosedList<Keyword, ParenItemType, BraceItemType>
where
    Keyword: Parse,
    ParenItemType: Parse,
    BraceItemType: Parse,
{
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let ident = input.parse()?;
        let content;
        let lookahead = input.lookahead1();
        if lookahead.peek(token::Paren) {
            parenthesized!(content in input);
            Ok(Self {
                ident,
                list: Enclosure::Paren {
                    fields: content.parse_terminated(ParenItemType::parse, Token![,])?,
                },
            })
        } else if lookahead.peek(token::Brace) {
            braced!(content in input);
            Ok(Self {
                ident,
                list: Enclosure::Brace {
                    fields: content.parse_terminated(BraceItemType::parse, Token![,])?,
                },
            })
        } else {
            Err(lookahead.error())
        }
    }
}

impl<Keyword: Token + Spanned, ParenItemType, BraceItemType> KeywordToken
    for MetaEnclosedList<Keyword, ParenItemType, BraceItemType>
{
    type Token = Keyword;

    fn keyword_span(&self) -> Span {
        self.ident.span()
    }
}

// This is like `syn::PatType` except:
// (1) Implements `Parse`;
// (2) No attributes;
// (3) Only allows an ident on the LHS instead of any `syn::Pat`.
#[derive(Debug, Clone)]
pub(crate) struct IdentPatType {
    pub(crate) ident: syn::Ident,
    pub(crate) ty: syn::Type,
}

impl Parse for IdentPatType {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let ident = input.parse()?;
        input.parse::<Token![:]>()?;
        let ty = input.parse()?;
        Ok(Self { ident, ty })
    }
}

// This is like `syn::PatType` except:
// (1) Implements `Parse`;
// (2) No attributes;
// (3) Only allows an ident on the LHS instead of any `syn::Pat`.
// (4) Optionally allows a `= $expr` following the type
#[derive(Debug, Clone)]
pub(crate) struct IdentTypeMaybeDefault {
    pub(crate) ident: syn::Ident,
    pub(crate) ty: syn::Type,
    pub(crate) default: Option<Box<syn::Expr>>,
}

impl Parse for IdentTypeMaybeDefault {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let ident = input.parse()?;
        input.parse::<Token![:]>()?;
        let ty = input.parse()?;
        let default = if input.lookahead1().peek(Token![=]) {
            input.parse::<Token![=]>()?;
            Some(input.parse()?)
        } else {
            None
        };

        Ok(Self { ident, ty, default })
    }
}

pub(crate) struct MetaAttrList<P>(Fields<P>);

impl<P> MetaAttrList<P> {
    pub(crate) fn into_iter(self) -> impl Iterator<Item = P> {
        self.0.into_iter()
    }
}

impl<P: Parse> Parse for MetaAttrList<P> {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        Ok(MetaAttrList(Fields::parse_terminated(input)?))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod kw {
        syn::custom_keyword!(test);
        syn::custom_keyword!(test_list);
        syn::custom_keyword!(test_enclosed_list);
    }

    type MetaValueTest = MetaValue<kw::test, Lit>;
    type MetaListTest = MetaList<kw::test_list, Lit>;
    type MetaAttrListTest = MetaAttrList<Lit>;
    type MetaEnclosedListTest = MetaEnclosedList<kw::test_enclosed_list, Lit, Lit>;

    macro_rules! try_parse {
        ($name:ident, $ty:ty, $tt:tt) => {
            #[test]
            #[cfg_attr(coverage_nightly, coverage(off))]
            fn $name() {
                syn::parse2::<$ty>(quote::quote! $tt).unwrap();
            }
        }
    }

    macro_rules! try_parse_fail {
        ($name:ident, $message:literal, $ty:ty, $tt:tt) => {
            #[test]
            #[cfg_attr(coverage_nightly, coverage(off))]
            #[should_panic = $message]
            fn $name() {
                syn::parse2::<$ty>(quote::quote! $tt).unwrap();
            }
        }
    }

    try_parse!(meta_keyword, kw::test, { test });

    try_parse!(meta_value_assign, MetaValueTest, { test = 3u8 });
    try_parse!(meta_value_paren, MetaValueTest, { test(b"TEST") });
    try_parse_fail!(meta_value_missing_keyword, "expected `test`", MetaValueTest, { = 3u8 });
    try_parse_fail!(meta_value_missing_value, "expected `=`", MetaValueTest, {
        test
    });
    try_parse_fail!(
        meta_value_wrong_keyword,
        "expected `test`",
        MetaValueTest,
        { wrong = 3u8 }
    );
    try_parse_fail!(
        meta_value_wrong_value_type,
        "expected literal",
        MetaValueTest,
        { test = u8 }
    );
    try_parse_fail!(
        meta_value_confused_as_list,
        "unexpected token",
        MetaValueTest,
        { test(3u8, 3u8) }
    );

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn meta_value_into_tokenstream() {
        let expected = quote::quote! { 0u8 };
        let value = syn::parse2::<MetaValueTest>(quote::quote! { test = #expected }).unwrap();
        assert_eq!(expected.to_string(), TokenStream::from(value).to_string());
    }

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn meta_value_to_tokens() {
        let expected = quote::quote! { 0u8 };
        let value = syn::parse2::<MetaValueTest>(quote::quote! { test = #expected }).unwrap();
        let mut actual = TokenStream::new();
        value.to_tokens(&mut actual);
        assert_eq!(expected.to_string(), actual.to_string());
    }

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn meta_value_keyword_token() {
        use syn::spanned::Spanned;
        let keyword = quote::quote! { test };
        let value = syn::parse2::<MetaValueTest>(quote::quote! { #keyword = 0u8 }).unwrap();
        assert_eq!(
            format!("{:?}", keyword.span()),
            format!("{:?}", value.keyword_span())
        );
    }

    try_parse!(meta_list, MetaListTest, { test_list(3u8, 3u8) });
    try_parse!(meta_list_empty, MetaListTest, { test_list() });
    try_parse_fail!(
        meta_list_missing_keyword,
        "expected `test_list`",
        MetaListTest,
        { (3u8, 3u8) }
    );
    try_parse_fail!(
        meta_list_missing_value,
        "unexpected end of input",
        MetaListTest,
        { test_list }
    );
    try_parse_fail!(
        meta_list_wrong_delimiter,
        "expected parentheses",
        MetaListTest,
        { test_list = (3u8, 3u8) }
    );
    try_parse_fail!(
        meta_list_wrong_keyword,
        "expected `test_list`",
        MetaListTest,
        { wrong }
    );
    try_parse_fail!(
        meta_list_wrong_item_type,
        "expected literal",
        MetaListTest,
        { test_list(i32) }
    );

    try_parse!(meta_enclosed_list_paren, MetaEnclosedListTest, {
        test_enclosed_list(3u8, 3u8)
    });
    try_parse!(meta_enclosed_list_paren_empty, MetaEnclosedListTest, {
        test_enclosed_list()
    });
    try_parse!(meta_enclosed_list_brace, MetaEnclosedListTest, { test_enclosed_list { 3u8, 3u8 } });
    try_parse!(meta_enclosed_list_brace_empty, MetaEnclosedListTest, {
        test_enclosed_list {}
    });
    try_parse_fail!(
        meta_enclosed_list_wrong_keyword,
        "expected `test_enclosed_list`",
        MetaEnclosedListTest,
        { wrong }
    );
    try_parse_fail!(
        meta_enclosed_list_wrong_delimiter,
        "expected parentheses or curly braces",
        MetaEnclosedListTest,
        { test_enclosed_list = (3u8, 3u8) }
    );
    try_parse_fail!(
        meta_enclosed_list_wrong_bracket_kind,
        "expected parentheses or curly braces",
        MetaEnclosedListTest,
        { test_enclosed_list [] }
    );
    try_parse_fail!(
        meta_enclosed_list_wrong_item_type,
        "expected literal",
        MetaEnclosedListTest,
        { test_enclosed_list(i32) }
    );

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn meta_list_keyword_token() {
        use syn::spanned::Spanned;
        let keyword = quote::quote! { test_list };
        let value = syn::parse2::<MetaListTest>(quote::quote! { #keyword(0u8, 0u8) }).unwrap();
        assert_eq!(
            format!("{:?}", keyword.span()),
            format!("{:?}", value.keyword_span())
        );
    }

    try_parse!(ident_pat_type, IdentPatType, { foo: u8 });
    try_parse_fail!(ident_pat_type_missing_ident, "expected identifier", IdentPatType, { : 3u8 });
    try_parse_fail!(ident_pat_type_missing_ty, "unexpected end of input", IdentPatType, { foo: });
    try_parse_fail!(ident_pat_type_wrong_ty_type, "expected one of", IdentPatType, { foo: 3u8 });

    try_parse!(ident_type_default, IdentTypeMaybeDefault, { foo: u8 = 1 });
    try_parse!(ident_type_no_default, IdentTypeMaybeDefault, { foo: u8 });
    try_parse_fail!(ident_type_missing_type, "unexpected end of input", IdentTypeMaybeDefault, { foo: });
    try_parse_fail!(ident_type_missing_colon, "expected `:`", IdentTypeMaybeDefault, { foo u8 });
    try_parse_fail!(ident_type_missing_ident, "expected identifier", IdentTypeMaybeDefault, { :u8 });

    try_parse!(meta_attr_list, MetaAttrListTest, { 1u8, 2u8, 3u8 });
    try_parse!(meta_attr_list_empty, MetaAttrListTest, {});
    try_parse_fail!(
        meta_attr_list_wrong_type,
        "expected literal",
        MetaAttrListTest,
        { i32 }
    );

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn meta_attr_list_into_iter() {
        let expected = [
            Lit::new(proc_macro2::Literal::u8_suffixed(1)),
            Lit::new(proc_macro2::Literal::u8_suffixed(2)),
            Lit::new(proc_macro2::Literal::u8_suffixed(3)),
        ];

        let value = syn::parse2::<MetaAttrListTest>(quote::quote! { 1u8, 2u8, 3u8 }).unwrap();
        assert_eq!(expected, value.into_iter().collect::<Vec<_>>()[..]);
    }
}