ruva_core/
message.rs

1//! ### TEvent
2//! [TEvent] is a trait to manage application-specific events.
3//! Using ruva framework, you can simply annotate struct as follows:
4//! ```rust,no_run
5//! #[derive(Serialize, Deserialize, Clone, TEvent)]
6//! #[aggregate(CustomAggregate)]
7//! #[internally_notifiable]
8//! #[externally_notifiable]
9//! pub struct CustomEvent {
10//!     
11//!     pub id: i64,
12//!     pub custom_field: String,
13//! }
14//! ```
15//! Here, `internally_notifiable` indicates that the event will be handled internally by `MessageBus`
16//! And the `externally_notifiable` means that the event will be stored in the form of `OutBox` and
17//! will be handled in the separate process (or thread)
18use crate::prelude::OutBox;
19use downcast_rs::{impl_downcast, Downcast};
20use std::fmt::Debug;
21
22pub trait TEvent: Sync + Send + Downcast {
23	fn externally_notifiable(&self) -> bool {
24		false
25	}
26	fn internally_notifiable(&self) -> bool {
27		false
28	}
29
30	fn metadata(&self) -> EventMetadata {
31		let event_name = std::any::type_name::<Self>().split("::").last().unwrap();
32		EventMetadata {
33			aggregate_id: Default::default(),
34			aggregate_name: Default::default(),
35			topic: event_name.to_string(),
36		}
37	}
38	fn outbox(&self) -> OutBox {
39		let metadata = self.metadata();
40		OutBox::new(metadata.aggregate_id, metadata.aggregate_name, metadata.topic, self.state())
41	}
42
43	fn state(&self) -> String;
44}
45
46impl_downcast!(TEvent);
47impl Debug for dyn TEvent {
48	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49		write!(f, "{}", self.metadata().topic)
50	}
51}
52
53#[derive(Debug)]
54pub struct EventMetadata {
55	pub aggregate_id: String,
56	pub aggregate_name: String,
57	pub topic: String,
58}
59
60pub trait TCommand: 'static + Send + Sync + Debug {}