struct ExampleEvent(Option<u32>);
struct ExampleAction(Option<u32>);
struct ExampleContext(u32);
struct ExampleAlgorithm;
impl augrim::Algorithm for ExampleAlgorithm {
type Event = ExampleEvent;
type Action = ExampleAction;
type Context = ExampleContext;
fn event(
&self,
event: Self::Event,
context: Self::Context,
) -> Result<Vec<Self::Action>, augrim::error::AlgorithmError> {
if let ExampleEvent(Some(i)) = event {
Ok(vec![ExampleAction(Some(i + context.0))])
} else {
Ok(vec![ExampleAction(None)])
}
}
}
impl<'a> TryFrom<Option<&'a str>> for ExampleEvent {
type Error = augrim::error::InternalError;
fn try_from(val: Option<&'a str>) -> Result<Self, Self::Error> {
val.map(|s| {
s.parse::<u32>()
.map_err(|e| augrim::error::InternalError::from_source(Box::new(e)))
})
.transpose()
.map(ExampleEvent)
}
}
impl TryFrom<ExampleAction> for Option<String> {
type Error = augrim::error::InternalError;
fn try_from(val: ExampleAction) -> Result<Self, Self::Error> {
Ok(val.0.map(|i| i.to_string()))
}
}
impl<'a> TryFrom<&'a str> for ExampleContext {
type Error = augrim::error::InternalError;
fn try_from(val: &'a str) -> Result<Self, Self::Error> {
val.parse::<u32>()
.map_err(|e| augrim::error::InternalError::from_source(Box::new(e)))
.map(ExampleContext)
}
}