grib-template-derive 0.1.3

Derive macros for the `grib` crate, used as an internal dependency
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
use proc_macro::TokenStream;
use quote::quote;

/// Derive macro generating an impl of the trait
/// `grib_template_helpers::TryFromSlice`.
#[proc_macro_derive(TryFromSlice, attributes(grib_template))]
pub fn derive_try_from_slice(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as syn::DeriveInput);

    match &input.data {
        syn::Data::Struct(data) => impl_try_from_slice_for_struct(&input, data),
        syn::Data::Enum(data) => impl_try_from_slice_for_enum(&input, data),
        _ => unimplemented!("`TryFromSlice` can only be derived for structs/enums"),
    }
    .into()
}

fn impl_try_from_slice_for_struct(
    input: &syn::DeriveInput,
    data: &syn::DataStruct,
) -> proc_macro2::TokenStream {
    let name = &input.ident;
    let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
    let Some((kind, fields)) = extract_struct_info(data) else {
        unimplemented!(
            "`TryFromSlice` can only be derived for structs with named fields or with a single unnamed `u8` field"
        )
    };

    if kind == StructKind::TupleStruct {
        let field_reads = fields.iter().map(|field| {
            let ty = &field.ty;
            quote! {
                <#ty as grib_template_helpers::TryFromSlice>::try_from_slice(slice, pos)?
            }
        });

        return quote! {
            impl #impl_generics grib_template_helpers::TryFromSlice for #name #type_generics #where_clause {
                fn try_from_slice(
                    slice: &[u8],
                    pos: &mut usize,
                ) -> grib_template_helpers::TryFromSliceResult<Self> {
                    Ok(Self(#(#field_reads),*))
                }
            }
        };
    }

    let mut field_reads = Vec::new();
    let mut idents = Vec::new();

    for field in fields {
        let ident = field.ident.as_ref().unwrap();
        let ty = &field.ty;

        let len_attr = field
            .attrs
            .iter()
            .find_map(|attr| attr_value(attr, "len").map(|v| parse_len_attr(&v)));
        if let Some(len) = len_attr {
            if let syn::Type::Path(type_path) = ty
                && let Some((inner_ty, has_option)) = extract_vec_inner(type_path)
            {
                let tokens = quote! {
                    let mut #ident = Vec::with_capacity(#len);
                    for _ in 0..#len {
                        let item =
                            <#inner_ty as grib_template_helpers::TryFromSlice>::try_from_slice(
                                slice,
                                pos,
                            )?;
                        #ident.push(item);
                    }
                };

                let tokens = if has_option {
                    quote! {
                        let #ident = if *pos == slice.len() {
                            None
                        } else {
                            #tokens
                            Some(#ident)
                        };
                    }
                } else {
                    tokens
                };
                field_reads.push(tokens);

                idents.push(ident);
                continue;
            }
            unimplemented!(
                "`#[grib_template(len = N)]` is only available for `Vec<T>` and `Option<Vec<T>>`"
            );
        }

        let disc_attr = field
            .attrs
            .iter()
            .find_map(|attr| attr_value(attr, "variant").map(|v| parse_variant_attr(&v)));
        if let Some(disc_ident) = disc_attr {
            field_reads.push(quote! {
                let #ident = <#ty as grib_template_helpers::TryEnumFromSlice>::try_enum_from_slice(
                    #disc_ident,
                    slice,
                    pos,
                )?;
            });
            idents.push(ident);
            continue;
        }

        field_reads.push(quote! {
            let #ident = <#ty as grib_template_helpers::TryFromSlice>::try_from_slice(slice, pos)?;
        });
        idents.push(ident);
    }

    quote! {
        impl #impl_generics grib_template_helpers::TryFromSlice for #name #type_generics #where_clause {
            fn try_from_slice(
                slice: &[u8],
                pos: &mut usize,
            ) -> grib_template_helpers::TryFromSliceResult<Self> {
                #(#field_reads)*
                Ok(Self { #(#idents),* })
            }
        }
    }
}

fn impl_try_from_slice_for_enum(
    input: &syn::DeriveInput,
    data: &syn::DataEnum,
) -> proc_macro2::TokenStream {
    let name = &input.ident;
    let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();

    let mut arms = Vec::new();

    for variant in &data.variants {
        let variant_ident = &variant.ident;
        let disc_expr = variant
            .discriminant
            .as_ref()
            .expect("`TryFromSlice` requires the enum to have explicit discriminant")
            .1
            .clone();

        if let syn::Fields::Unnamed(fields) = &variant.fields
            && fields.unnamed.len() == 1
        {
            let inner_ty = &fields.unnamed.first().unwrap().ty;
            arms.push(quote! {
                #disc_expr => {
                    let inner = <#inner_ty as grib_template_helpers::TryFromSlice>::try_from_slice(
                        slice,
                        pos
                    )?;
                    Ok(#name::#variant_ident(inner))
                }
            });
        } else {
            unimplemented!("`TryFromSlice` only supports single-field tuple variants");
        }
    }

    quote! {
        impl #impl_generics grib_template_helpers::TryEnumFromSlice for #name #type_generics #where_clause {
            fn try_enum_from_slice(
                discriminant: impl Into<u64>,
                slice: &[u8],
                pos: &mut usize,
            ) -> grib_template_helpers::TryFromSliceResult<Self> {
                match discriminant.into() {
                    #(#arms),*,
                    _ => panic!("unknown variant for {}", stringify!(#name)),
                }
            }
        }
    }
}

enum LenKind {
    Literal(usize),
    Ident(syn::Ident),
}

impl quote::ToTokens for LenKind {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        match self {
            LenKind::Literal(n) => {
                tokens.extend(quote! { #n });
            }
            LenKind::Ident(ident) => {
                tokens.extend(quote! { #ident as usize });
            }
        }
    }
}

fn attr_value(attr: &syn::Attribute, ident: &str) -> Option<syn::Expr> {
    if !attr.path().is_ident("grib_template") {
        return None;
    }
    let meta = attr.parse_args::<syn::Meta>().ok()?;
    if let syn::Meta::NameValue(nv) = meta {
        if !nv.path.is_ident(ident) {
            return None;
        }
        Some(nv.value)
    } else {
        None
    }
}

fn parse_len_attr(attr_value: &syn::Expr) -> Option<LenKind> {
    match attr_value {
        syn::Expr::Lit(syn::ExprLit {
            lit: syn::Lit::Int(lit_int),
            ..
        }) => Some(LenKind::Literal(lit_int.base10_parse::<usize>().unwrap())),
        syn::Expr::Lit(syn::ExprLit {
            lit: syn::Lit::Str(lit_str),
            ..
        }) => Some(LenKind::Ident(syn::Ident::new(
            &lit_str.value(),
            lit_str.span(),
        ))),
        _ => None,
    }
}

fn parse_variant_attr(attr_value: &syn::Expr) -> Option<syn::Ident> {
    match attr_value {
        syn::Expr::Lit(syn::ExprLit {
            lit: syn::Lit::Str(lit_str),
            ..
        }) => Some(syn::Ident::new(&lit_str.value(), lit_str.span())),
        _ => None,
    }
}

fn extract_vec_inner(type_path: &syn::TypePath) -> Option<(syn::Type, bool)> {
    if type_path.path.segments.len() == 1 {
        let (type_path, has_option) = if type_path.path.segments[0].ident == "Option"
            && let syn::PathArguments::AngleBracketed(ref args) =
                type_path.path.segments[0].arguments
            && let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first()
            && let syn::Type::Path(type_path) = inner_ty
        {
            (type_path, true)
        } else {
            (type_path, false)
        };

        if type_path.path.segments[0].ident == "Vec"
            && let syn::PathArguments::AngleBracketed(ref args) =
                type_path.path.segments[0].arguments
            && let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first()
        {
            return Some((inner_ty.clone(), has_option));
        }
    }
    None
}

/// Derive macro generating an impl of the trait `grib_template_helpers::Dump`.
#[proc_macro_derive(Dump)]
pub fn derive_dump(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as syn::DeriveInput);

    match &input.data {
        syn::Data::Struct(data) => impl_dump_for_struct(&input, data),
        syn::Data::Enum(data) => impl_dump_for_enum(&input, data),
        _ => unimplemented!("`Dump` can only be derived for structs/enums"),
    }
    .into()
}

fn impl_dump_for_struct(
    input: &syn::DeriveInput,
    data: &syn::DataStruct,
) -> proc_macro2::TokenStream {
    let name = &input.ident;
    let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
    let Some((kind, fields)) = extract_struct_info(data) else {
        unimplemented!(
            "`Dump` can only be derived for structs with named fields or with a single unnamed `u8` field"
        )
    };

    if kind == StructKind::TupleStruct {
        let doc = get_doc(&fields[0].attrs)
            .map(|s| format!("  // {}", s.trim()))
            .unwrap_or_default();

        return quote! {
            impl #impl_generics grib_template_helpers::Dump for #name #type_generics #where_clause {
                fn dump<W: std::io::Write>(
                    &self,
                    parent: Option<&std::borrow::Cow<str>>,
                    pos: &mut usize,
                    output: &mut W,
                ) -> Result<(), std::io::Error> {
                    let size = 1;
                    grib_template_helpers::write_position_column(output, pos, size)?;
                    if let Some(parent) = parent {
                        write!(output, "{}", parent)?;
                    }
                    writeln!(output, " = {:#010b}{}",
                        self.0,
                        #doc,
                    )
                }
            }
        };
    }

    let mut dumps = Vec::new();

    for field in fields {
        let ident = field.ident.as_ref().unwrap();
        let ty = &field.ty;

        let doc = get_doc(&field.attrs)
            .map(|s| format!("  // {}", s.trim()))
            .unwrap_or_default();
        dumps.push(quote! {
            <#ty as grib_template_helpers::DumpField>::dump_field(
                &self.#ident,
                stringify!(#ident),
                parent,
                #doc,
                pos,
                output,
            )?;
        });
    }

    quote! {
        impl #impl_generics grib_template_helpers::Dump for #name #type_generics #where_clause {
            fn dump<W: std::io::Write>(
                &self,
                parent: Option<&std::borrow::Cow<str>>,
                pos: &mut usize,
                output: &mut W,
            ) -> Result<(), std::io::Error> {
                #(#dumps)*;
                Ok(())
            }
        }
    }
}

fn impl_dump_for_enum(input: &syn::DeriveInput, data: &syn::DataEnum) -> proc_macro2::TokenStream {
    let name = &input.ident;
    let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();

    let mut arms = Vec::new();

    for variant in &data.variants {
        let variant_ident = &variant.ident;

        if let syn::Fields::Unnamed(fields) = &variant.fields
            && fields.unnamed.len() == 1
        {
            let inner_ty = &fields.unnamed.first().unwrap().ty;
            arms.push(quote! {
                #name::#variant_ident(inner) => <#inner_ty as grib_template_helpers::Dump>::dump(
                    inner,
                    parent,
                    pos,
                    output
                )
            });
        } else {
            unimplemented!("`Dump` only supports single-field tuple variants");
        }
    }

    quote! {
        impl #impl_generics grib_template_helpers::Dump for #name #type_generics #where_clause {
            fn dump<W: std::io::Write>(
                &self,
                parent: Option<&std::borrow::Cow<str>>,
                pos: &mut usize,
                output: &mut W,
            ) -> Result<(), std::io::Error> {
                match self {
                    #(#arms),*,
                }
            }
        }
    }
}

fn get_doc(attrs: &[syn::Attribute]) -> Option<String> {
    let mut doc = String::new();
    for attr in attrs.iter() {
        match attr.meta {
            syn::Meta::NameValue(ref value) if value.path.is_ident("doc") => {
                if let syn::Expr::Lit(lit) = &value.value
                    && let syn::Lit::Str(s) = &lit.lit
                {
                    doc.push_str(&s.value());
                }
            }
            _ => {}
        }
    }
    if doc.is_empty() { None } else { Some(doc) }
}

fn extract_struct_info(
    data: &syn::DataStruct,
) -> Option<(
    StructKind,
    &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>,
)> {
    match &data.fields {
        syn::Fields::Named(fields) => Some((StructKind::NamedStruct, &fields.named)),
        syn::Fields::Unnamed(fields) => {
            let fields = &fields.unnamed;
            if fields.len() == 1 && is_type_u8(&fields.first().unwrap().ty) {
                Some((StructKind::TupleStruct, fields))
            } else {
                None
            }
        }
        _ => None,
    }
}

#[derive(PartialEq)]
enum StructKind {
    TupleStruct,
    NamedStruct,
}

fn is_type_u8(ty: &syn::Type) -> bool {
    if let syn::Type::Path(syn::TypePath { path, .. }) = ty
        && let Some(segment) = path.segments.last()
    {
        return segment.ident == "u8";
    }
    false
}