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/// # Examples
7///
8/// ```rust
9/// use singleton_registry::RegistryEvent;
10///
11/// let event = RegistryEvent::Register { type_name: "i32" };
12/// assert_eq!(event.to_string(), "register { type_name: i32 }");
13/// ```
14#[derive(Debug, Clone)]
15pub enum RegistryEvent {
16    /// A value was registered in the registry.
17    Register {
18        /// The type name of the registered value (e.g., "i32", "alloc::string::String")
19        type_name: &'static str,
20    },
21
22    /// A value was requested from the registry.
23    Get {
24        /// The type name that was requested
25        type_name: &'static str,
26        /// Whether the value was found in the registry
27        found: bool,
28    },
29
30    /// A type existence check was performed.
31    Contains {
32        /// The type name that was checked
33        type_name: &'static str,
34        /// Whether the type exists in the registry
35        found: bool,
36    },
37
38    /// The registry was cleared.
39    Clear {},
40}
41
42impl std::fmt::Display for RegistryEvent {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            RegistryEvent::Register { type_name } => {
46                write!(f, "register {{ type_name: {} }}", type_name)
47            }
48            RegistryEvent::Get { type_name, found } => {
49                write!(f, "get {{ type_name: {}, found: {} }}", type_name, found)
50            }
51            RegistryEvent::Contains { type_name, found } => {
52                write!(
53                    f,
54                    "contains {{ type_name: {}, found: {} }}",
55                    type_name, found
56                )
57            }
58            RegistryEvent::Clear {} => write!(f, "Clearing the Registry"),
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_display_register() {
69        let ev = RegistryEvent::Register { type_name: "i32" };
70        assert_eq!(ev.to_string(), "register { type_name: i32 }");
71    }
72
73    #[test]
74    fn test_display_get() {
75        let ev = RegistryEvent::Get {
76            type_name: "String",
77            found: true,
78        };
79        assert_eq!(ev.to_string(), "get { type_name: String, found: true }");
80    }
81
82    #[test]
83    fn test_display_contains() {
84        let ev = RegistryEvent::Contains {
85            type_name: "u8",
86            found: false,
87        };
88        assert_eq!(ev.to_string(), "contains { type_name: u8, found: false }");
89    }
90
91    #[test]
92    fn test_display_clear() {
93        let ev = RegistryEvent::Clear {};
94        assert_eq!(ev.to_string(), "Clearing the Registry");
95    }
96}