Skip to main content

moire_types/objects/
events.rs

1use facet::Facet;
2use moire_trace_types::BacktraceId;
3
4use crate::{EntityId, EventId, Json, PTime, ScopeId, next_event_id};
5
6// r[impl model.event.fields]
7#[derive(Facet)]
8pub struct Event {
9    /// Opaque event identifier.
10    pub id: EventId,
11
12    /// Event timestamp.
13    pub at: PTime,
14
15    /// Event source.
16    pub backtrace: BacktraceId,
17
18    /// Event target (entity or scope).
19    pub target: EventTarget,
20
21    /// Event kind.
22    pub kind: EventKind,
23}
24
25impl Event {
26    /// Builds an event with explicit source context.
27    pub fn new(target: EventTarget, kind: EventKind, backtrace: BacktraceId) -> Self {
28        Self {
29            id: next_event_id(),
30            at: PTime::now(),
31            backtrace,
32            target,
33            kind,
34        }
35    }
36}
37
38#[derive(Facet)]
39#[repr(u8)]
40#[facet(rename_all = "snake_case")]
41pub enum EventTarget {
42    Entity(EntityId),
43    Scope(ScopeId),
44}
45
46// r[impl model.event.kinds]
47#[derive(Facet)]
48#[repr(u8)]
49#[facet(rename_all = "snake_case")]
50pub enum EventKind {
51    StateChanged,
52    ChannelSent,
53    ChannelReceived,
54    Custom(CustomEventKind),
55}
56
57/// A user-defined event kind with arbitrary payload.
58///
59/// Library consumers can emit custom events on any entity without modifying moire source.
60#[derive(Facet)]
61pub struct CustomEventKind {
62    /// Event kind identifier (e.g. "query_executed").
63    pub kind: String,
64    /// Human-readable display name (e.g. "Query Executed").
65    pub display_name: String,
66    /// Arbitrary structured payload as JSON.
67    pub payload: Json,
68}
69
70crate::impl_sqlite_json!(EventTarget);
71crate::impl_sqlite_json!(EventKind);
72
73crate::declare_event_target_slots!(
74    EntityTargetSlot::Entity(EntityId),
75    ScopeTargetSlot::Scope(ScopeId),
76);
77
78crate::declare_event_kind_slots!(
79    StateChangedKindSlot::StateChanged,
80    ChannelSentKindSlot::ChannelSent,
81    ChannelReceivedKindSlot::ChannelReceived,
82);
83
84#[derive(Facet)]
85pub struct ChannelSentEvent {
86    /// Observed wait duration in nanoseconds, if this operation suspended.
87    pub wait_ns: Option<u64>,
88    /// True if the send failed because the other side was gone.
89    pub closed: bool,
90}
91
92#[derive(Facet)]
93pub struct ChannelReceivedEvent {
94    /// Observed wait duration in nanoseconds, if this operation suspended.
95    pub wait_ns: Option<u64>,
96    /// True if the recv failed because the other side was gone.
97    pub closed: bool,
98}