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
//! Algorithmia entrypoint for authoring Rust-based algorithms
//!
//! This module contains the traits that are used for defining the entrypoint
//! of a rust-based algorithm on the Algorithmia platform.
//!
//! Rust algorithms are loaded by instantiating an `Algo` type via `Algo::default()`
//! and each API request invokes the `apply` method on that instance.
//! While it is possible to manually define the `Algo` type and implement
//! either `EntryPoint` or `DecodedEntryPoint`, the `#[entrypoint]`
//! attribute will generate these implementations for you based on the signature
//! of the function (or impl) that it is attached to.
//!
//! ## `#[entrypoint]` Usage
//!
//! Just annotate a function with `#[entrypoint]`
//!
//! ```no_compile
//! #[entrypoint]
//! fn apply(name: String) -> Result<String, String> {
//! Ok(format!("Hello, {}.", name))
//! }
//! ```
//!
//! This will generate the algorithm entrypoint as long the function
//! signature specifices supported input and output types:
//!
//! **Supported input types**:
//! - String types (e.g. `&str` and `String`)
//! - Byte types (e.g. `&[u8]` and `Vec<u8>`)
//! - Json `&Value` type
//! - `AlgoIo` enum type (for matching on text, json, and binary input)
//! - Any type that implements `serde::Deserialize` (e.g. `#[derive(Deserialize)]`)
//!
//! **Supported output (`Ok` variant of return value)**:
//! - `String`, `Vec<u8>`, `Value`, `AlgoIo`
//! - Any type that implements `serde::Serialize` (e.g. `#[derive(Serialize)]`)
//!
//! **Supported error types (`Err` variant of return value)**:
//! - Any type with a conversion to `Box<std::error::Error>`. This includes `String` and basically any type that implements the `Error` trait.
//!
//!
//! ## Automatic JSON serialization/deserialization
//!
//! Since `#[entrypoint]` supports `Deserialize` input and `Serialize` output, this example will
//! accept a JSON array and return a JSON number:
//!
//! ```no_compile
//! #[entrypoint]
//! fn start(names: Vec<String>) -> Result<usize, String> {
//! Ok(name.len())
//! }
//! ```
//!
//! To use your own custom types as input and output, simply implement `Deserialize` and
//! `Serialize` respectively.
//!
//! ```no_compile
//! #[derive(Deserialize)
//! struct Input { titles: Vec<String> }
//!
//! #[derive(Serialize)
//! struct Output { count: u32 }
//!
//! #[entrypoint]
//! fn start(input: Input) -> Result<Output, String> {
//! Ok(Output{ count: input.titles.len() })
//! }
//! ```
//!
//! ## Preloading (advanced usage)
//!
//! If your algorithm has a preload step that doesn't vary with user input (e.g. loading a model),
//! you can create a type that implements `Default`, and use a method on that type as your
//! entrypoint (the `#[entrypoint]` annotation goes on the impl of your type. Multiple API calls in
//! succession from a single user will only instantiate the type once, but call `apply` multiple
//! times:
//!
//! ```no_compile
//! #[derive(Deserialize)
//! struct Input { titles: Vec<String>, max: u32 }
//!
//! #[derive(Serialize)
//! struct Output { titles: Vec<String> }
//!
//! struct App { model: Vec<u8> }
//!
//! #[entrypoint]
//! impl App {
//! fn apply(&mut self, input: Input) -> Result<Output, String> {
//! unimplemented!();
//! }
//! }
//!
//! impl Default for App {
//! fn default() -> Self {
//! App { model: load_model() }
//! }
//! }
//! ```
use crateAlgoIo;
use crate;
use serde_json;
use Error as StdError;
use DeserializeOwned;
use Value;
pub use entrypoint;
/// Alternate implementation for `EntryPoint`
/// that automatically decodes JSON input to the associate type
///
/// # Examples
/// ```no_run
/// # use algorithmia::prelude::*;
/// # use std::error::Error;
/// # #[derive(Default)]
/// # struct Algo;
/// impl DecodedEntryPoint for Algo {
/// // Expect input to be an array of 2 strings
/// type Input = (String, String);
/// fn apply_decoded(&mut self, input: Self::Input) -> Result<AlgoIo, Box<Error>> {
/// let msg = format!("{} - {}", input.0, input.1);
/// Ok(msg.into())
/// }
/// }
/// ```
/// Implementing an algorithm involves overriding at least one of these methods