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
#![recursion_limit = "1024"]

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use crate::proc_macro::TokenStream;
use quote::{ToTokens, Tokens};
use syn::*;

#[proc_macro_attribute]
pub fn entrypoint(_args: TokenStream, input: TokenStream) -> TokenStream {
    // let args_path = parse_path(&args.to_string()).ok();
    let item =
        parse_item(&input.to_string()).expect("#[entrypoint] must be attached to a function");

    let entrypoint = Entrypoint::from_item(item);
    entrypoint
        .output()
        .parse()
        .expect("#[entrypoint] failed to parse output")
}

struct Entrypoint {
    fn_name: Ident,
    self_type: Option<Ty>,
    input_type: Ty,
    item: Item,
}

impl Entrypoint {
    fn from_item(item: Item) -> Self {
        let item_clone = item.clone();
        match item.node {
            ItemKind::Fn(fn_decl, unsafety, _constness, _abi, generics, _block) => {
                assert_eq!(
                    unsafety,
                    Unsafety::Normal,
                    "Unsafe #[entrypoint] functions are not supported"
                );
                assert_generics_empty(&generics);

                assert_eq!(
                    fn_decl.inputs.len(),
                    1,
                    "#[entrypoint] function may only take one arg"
                );
                let input_type = match fn_decl.inputs[0] {
                    FnArg::SelfRef(..) | FnArg::SelfValue(..) => {
                        panic!("Attach #[entrypoint] to impl block to support &self arg")
                    }
                    FnArg::Captured(_, ref ty) => ty.clone(),
                    FnArg::Ignored(ref ty) => ty.clone(),
                };

                Entrypoint {
                    fn_name: item.ident,
                    self_type: None,
                    input_type: input_type,
                    item: item_clone,
                }
            }

            ItemKind::Impl(unsafety, _polarity, generics, _path, ty, impl_items) => {
                assert_eq!(
                    unsafety,
                    Unsafety::Normal,
                    "Unsafe #[entrypoint] impls are not supported"
                );
                assert_generics_empty(&generics);
                // TODO: assert that `ty` isn't of type `Algo` which would cause name collisions

                let method_name = Ident::new("apply");

                let fn_decl = impl_items
                    .iter()
                    .filter_map(|item| match item.node {
                        ImplItemKind::Method(ref sig, _) => Some((&item.ident, &sig.decl)),
                        _ => None,
                    })
                    .find(|pair| pair.0 == &method_name)
                    .map(|pair| pair.1.clone())
                    .expect("#[entrypoint] impl must include 'apply' method");

                assert_eq!(
                    fn_decl.inputs.len(),
                    2,
                    "#[entrypoint] within an impl take a reference to self and a single input arg"
                );
                match fn_decl.inputs[0] {
                    FnArg::SelfRef(..) => {}
                    _ => panic!(
                        "First argument of #[entrypoint] apply method must be a reference to self"
                    ),
                }

                let input_type = match fn_decl.inputs[1] {
                    FnArg::SelfRef(..) | FnArg::SelfValue(..) => {
                        panic!("Are you using self as a second argument?")
                    }
                    FnArg::Captured(_, ref ty) => ty.clone(),
                    FnArg::Ignored(ref ty) => ty.clone(),
                };

                // TODO: assert that fn_decl.output includes a `Result` token

                Entrypoint {
                    fn_name: method_name,
                    self_type: Some(ty.as_ref().clone()),
                    input_type: input_type,
                    item: item_clone,
                }
            }

            _ => panic!("#[entrypoint]` attribute must be attached to a function or impl"),
        }
    }

    fn output(&self) -> Tokens {
        let mut input_type_tokens = Tokens::new();
        self.input_type.to_tokens(&mut input_type_tokens);

        match input_type_tokens.as_str() {
            "& str" | "String" => self.impl_entrypoint(Ident::new("apply_str"), "&str"),
            "& [ u8 ]" | "Vec < u8 >" => self.impl_entrypoint(Ident::new("apply_bytes"), "&[u8]"),
            "& Value" => self.impl_entrypoint(Ident::new("apply_json"), "&Value"),
            "AlgoIo" => self.impl_entrypoint(Ident::new("apply"), "AlgoIo"),
            _ => self.impl_decoded_entrypoint(),
        }
    }

    fn impl_entrypoint(&self, apply_fn: Ident, input_type: &str) -> Tokens {
        let ref item = self.item;
        let ref fn_name = self.fn_name;
        let input_type = parse_type(input_type).unwrap();

        // We're auto-boxing the Ok variant of the Result output
        // until specialization lands because there is a conversion
        // Box<Serialize> but not impl Serialize
        match self.self_type {
            Some(ref self_type) => {
                quote! {
                    pub struct Algo(#self_type);
                    impl algorithmia::entrypoint::EntryPoint for Algo {
                        fn #apply_fn(&mut self, input: #input_type) -> ::std::result::Result<algorithmia::algo::AlgoIo, Box<::std::error::Error>> {
                            (self.0).#fn_name(input.into()).map(|out| algorithmia::algo::AlgoIo::from(Box::new(out))).map_err(|err| err.into())
                        }
                    }
                    impl Default for Algo {
                        fn default() -> Self {
                            Algo(Default::default())
                        }
                    }

                    #item
                }
            }
            None => {
                quote! {
                    #[derive(Default)] pub struct Algo;
                    impl algorithmia::entrypoint::EntryPoint for Algo {
                        fn #apply_fn(&mut self, input: #input_type) -> ::std::result::Result<algorithmia::algo::AlgoIo, Box<::std::error::Error>> {
                            #fn_name(input.into()).map(|out| algorithmia::algo::AlgoIo::from(Box::new(out))).map_err(|err| err.into())
                        }
                    }

                    #item
                }
            }
        }
    }

    fn impl_decoded_entrypoint(&self) -> Tokens {
        let ref fn_name = self.fn_name;
        let ref input_type = self.input_type;
        let ref item = self.item;

        match self.self_type {
            Some(ref self_type) => {
                quote! {
                    pub struct Algo(#self_type);
                    impl algorithmia::entrypoint::DecodedEntryPoint for Algo {
                        type Input = #input_type;
                        fn apply_decoded(&mut self, input: #input_type) -> ::std::result::Result<algorithmia::algo::AlgoIo, Box<::std::error::Error>> {
                            (self.0).#fn_name(input).map(|out| algorithmia::algo::AlgoIo::from(Box::new(out))).map_err(|err| err.into())
                        }
                    }

                    impl Default for Algo {
                        fn default() -> Self {
                            Algo(Default::default())
                        }
                    }

                    #item
                }
            }
            None => {
                quote! {
                    #[derive(Default)] pub struct Algo;
                    impl algorithmia::entrypoint::DecodedEntryPoint for Algo {
                        type Input = #input_type;
                        fn apply_decoded(&mut self, input: #input_type) -> ::std::result::Result<algorithmia::algo::AlgoIo, Box<::std::error::Error>> {
                            #fn_name(input).map(|out| algorithmia::algo::AlgoIo::from(Box::new(out))).map_err(|err| err.into())
                        }
                    }

                    #item
                }
            }
        }
    }
}

fn assert_generics_empty(generics: &Generics) {
    assert!(
        generics.lifetimes.is_empty()
            && generics.ty_params.is_empty()
            && generics.where_clause.predicates.is_empty(),
        "Generics are not supported on the #[algo_entrypoint] function"
    )
}