use-event-handler 0.1.0

Synchronous event handler trait for RustUse event primitives.
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

pub trait EventHandler<E> {
    type Output;
    type Error;

    fn handle(&self, event: E) -> Result<Self::Output, Self::Error>;
}

impl<E, F, Output, Error> EventHandler<E> for F
where
    F: Fn(E) -> Result<Output, Error>,
{
    type Output = Output;
    type Error = Error;

    fn handle(&self, event: E) -> Result<Self::Output, Self::Error> {
        self(event)
    }
}

#[cfg(test)]
mod tests {
    use super::EventHandler;
    use core::convert::Infallible;

    #[test]
    fn closures_handle_events() {
        let handler = |event: &str| -> Result<usize, Infallible> { Ok(event.len()) };

        assert_eq!(handler.handle("command.started"), Ok(15));
    }
}