serde-feather-macros 0.1.0

Proc-macro scaffold crate for serde-feather
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#![forbid(unsafe_code)]

//! Proc-macro derive implementation for `serde-feather`.

use std::collections::HashMap;

use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote};
use syn::{
    ext::IdentExt, parse_macro_input, spanned::Spanned, Attribute, Data, DeriveInput, Field,
    Fields, Ident, LitStr,
};

#[proc_macro_derive(FeatherSerialize, attributes(serde))]
pub fn derive_feather_serialize(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match expand_serialize(&input) {
        Ok(output) => output.into(),
        Err(error) => error.into_compile_error().into(),
    }
}

#[proc_macro_derive(FeatherDeserialize, attributes(serde))]
pub fn derive_feather_deserialize(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match expand_deserialize(&input) {
        Ok(output) => output.into(),
        Err(error) => error.into_compile_error().into(),
    }
}

struct ContainerAttrOptions {
    rename: Option<LitStr>,
}

#[derive(Default)]
struct FieldAttrOptions {
    rename: Option<LitStr>,
    default: bool,
    skip_serializing: bool,
    skip_deserializing: bool,
}

struct ParsedField {
    ident: Ident,
    ty: syn::Type,
    serialized_name: LitStr,
    default: bool,
    skip_serializing: bool,
    skip_deserializing: bool,
}

struct ParsedStruct {
    ident: Ident,
    struct_name: LitStr,
    fields: Vec<ParsedField>,
}

#[derive(Clone, Copy)]
enum WireDirection {
    Serialize,
    Deserialize,
}

impl WireDirection {
    fn includes(self, field: &ParsedField) -> bool {
        match self {
            Self::Serialize => !field.skip_serializing,
            Self::Deserialize => !field.skip_deserializing,
        }
    }

    fn name(self) -> &'static str {
        match self {
            Self::Serialize => "serialization",
            Self::Deserialize => "deserialization",
        }
    }
}

fn expand_serialize(input: &DeriveInput) -> syn::Result<TokenStream2> {
    let parsed = parse_input(input, "FeatherSerialize")?;
    let crate_path = serde_feather_path();

    let included_fields: Vec<&ParsedField> = parsed
        .fields
        .iter()
        .filter(|field| !field.skip_serializing)
        .collect();

    let field_count = included_fields.len();
    let serialize_fields = included_fields.into_iter().map(|field| {
        let field_ident = &field.ident;
        let field_name = &field.serialized_name;
        quote! {
            #crate_path::serde::ser::SerializeStruct::serialize_field(
                &mut state,
                #field_name,
                &self.#field_ident,
            )?;
        }
    });

    let struct_ident = &parsed.ident;
    let struct_name = &parsed.struct_name;

    Ok(quote! {
        impl #crate_path::serde::ser::Serialize for #struct_ident {
            fn serialize<S>(
                &self,
                serializer: S,
            ) -> ::core::result::Result<S::Ok, S::Error>
            where
                S: #crate_path::serde::ser::Serializer,
            {
                let mut state = #crate_path::serde::ser::Serializer::serialize_struct(
                    serializer,
                    #struct_name,
                    #field_count,
                )?;
                #(#serialize_fields)*
                #crate_path::serde::ser::SerializeStruct::end(state)
            }
        }
    })
}

fn expand_deserialize(input: &DeriveInput) -> syn::Result<TokenStream2> {
    let parsed = parse_input(input, "FeatherDeserialize")?;
    let crate_path = serde_feather_path();

    struct DeserBinding {
        field_index: usize,
        binding_ident: Ident,
        field_name: LitStr,
        field_ty: syn::Type,
        default: bool,
    }

    let mut bindings = Vec::<DeserBinding>::new();
    for (index, field) in parsed.fields.iter().enumerate() {
        if field.skip_deserializing {
            continue;
        }

        bindings.push(DeserBinding {
            field_index: index,
            binding_ident: format_ident!("__feather_field_{index}"),
            field_name: field.serialized_name.clone(),
            field_ty: field.ty.clone(),
            default: field.default,
        });
    }

    let field_bindings: Vec<TokenStream2> = bindings
        .iter()
        .map(|binding| {
            let binding_ident = &binding.binding_ident;
            let field_ty = &binding.field_ty;
            quote! { let mut #binding_ident: ::core::option::Option<#field_ty> = ::core::option::Option::None; }
        })
        .collect();
    let field_bindings_in_map = field_bindings.clone();
    let field_bindings_in_seq = field_bindings;

    let field_setter_match_arms = bindings.iter().enumerate().map(|(binding_index, binding)| {
        let field_index = binding_index;
        let binding_ident = &binding.binding_ident;
        let field_name = &binding.field_name;
        let field_ty = &binding.field_ty;
        quote! {
            #field_index => {
                if #binding_ident.is_some() {
                    return ::core::result::Result::Err(
                        #crate_path::serde::de::Error::duplicate_field(#field_name),
                    );
                }
                #binding_ident = ::core::option::Option::Some(#crate_path::serde::de::MapAccess::next_value::<#field_ty>(&mut map)?);
            }
        }
    });

    let known_fields: Vec<LitStr> = bindings
        .iter()
        .map(|binding| binding.field_name.clone())
        .collect();
    let known_fields_in_map = known_fields.clone();

    let construct_fields: Vec<TokenStream2> = parsed
        .fields
        .iter()
        .enumerate()
        .map(|(index, field)| {
            let field_ident = &field.ident;
            let field_name = &field.serialized_name;
            if field.skip_deserializing {
                return quote! {
                    #field_ident: ::core::default::Default::default()
                };
            }

            let binding_ident = bindings
                .iter()
                .find(|binding| binding.field_index == index)
                .expect("binding for non-skipped field")
                .binding_ident
                .clone();

            if field.default {
                quote! {
                    #field_ident: #binding_ident.unwrap_or_default()
                }
            } else {
                quote! {
                    #field_ident: match #binding_ident {
                        ::core::option::Option::Some(value) => value,
                        ::core::option::Option::None => {
                            return ::core::result::Result::Err(
                                #crate_path::serde::de::Error::missing_field(#field_name),
                            );
                        }
                    }
                }
            }
        })
        .collect();
    let construct_fields_in_map = construct_fields.clone();
    let construct_fields_in_seq = construct_fields;

    let seq_field_decode_steps = bindings.iter().enumerate().map(|(seq_index, binding)| {
        let binding_ident = &binding.binding_ident;
        let field_ty = &binding.field_ty;
        if binding.default {
            quote! {
                if let ::core::option::Option::Some(value) =
                    #crate_path::serde::de::SeqAccess::next_element::<#field_ty>(&mut seq)?
                {
                    #binding_ident = ::core::option::Option::Some(value);
                }
            }
        } else {
            quote! {
                #binding_ident =
                    match #crate_path::serde::de::SeqAccess::next_element::<#field_ty>(&mut seq)? {
                        ::core::option::Option::Some(value) => ::core::option::Option::Some(value),
                        ::core::option::Option::None => {
                            return ::core::result::Result::Err(
                                #crate_path::serde::de::Error::invalid_length(#seq_index, &self),
                            );
                        }
                    };
            }
        }
    });

    let seq_expected_len = bindings.len();

    let struct_ident = &parsed.ident;
    let struct_name = &parsed.struct_name;

    Ok(quote! {
        impl<'de> #crate_path::serde::de::Deserialize<'de> for #struct_ident {
            fn deserialize<D>(deserializer: D) -> ::core::result::Result<Self, D::Error>
            where
                D: #crate_path::serde::de::Deserializer<'de>,
            {
                struct __FeatherVisitor;

                impl<'de> #crate_path::serde::de::Visitor<'de> for __FeatherVisitor {
                    type Value = #struct_ident;

                    fn expecting(
                        &self,
                        formatter: &mut ::core::fmt::Formatter<'_>,
                    ) -> ::core::fmt::Result {
                        ::core::write!(formatter, "struct {}", #struct_name)
                    }

                    fn visit_map<V>(
                        self,
                        mut map: V,
                    ) -> ::core::result::Result<Self::Value, V::Error>
                    where
                        V: #crate_path::serde::de::MapAccess<'de>,
                    {
                        const __FEATHER_FIELDS: &[&str] = &[#(#known_fields_in_map),*];
                        #(#field_bindings_in_map)*
                        while let ::core::option::Option::Some(key) = #crate_path::serde::de::MapAccess::next_key::<#crate_path::__private::OwnedFieldName>(&mut map)?
                        {
                            match #crate_path::__private::select_field_index(key.as_str(), __FEATHER_FIELDS) {
                                ::core::option::Option::Some(index) => match index {
                                    #(#field_setter_match_arms)*
                                    _ => {
                                        let _: #crate_path::serde::de::IgnoredAny =
                                            #crate_path::serde::de::MapAccess::next_value(&mut map)?;
                                    }
                                },
                                ::core::option::Option::None => {
                                    let _: #crate_path::serde::de::IgnoredAny =
                                        #crate_path::serde::de::MapAccess::next_value(&mut map)?;
                                }
                            }
                        }

                        ::core::result::Result::Ok(#struct_ident {
                            #(#construct_fields_in_map,)*
                        })
                    }

                    fn visit_seq<V>(
                        self,
                        mut seq: V,
                    ) -> ::core::result::Result<Self::Value, V::Error>
                    where
                        V: #crate_path::serde::de::SeqAccess<'de>,
                    {
                        #(#field_bindings_in_seq)*
                        #(#seq_field_decode_steps)*

                        if #crate_path::serde::de::SeqAccess::next_element::<#crate_path::serde::de::IgnoredAny>(&mut seq)?.is_some() {
                            return ::core::result::Result::Err(
                                #crate_path::serde::de::Error::invalid_length(#seq_expected_len + 1, &self),
                            );
                        }

                        ::core::result::Result::Ok(#struct_ident {
                            #(#construct_fields_in_seq,)*
                        })
                    }
                }

                const __FEATHER_FIELDS: &[&str] = &[#(#known_fields),*];
                #crate_path::serde::de::Deserializer::deserialize_struct(
                    deserializer,
                    #struct_name,
                    __FEATHER_FIELDS,
                    __FeatherVisitor,
                )
            }
        }
    })
}

fn parse_input(input: &DeriveInput, macro_name: &str) -> syn::Result<ParsedStruct> {
    if !input.generics.params.is_empty() || input.generics.where_clause.is_some() {
        return Err(syn::Error::new_spanned(
            &input.generics,
            format!("{macro_name} only supports non-generic structs in this MVP"),
        ));
    }

    let container_options = parse_container_attributes(&input.attrs)?;
    let struct_name = container_options
        .rename
        .unwrap_or_else(|| LitStr::new(&input.ident.to_string(), input.ident.span()));

    let named_fields = match &input.data {
        Data::Struct(data_struct) => match &data_struct.fields {
            Fields::Named(fields) => &fields.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    &data_struct.fields,
                    format!("{macro_name} only supports structs with named fields in this MVP"),
                ))
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                &input.ident,
                format!("{macro_name} only supports structs in this MVP"),
            ))
        }
    };

    let mut parsed_fields = Vec::with_capacity(named_fields.len());
    for field in named_fields {
        parsed_fields.push(parse_field(field)?);
    }

    validate_unique_wire_field_names(&parsed_fields, WireDirection::Serialize)?;
    validate_unique_wire_field_names(&parsed_fields, WireDirection::Deserialize)?;

    Ok(ParsedStruct {
        ident: input.ident.clone(),
        struct_name,
        fields: parsed_fields,
    })
}

fn validate_unique_wire_field_names(
    parsed_fields: &[ParsedField],
    direction: WireDirection,
) -> syn::Result<()> {
    let mut seen_by_name: HashMap<String, String> = HashMap::new();

    for field in parsed_fields {
        if !direction.includes(field) {
            continue;
        }

        let wire_name = field.serialized_name.value();
        let current_field = field.ident.to_string();
        if let Some(previous_field) = seen_by_name.insert(wire_name.clone(), current_field) {
            return Err(syn::Error::new(
                field.serialized_name.span(),
                format!(
                    "duplicate wire field name `{wire_name}` in {}; conflicts with field \
                     `{previous_field}`",
                    direction.name()
                ),
            ));
        }
    }

    Ok(())
}

fn parse_container_attributes(attrs: &[Attribute]) -> syn::Result<ContainerAttrOptions> {
    let mut options = ContainerAttrOptions { rename: None };

    for attr in attrs {
        if !attr.path().is_ident("serde") {
            continue;
        }

        attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("rename") {
                let rename_value: LitStr = meta.value()?.parse()?;
                if options.rename.replace(rename_value).is_some() {
                    return Err(meta.error("duplicate serde container attribute `rename`"));
                }
                return Ok(());
            }

            Err(meta.error("unsupported serde container attribute; supported attributes: `rename`"))
        })?;
    }

    Ok(options)
}

fn parse_field(field: &Field) -> syn::Result<ParsedField> {
    let field_ident = field.ident.clone().ok_or_else(|| {
        syn::Error::new(
            field.span(),
            "Feather derives only support fields with identifiers",
        )
    })?;

    let mut options = FieldAttrOptions::default();

    for attr in &field.attrs {
        if !attr.path().is_ident("serde") {
            continue;
        }

        attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("rename") {
                let rename_value: LitStr = meta.value()?.parse()?;
                if options.rename.replace(rename_value).is_some() {
                    return Err(meta.error("duplicate serde field attribute `rename`"));
                }
                return Ok(());
            }

            if meta.path.is_ident("default") {
                ensure_flag_meta_has_no_value(&meta, "default")?;
                if options.default {
                    return Err(meta.error("duplicate serde field attribute `default`"));
                }
                options.default = true;
                return Ok(());
            }

            if meta.path.is_ident("skip") {
                ensure_flag_meta_has_no_value(&meta, "skip")?;
                if options.skip_serializing || options.skip_deserializing {
                    return Err(meta.error(
                        "serde field attribute `skip` conflicts with previously declared `skip`, \
                         `skip_serializing`, or `skip_deserializing`",
                    ));
                }
                options.skip_serializing = true;
                options.skip_deserializing = true;
                return Ok(());
            }

            if meta.path.is_ident("skip_serializing") {
                ensure_flag_meta_has_no_value(&meta, "skip_serializing")?;
                if options.skip_serializing {
                    return Err(meta.error("duplicate serde field attribute `skip_serializing`"));
                }
                if options.skip_deserializing {
                    return Err(meta.error(
                        "serde field attributes `skip_serializing` and `skip_deserializing` \
                         cannot be combined",
                    ));
                }
                options.skip_serializing = true;
                return Ok(());
            }

            if meta.path.is_ident("skip_deserializing") {
                ensure_flag_meta_has_no_value(&meta, "skip_deserializing")?;
                if options.skip_deserializing {
                    return Err(meta.error("duplicate serde field attribute `skip_deserializing`"));
                }
                if options.skip_serializing {
                    return Err(meta.error(
                        "serde field attributes `skip_serializing` and `skip_deserializing` \
                         cannot be combined",
                    ));
                }
                options.skip_deserializing = true;
                return Ok(());
            }

            Err(meta.error(
                "unsupported serde field attribute; supported attributes: `rename`, `default`, \
                 `skip`, `skip_serializing`, `skip_deserializing`",
            ))
        })?;
    }

    let serialized_name = options
        .rename
        .unwrap_or_else(|| LitStr::new(&field_ident.unraw().to_string(), field_ident.span()));

    Ok(ParsedField {
        ident: field_ident,
        ty: field.ty.clone(),
        serialized_name,
        default: options.default,
        skip_serializing: options.skip_serializing,
        skip_deserializing: options.skip_deserializing,
    })
}

fn ensure_flag_meta_has_no_value(
    meta: &syn::meta::ParseNestedMeta<'_>,
    name: &str,
) -> syn::Result<()> {
    if !meta.input.peek(syn::Token![=]) && !meta.input.peek(syn::token::Paren) {
        return Ok(());
    }

    Err(meta.error(format!(
        "serde field attribute `{name}` does not accept a value"
    )))
}

fn serde_feather_path() -> TokenStream2 {
    match crate_name("serde-feather") {
        Ok(FoundCrate::Itself) => quote!(crate),
        Ok(FoundCrate::Name(name)) => {
            let ident = Ident::new(&name.replace('-', "_"), Span::call_site());
            quote!(::#ident)
        }
        Err(_) => quote!(::serde_feather),
    }
}