serenade_messenger/handler.rs
1//! Typed command and event handlers.
2
3use crate::{Command, Event, MessengerError};
4
5/// Handles one command type. Register at most one per [`crate::Message::NAME`].
6pub trait CommandHandler<C: Command>: Send + Sync {
7 /// Handles `command`.
8 ///
9 /// # Errors
10 ///
11 /// Return [`MessengerError`] when handling fails.
12 fn handle(&self, command: &C) -> Result<(), MessengerError>;
13}
14
15/// Handles one event type. Many handlers may share the same [`crate::Message::NAME`].
16pub trait EventHandler<E: Event>: Send + Sync {
17 /// Handles `event`.
18 ///
19 /// # Errors
20 ///
21 /// Return [`MessengerError`] when handling fails.
22 fn handle(&self, event: &E) -> Result<(), MessengerError>;
23}