dddk_core/dddk/event/
event_handler.rs

1use std::any::{Any, TypeId};
2use std::sync::Arc;
3use crate::dddk::event::event::Event;
4use crate::dddk::aliases::GenericError;
5
6pub trait EventHandler<E: Sized + Any + Event> {
7    fn handle_generic_event(&self, event: Arc<dyn Event>) -> Result<(), GenericError> {
8        if let Some(event_ref) = event.as_any().downcast_ref::<E>() {
9            return self.handle(event_ref);
10        } else {
11            panic!("Given event is not associated with the handler !");
12        }
13    }
14
15    fn handle(&self, event: &E) -> Result<(), GenericError>;
16
17    fn get_associated_event(&self) -> TypeId {
18        return TypeId::of::<E>();
19    }
20}
21
22pub trait EventHandlerInBus {
23    fn handle_from_bus(&self, event: Arc<dyn Event>) -> Result<(), GenericError>;
24
25    fn get_associated_event_from_bus(&self) -> TypeId;
26
27    fn as_any(&self) -> &dyn Any;
28
29    fn get_event_handler_name(&self) -> String;
30}