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
use proc_macro::TokenStream;
use quote::quote;
use syn::{
    bracketed, parenthesized, parse::Parse, parse_macro_input, parse_quote,
};

#[proc_macro_attribute]
pub fn model(_: TokenStream, input: TokenStream) -> TokenStream {
    let item = parse_macro_input!(input as syn::ItemFn);
    let inputs = item.clone().sig.inputs;

    let args: Vec<Argument> =
        inputs.iter().map(|inp| parse_quote!(#inp)).collect();

    let mut parameter_extraction = Vec::new();

    let mut min_checks = Vec::new();
    let mut max_checks = Vec::new();
    for arg in args {
        let ident = arg.ident;
        let ty = arg.ty;
        if let Some(attr) = arg.attr {
            if let Some(default) = attr.get_default() {
                let def = default.val;
                parameter_extraction.push(quote! {
                    let #ident: #ty = args.get(stringify!(#ident))
                            .map(|arg| arg.parse().unwrap())
                            .unwrap_or(#def);
                });
            } else {
                parameter_extraction.push(quote! {
                let #ident: #ty = args.get(stringify!(#ident))
                        .map(|arg| arg.parse().unwrap())
                        .expect(format!("A value for `{}` has to be provided since no default is specified",stringify!(#ident)).as_str());
            });
            }

            if let Some(minimum) = attr.get_minimum() {
                let min = minimum.val;
                min_checks.push(quote! {
                if #ident < #min {
                    panic!("Value of `{}` must not be smaller than: {}",stringify!(#ident), #min);
                }
            });
            }
            if let Some(maximum) = attr.get_maximum() {
                let max = maximum.val;
                max_checks.push(quote! {
                if #ident > #max {
                    panic!("Value of `{}` must not be larger than: {}", stringify!(#ident), #max);
                }
            })
            }
        } else {
            parameter_extraction.push(quote! {
                let #ident: #ty = args.get(stringify!(#ident))
                        .map(|arg| arg.parse().unwrap())
                        .expect(format!("A value for `{}` has to be provided since no default is specified",stringify!(#ident)).as_str());
            });
        }
    }
    let block = item.block;

    let function_boilerplate = quote! {
        #[no_mangle]
            pub extern "C" fn model(
                args: &std::collections::HashMap<String, String>
            ) -> fj::Shape
    };

    quote! {
    #function_boilerplate {
        #(
            #parameter_extraction
        )*
        #(
            #min_checks
        )*
        #(
            #max_checks
        )*
        #block
    }
    }
    .into()
}

/// Represents one parameter given to the `model`
/// `#[param(default=3, min=4)] num_points: u64`
/// `^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~~~~  ^^^-- ty`
/// `           |                    |`
/// `         attr                 ident`
#[derive(Debug, Clone)]
struct Argument {
    pub attr: Option<HelperAttribute>,
    pub ident: proc_macro2::Ident,
    pub ty: proc_macro2::Ident,
}

impl Parse for Argument {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut attr = None;
        if input.peek(syn::token::Pound) {
            attr = Some(input.parse()?);
        }
        let ident: proc_macro2::Ident = input.parse()?;

        let _: syn::token::Colon = input.parse()?;

        let ty: proc_macro2::Ident = input.parse()?;
        Ok(Self { attr, ident, ty })
    }
}

/// Represents all arguments given to the `#[param]` attribute eg:
/// `#[param(default=3, min=4)]`
/// `        ^^^^^^^^^^^^^^^^`
#[derive(Debug, Clone)]
struct HelperAttribute {
    pub param:
        Option<syn::punctuated::Punctuated<DefaultParam, syn::Token![,]>>,
}

impl Parse for HelperAttribute {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let attr_content;
        let param_content;
        let _: syn::token::Pound = input.parse()?;
        bracketed!(attr_content in input);
        let ident: proc_macro2::Ident = attr_content.parse()?;
        if ident != *"param" {
            return Err(syn::Error::new_spanned(
                ident.clone(),
                format!(
                    "Unknown attribute \"{}\" found, expected \"param\"",
                    ident
                ),
            ));
        }

        if attr_content.peek(syn::token::Paren) {
            parenthesized!(param_content in attr_content);
            if param_content.is_empty() {
                Ok(Self { param: None })
            } else {
                Ok(Self {
                param: Some(
                    syn::punctuated::Punctuated::parse_separated_nonempty_with(
                        &param_content,
                        DefaultParam::parse,
                    )?,
                ),
            })
            }
        } else {
            Ok(Self { param: None })
        }
    }
}

impl HelperAttribute {
    fn get_parameter(&self, parameter_name: &str) -> Option<DefaultParam> {
        if let Some(values) = self.param.clone() {
            values.into_iter().find(|val| val.ident == *parameter_name)
        } else {
            None
        }
    }

    pub fn get_default(&self) -> Option<DefaultParam> {
        self.get_parameter("default")
    }

    pub fn get_minimum(&self) -> Option<DefaultParam> {
        self.get_parameter("min")
    }

    pub fn get_maximum(&self) -> Option<DefaultParam> {
        self.get_parameter("max")
    }
}

/// Represents one argument given to the `#[param]` attribute eg:
/// `#[param(default=3)]`
/// `        ^^^^^^^^^----- is parsed as DefaultParam{ ident: Some(default), val: 3 }`
#[derive(Debug, Clone)]
struct DefaultParam {
    pub ident: proc_macro2::Ident,
    pub val: syn::Expr,
}

impl Parse for DefaultParam {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        if input.peek(syn::Ident) {
            let ident: proc_macro2::Ident = input.parse()?;
            let _: syn::token::Eq = input.parse()?;
            Ok(Self {
                ident,
                val: input.parse()?,
            })
        } else {
            Err(input
                .parse::<proc_macro2::Ident>()
                .expect_err("No identifier found"))
        }
    }
}