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
#![warn(clippy::all, clippy::cargo)]

extern crate proc_macro;

mod construction;
mod extraction;
mod identifiers;

use self::construction::build_struct_constructor;
use self::construction::build_variant_constructor;
use self::extraction::build_extractors;
use self::identifiers::build_variant_parser_ident;
use self::identifiers::build_variant_parser_idents;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
use syn::parse_quote;
use syn::Ident;
use syn::Item;
use syn::ItemEnum;
use syn::ItemFn;
use syn::ItemImpl;
use syn::ItemStruct;
use syn::Variant;

/// The derive macro that is used to generate the parsing logic for a struct
/// representing the parameters for an attribute.
#[proc_macro_derive(AttrArgs)]
pub fn attr_args(input: TokenStream) -> TokenStream {
    let output = match parse_macro_input!(input as Item) {
        Item::Struct(input_struct) => {
            let impl_item = impl_parse_for_struct(&input_struct);
            quote! { #impl_item }
        }
        Item::Enum(input_enum) => {
            let impl_item = impl_parse_for_enum(&input_enum);
            quote! { #impl_item }
        }
        _ => panic!("The attribute can only be applied to structs and enums"),
    };

    output.into()
}

/// Creates the impl body for a tagged struct
fn impl_parse_for_struct(input_struct: &ItemStruct) -> ItemImpl {
    let struct_name = &input_struct.ident;

    // Build the statements that pull out the field values from the Parameters
    let field_extractors = build_extractors(&input_struct.fields);

    // Build the statement that constructs the struct
    let struct_return = build_struct_constructor(&input_struct);

    parse_quote! {
        impl syn::parse::Parse for #struct_name {
            fn parse(buffer: &syn::parse::ParseBuffer) -> syn::parse::Result<Self> {
                let mut attr_args = <attribution::Parameters as syn::parse::Parse>::parse(buffer)?;

                #(#field_extractors)*

                #struct_return
            }
        }
    }
}

/// Creates the impl body for a tagged enum
fn impl_parse_for_enum(input_enum: &ItemEnum) -> ItemImpl {
    let enum_name = &input_enum.ident;
    let parser_idents = build_variant_parser_idents(&input_enum);

    // Builds a function used to try to parse each variant of the enum
    let parser_decls = input_enum
        .variants
        .iter()
        .map(|variant| build_variant_parser(&input_enum.ident, variant));

    parse_quote! {

        impl syn::parse::Parse for #enum_name {
            fn parse(buffer: &syn::parse::ParseBuffer) -> syn::parse::Result<Self> {
                #(#parser_decls)*

                // Groups the parsers together
                let parsers = [#(#parser_idents),*];

                // Find the first parser that matches the input.
                let parse_result = parsers.iter()
                    .map(|func| func(buffer))
                    .filter(Result::is_ok)
                    .next();

                // Return the parsed data or an error stating that it could
                // not be parsed.
                parse_result.unwrap_or_else(|| {
                    let span = proc_macro2::Span::call_site();
                    Err(syn::parse::Error::new(span, "No matching variant found."))
                })
            }
        }
    }
}

/// Constructs a function that will attempt to parse a token stream as a
/// provided enum variant.
fn build_variant_parser(enum_name: &Ident, variant: &Variant) -> ItemFn {
    let parser_ident = build_variant_parser_ident(variant);

    let extractors = build_extractors(&variant.fields);
    let constructor = build_variant_constructor(enum_name, variant);

    parse_quote! {
        #[allow(non_snake_case)]
        fn #parser_ident(buffer: &syn::parse::ParseBuffer) -> syn::parse::Result<#enum_name> {
            let mut attr_args = <attribution::Parameters as syn::parse::Parse>::parse(buffer)?;

            #(#extractors)*

            #constructor
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use syn::parse_quote;

    #[test]
    fn impl_parse_for_struct_unnamed_struct_test() {
        let input_struct: ItemStruct = parse_quote! {
            struct Foo(u64, u64);
        };

        let actual = impl_parse_for_struct(&input_struct);
        let expected: ItemImpl = parse_quote! {
            impl syn::parse::Parse for Foo {
                fn parse(buffer: &syn::parse::ParseBuffer) -> syn::parse::Result<Self> {
                    let mut attr_args = <attribution::Parameters as syn::parse::Parse>::parse(buffer)?;

                    let _0 = attribution::FromParameters::from_parameters(&mut attr_args, &0usize.into())?;
                    let _1 = attribution::FromParameters::from_parameters(&mut attr_args, &1usize.into())?;

                    Ok(Foo(_0, _1))
                }
            }
        };

        assert_eq!(
            expected, actual,
            "Expected: `{:#?}`\nActual: `{:#?}`",
            expected, actual
        );
    }
}