Skip to main content

singleton_registry/
registry_event.rs

1/// Events emitted by the registry during operations.
2///
3/// These events are passed to the tracing callback set via `set_trace_callback`.
4/// The `Clone` derive allows callbacks to store or forward events if needed.
5///
6/// For `register`, two events are emitted per call: `Register` fires before the
7/// value is stored (so a panic during storage is visible in the log), and
8/// `RegisterCompleted` fires after the value is successfully stored. If only
9/// `Register` appears without a following `RegisterCompleted`, the store panicked.
10///
11/// # Examples
12///
13/// ```rust
14/// use singleton_registry::RegistryEvent;
15///
16/// let event = RegistryEvent::Register { type_name: "i32" };
17/// assert_eq!(event.to_string(), "register { type_name: i32 }");
18/// ```
19#[derive(Debug, Clone)]
20pub enum RegistryEvent {
21    /// A register call was received. Fires before the value is stored.
22    /// Followed by `RegisterCompleted` on success.
23    Register {
24        /// The type name of the value being registered (e.g., "i32", "alloc::string::String")
25        type_name: &'static str,
26    },
27
28    /// A value was successfully stored in the registry. Fires after the insert.
29    RegisterCompleted {
30        /// The type name of the value that was stored
31        type_name: &'static str,
32    },
33
34    /// A value was requested from the registry.
35    Get {
36        /// The type name that was requested
37        type_name: &'static str,
38        /// Whether the value was found in the registry
39        found: bool,
40    },
41
42    /// A type existence check was performed.
43    Contains {
44        /// The type name that was checked
45        type_name: &'static str,
46        /// Whether the type exists in the registry
47        found: bool,
48    },
49
50    /// The registry was cleared.
51    Clear {},
52}
53
54impl std::fmt::Display for RegistryEvent {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            RegistryEvent::Register { type_name } => {
58                write!(f, "register {{ type_name: {} }}", type_name)
59            }
60            RegistryEvent::RegisterCompleted { type_name } => {
61                write!(f, "register_completed {{ type_name: {} }}", type_name)
62            }
63            RegistryEvent::Get { type_name, found } => {
64                write!(f, "get {{ type_name: {}, found: {} }}", type_name, found)
65            }
66            RegistryEvent::Contains { type_name, found } => {
67                write!(
68                    f,
69                    "contains {{ type_name: {}, found: {} }}",
70                    type_name, found
71                )
72            }
73            RegistryEvent::Clear {} => write!(f, "Clearing the Registry"),
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_display_register() {
84        let ev = RegistryEvent::Register { type_name: "i32" };
85        assert_eq!(ev.to_string(), "register { type_name: i32 }");
86    }
87
88    #[test]
89    fn test_display_register_completed() {
90        let ev = RegistryEvent::RegisterCompleted { type_name: "i32" };
91        assert_eq!(ev.to_string(), "register_completed { type_name: i32 }");
92    }
93
94    #[test]
95    fn test_display_get() {
96        let ev = RegistryEvent::Get {
97            type_name: "String",
98            found: true,
99        };
100        assert_eq!(ev.to_string(), "get { type_name: String, found: true }");
101    }
102
103    #[test]
104    fn test_display_contains() {
105        let ev = RegistryEvent::Contains {
106            type_name: "u8",
107            found: false,
108        };
109        assert_eq!(ev.to_string(), "contains { type_name: u8, found: false }");
110    }
111
112    #[test]
113    fn test_display_clear() {
114        let ev = RegistryEvent::Clear {};
115        assert_eq!(ev.to_string(), "Clearing the Registry");
116    }
117}