#[macro_export]
macro_rules! algo_entrypoint {
($t:ty, $apply:ident, Algo::$method:ident) => {
impl EntryPoint for Algo {
fn $apply(&self, input: $t) -> ::std::result::Result<AlgoOutput, Box<::std::error::Error>> {
self.$method(input).map(AlgoOutput::from).map_err(|err| err.into())
}
}
};
($t:ty, $apply:ident, $p:path) => {
#[derive(Default)] pub struct Algo;
impl EntryPoint for Algo {
fn $apply(&self, input: $t) -> ::std::result::Result<AlgoOutput, Box<::std::error::Error>> {
$p(input).map(AlgoOutput::from).map_err(|err| err.into())
}
}
};
(&str) => {
algo_entrypoint!(&str => apply);
};
(&[u8]) => {
algo_entrypoint!(&[u8] => apply);
};
(&JsonValue) => {
algo_entrypoint!(&JsonValue => apply);
};
(AlgoInput) => {
algo_entrypoint!(AlgoInput => apply);
};
($t:ty) => {
algo_entrypoint!($t => apply);
};
(&str => Algo::$i:ident) => {
algo_entrypoint!(&str, apply_str, Algo::$i);
};
(&[u8] => Algo::$i:ident) => {
algo_entrypoint!(&[u8], apply_bytes, Algo::$i);
};
(&JsonValue => Algo::$i:ident) => {
algo_entrypoint!(&JsonValue, apply_json, Algo::$i);
};
(AlgoInput => Algo::$i:ident) => {
algo_entrypoint!(AlgoInput, apply, Algo::$i);
};
($t:ty => Algo::$i:ident) => {
impl DecodedEntryPoint for Algo {
type Input = $t;
fn apply_decoded(&self, input: $t) -> ::std::result::Result<AlgoOutput, Box<::std::error::Error>> {
self.$i(input).map(AlgoOutput::from).map_err(|err| err.into())
}
}
};
(&str => $p:path) => {
algo_entrypoint!(&str, apply_str, $p);
};
(&[u8] => $p:path) => {
algo_entrypoint!(&[u8], apply_bytes, $p);
};
(&JsonValue => $p:path) => {
algo_entrypoint!(&JsonValue, apply_json, $p);
};
(AlgoInput => $p:path) => {
algo_entrypoint!(AlgoInput, apply, $p);
};
($t:ty => $p:path) => {
#[derive(Default)] pub struct Algo;
impl DecodedEntryPoint for Algo {
type Input = $t;
fn apply_decoded(&self, input: $t) -> ::std::result::Result<AlgoOutput, Box<::std::error::Error>> {
$p(input).map(AlgoOutput::from).map_err(|err| err.into())
}
}
};
}
mod test_str {
use prelude::*;
algo_entrypoint!(&str => hello_text);
fn hello_text(_input: &str) -> Result<String, String> {
unimplemented!()
}
}
mod test_bytes {
use prelude::*;
algo_entrypoint!(&[u8] => hello_bytes);
fn hello_bytes(_input: &[u8]) -> Result<Vec<u8>, String> {
unimplemented!()
}
}
mod test_json {
use prelude::*;
algo_entrypoint!(&JsonValue => hello_json);
fn hello_json(_input: &JsonValue) -> Result<JsonValue, String> {
unimplemented!()
}
}
mod test_enum {
use prelude::*;
algo_entrypoint!(AlgoInput => hello_enum);
fn hello_enum(_input: AlgoInput) -> Result<AlgoOutput, String> {
unimplemented!()
}
}
mod test_decode {
use prelude::*;
#[cfg_attr(feature="with-rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature="with-serde", derive(Deserialize, Serialize))]
pub struct Custom {
foo: String,
bar: Vec<u32>,
baz: bool,
}
algo_entrypoint!(Custom => hello_decoded);
fn hello_decoded(_input: Custom) -> Result<Box<Custom>, String> {
unimplemented!()
}
}
mod test_decode_with_default {
use prelude::*;
pub struct Algo { _magic: u32 }
#[cfg_attr(feature="with-rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature="with-serde", derive(Deserialize, Serialize))]
pub struct Custom {
foo: String,
bar: Vec<u32>,
baz: bool,
}
algo_entrypoint!(Custom => Algo::hello_decoded);
impl Algo {
fn hello_decoded(&self, _input: Custom) -> Result<Box<Custom>, String> {
unimplemented!()
}
}
impl Default for Algo {
fn default() -> Self {
Algo { _magic: 42 }
}
}
}
mod test_shorthand_str {
use prelude::*;
algo_entrypoint!(&str => apply);
fn apply(_input: &str) -> Result<String, String> {
unimplemented!()
}
}
mod test_shorthand_bytes {
use prelude::*;
algo_entrypoint!(&[u8]);
fn apply(_input: &[u8]) -> Result<Vec<u8>, String> {
unimplemented!()
}
}
mod test_shorthand_json {
use prelude::*;
algo_entrypoint!(&JsonValue);
fn apply(_input: &JsonValue) -> Result<JsonValue, String> {
unimplemented!()
}
}
mod test_shorthand_enum {
use prelude::*;
algo_entrypoint!(AlgoInput);
fn apply(_input: AlgoInput) -> Result<AlgoOutput, String> {
unimplemented!()
}
}
mod test_shorthand_decode {
use prelude::*;
#[cfg_attr(feature="with-rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature="with-serde", derive(Deserialize, Serialize))]
pub struct Custom {
foo: String,
bar: Vec<u32>,
baz: bool,
}
algo_entrypoint!(Custom);
fn apply(_input: Custom) -> Result<Box<Custom>, String> {
unimplemented!()
}
}