sim_lib_music_core/trace.rs
1use sim_kernel::Symbol;
2
3use crate::{PlayEvent, PlayerDeviceId};
4
5/// The kind of action a player chain took on an event, as traced.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum TraceAction {
8 /// The event was newly produced by a player.
9 Generated,
10 /// The event was discarded and not forwarded.
11 Dropped,
12 /// The event was modified before being forwarded.
13 Rewritten,
14 /// The event was forwarded to a different device.
15 Routed,
16}
17
18impl TraceAction {
19 /// Returns the stable wire label for this action.
20 pub fn wire_label(self) -> &'static str {
21 match self {
22 Self::Generated => "generated",
23 Self::Dropped => "dropped",
24 Self::Rewritten => "rewritten",
25 Self::Routed => "routed",
26 }
27 }
28
29 /// Returns the qualified trace symbol for this action.
30 pub fn symbol(self) -> Symbol {
31 Symbol::qualified("music/player-trace", self.wire_label())
32 }
33}
34
35/// A single trace record describing one action in a player chain.
36#[derive(Clone, Debug, PartialEq, Eq)]
37pub struct ChainTraceRecord {
38 /// Monotonic sequence number ordering records within a trace.
39 pub sequence: u64,
40 /// The device that produced or handled the event.
41 pub device_id: PlayerDeviceId,
42 /// The action taken on the event.
43 pub action: TraceAction,
44 /// The event the action applied to.
45 pub event: PlayEvent,
46 /// Human-readable detail about the action.
47 pub detail: String,
48}
49
50impl ChainTraceRecord {
51 /// Builds a trace record from its fields, converting `detail` into a string.
52 pub fn new(
53 sequence: u64,
54 device_id: PlayerDeviceId,
55 action: TraceAction,
56 event: PlayEvent,
57 detail: impl Into<String>,
58 ) -> Self {
59 Self {
60 sequence,
61 device_id,
62 action,
63 event,
64 detail: detail.into(),
65 }
66 }
67}