Skip to content

Instantly share code, notes, and snippets.

@anowell
Last active February 12, 2017 04:27
Show Gist options
  • Save anowell/41e254e85b8c6a3988a92f83bdb40172 to your computer and use it in GitHub Desktop.
Save anowell/41e254e85b8c6a3988a92f83bdb40172 to your computer and use it in GitHub Desktop.
My preferred rust template for Algorithmia algorithms
# You'll need at least these dependencies
[dependencies]
algorithmia = "2.0.0"
base64 = "0.3.0"
serde = "0.9.0"
serde_derive = "0.9.0"
serde_json = "0.9.0"
error-chain = "0.9.0"
// This is a rust algorithm template that
// 1. Automatically handles JSON input deserialization and JSON output serialization
// 2. Uses chained errors to easily add error message context to underlying error types
// 3. Provides test stubs
#[macro_use] extern crate algorithmia;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate serde_derive;
use algorithmia::prelude::*;
error_chain!{}
#[derive(Deserialize, Debug)]
pub struct Input {
text: String,
}
#[derive(Serialize, Debug)]
pub struct Output {
text: String
}
algo_entrypoint!(Input);
fn apply(input: Input) -> Result<Box<Output>> {
// Use `chain_err` to return errors wrapped with your own error message, e.g.,
let foo = do_something().chain_err(|| "Message about what failed")?;
// Use `bail!` to explicitly return error messages without an underlying error type, e.g.
if some_condition_is_not_met {
bail!("Some condition was not met");
}
// Box up the return value so it can be serialized, e.g.
let output = Output { ... };
Ok(Box::new(output))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_basic() {
assert_eq!(true, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment