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
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DataStruct, DeriveInput, Error, Fields, Ident};

type Result<T> = std::result::Result<T, Error>;

macro_rules! unwrap_or_compile_error {
    ($e:expr) => {
        match $e {
            Ok(x) => x,
            Err(e) => return e.into_compile_error().into(),
        }
    };
}

#[proc_macro_derive(AsTracer, attributes(skip_tracer))]
pub fn generate_as_tracer_function(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let data = unwrap_or_compile_error!(get_struct_data(&input));
    let skipped_fields = unwrap_or_compile_error!(check_all_fields(data));
    let field_names = unwrap_or_compile_error!(get_field_names(data, name));
    unwrap_or_compile_error!(check_num_fields(&field_names, name, 10));

    let mut match_arms = Vec::new();
    let match_more_fields = if skipped_fields {
        quote!(, ..)
    } else {
        quote!()
    };

    for mask in 0..(1 << field_names.len()) {
        let mut this_combination = Vec::new();
        let mut tuple_elements = Vec::new();
        for (index, field) in field_names.iter().enumerate() {
            if (mask >> index) & 1 == 1 {
                this_combination.push(quote! { #field: Some(#field) });
                tuple_elements.push(quote! { #field });
            } else {
                this_combination.push(quote! { #field: None });
            }
        }
        let match_arm = match tuple_elements.len() {
            0 => quote! {
                #name {#(#this_combination),* #match_more_fields} => Box::new(()) // as Box<dyn MyTrait>
            },
            1 => quote! {
                #name {#(#this_combination),* #match_more_fields} => Box::new(#(#tuple_elements),*) // as Box<dyn MyTrait>
            },
            _ => quote! {
                #name {#(#this_combination),* #match_more_fields} => Box::new((#(#tuple_elements),*)) // as Box<dyn MyTrait>
            },
        };
        match_arms.push(match_arm);
    }

    let expanded = quote! {
        impl AsTracer for #name {
            fn as_tracer<'a>(&'a mut self) -> Box<dyn 'a + TracerTrait> {
                match self {
                    #(#match_arms,)*
                }
            }
        }
    };

    expanded.into()
}

#[proc_macro_derive(DrainTrace)]
pub fn generate_drain_trace_function(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let data = unwrap_or_compile_error!(get_struct_data(&input));
    let field_names = unwrap_or_compile_error!(get_field_names(data, name));

    let drain_statements = field_names.iter().map(|field| {
        quote! { self.#field.drain_trace(map); }
    });

    let expanded = quote! {
        impl DrainTrace for #name {
            fn drain_trace(self, map: &mut typemap::ShareDebugMap) {
                #(#drain_statements)*
            }
        }
    };

    expanded.into()
}

fn get_struct_data(input: &DeriveInput) -> Result<&DataStruct> {
    match &input.data {
        Data::Struct(data) => Ok(data),
        _ => Err(Error::new_spanned(&input.ident, "Only struct is supported")),
    }
}

fn check_all_fields(data: &DataStruct) -> Result<bool> {
    let mut type_error = vec![];
    let mut skipped_field = false;
    for field in &data.fields {
        if field
            .attrs
            .iter()
            .any(|attr| attr.path.is_ident("skip_tracer"))
        {
            skipped_field = true;
            continue;
        }

        if let syn::Type::Path(type_path) = &field.ty {
            if type_path
                .path
                .segments
                .last()
                .map_or(false, |seg| seg.ident == "Option")
            {
                continue;
            }
        }
        type_error.push(Error::new_spanned(
            &field.ty,
            "All fields must be of type Option",
        ));
    }
    if !type_error.is_empty() {
        let mut type_error_iter = type_error.into_iter();
        let mut error = type_error_iter.next().unwrap();
        error.extend(type_error_iter);
        Err(error)
    } else {
        Ok(skipped_field)
    }
}

fn get_field_names<'a>(
    data: &'a DataStruct, name: &Ident,
) -> Result<Vec<&'a Option<Ident>>> {
    match &data.fields {
        Fields::Named(fields) => Ok(fields
            .named
            .iter()
            .filter(|f| {
                !f.attrs.iter().any(|attr| attr.path.is_ident("skip_tracer"))
            })
            .map(|f| &f.ident)
            .collect::<Vec<_>>()),
        _ => Err(Error::new_spanned(&name, "Only named struct is supported")),
    }
}

fn check_num_fields(
    field_names: &Vec<&Option<Ident>>, name: &Ident, max_entries: usize,
) -> Result<()> {
    if field_names.len() > max_entries {
        Err(Error::new_spanned(
            name,
            "Too many fields in the struct! Limit is 10.",
        ))
    } else {
        Ok(())
    }
}