1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! The [`Event`] trait and the [`DynEvent`] helper.
//!
//! An `Event` is a fire-and-forget broadcast identified by a string
//! id. Unlike [`Command`](crate::command::Command), events have no
//! handler — consumers subscribe via
//! [`CommandRegistry::on`](crate::registry::CommandRegistry::on) and
//! receive the deserialized payload.
//!
//! For compile-time events, the `#[event]` attribute macro generates
//! the trait impl from a payload struct. For runtime-constructed
//! events (plugin runtimes, FFI, scripting hosts), [`DynEvent`] lets
//! you build an `Event` whose id is owned at runtime.
use Serialize;
use Value;
/// A typed event payload.
///
/// Implementations pair a compile-time string id with a `Serialize`
/// payload. The struct itself *is* the payload — `serde_json::to_value`
/// on an instance produces what goes on the wire.
///
/// # Compile-time vs runtime events
///
/// - **Compile-time**: `const ID` / `const DESCRIPTION` and the
/// `#[event]` macro. The defaults for [`id`](Self::id) and
/// [`description`](Self::description) read these constants.
/// - **Runtime**: use [`DynEvent`] to supply an owned `String` id,
/// description, and payload. `DynEvent` implements `Event` by
/// overriding the instance-level methods.
///
/// Both paths emit through the same
/// [`emit`](crate::registry::CommandRegistry::emit) entry point.
/// A runtime-constructed [`Event`]. Use this when the event id or
/// payload shape is only known at runtime (plugin runtimes, FFI,
/// scripting hosts).
///
/// ```ignore
/// use coralstack_cmd_ipc::prelude::*;
/// use serde_json::json;
///
/// registry.emit(DynEvent::new(
/// "plugin.say_hi",
/// json!({ "greeting": "hello" }),
/// ))?;
/// ```