singleton_registry/
registry_error.rs

1use std::fmt;
2
3/// Error type for registry operations.
4///
5/// All fallible registry operations return this error type to indicate
6/// what went wrong during the operation.
7#[derive(Debug, PartialEq)]
8pub enum RegistryError {
9    /// Failed to acquire the registry lock (lock poisoning).
10    ///
11    /// This is automatically recovered in most operations, but exposed
12    /// here for completeness.
13    RegistryLock,
14
15    /// Type mismatch during downcast (should never happen in practice).
16    ///
17    /// Includes the type name that was requested.
18    TypeMismatch {
19        /// The type name that was requested
20        type_name: &'static str,
21    },
22
23    /// The requested type was not found in the registry.
24    ///
25    /// Includes the type name that was requested.
26    TypeNotFound {
27        /// The type name that was requested
28        type_name: &'static str,
29    },
30}
31
32impl fmt::Display for RegistryError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            RegistryError::RegistryLock => write!(f, "Failed to acquire registry lock"),
36            RegistryError::TypeMismatch { type_name } => {
37                write!(f, "Type mismatch in registry for type: {}", type_name)
38            }
39            RegistryError::TypeNotFound { type_name } => {
40                write!(f, "Type not found in registry: {}", type_name)
41            }
42        }
43    }
44}
45
46impl std::error::Error for RegistryError {}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_registry_lock_display() {
54        let err = RegistryError::RegistryLock;
55        assert_eq!(err.to_string(), "Failed to acquire registry lock");
56    }
57
58    #[test]
59    fn test_type_mismatch_display() {
60        let err = RegistryError::TypeMismatch { type_name: "i32" };
61        assert_eq!(err.to_string(), "Type mismatch in registry for type: i32");
62    }
63
64    #[test]
65    fn test_type_not_found_display() {
66        let err = RegistryError::TypeNotFound {
67            type_name: "String",
68        };
69        assert_eq!(err.to_string(), "Type not found in registry: String");
70    }
71
72    #[test]
73    fn test_debug_format() {
74        let err = RegistryError::TypeNotFound {
75            type_name: "String",
76        };
77        assert!(format!("{:?}", err).contains("TypeNotFound"));
78    }
79
80    #[test]
81    fn test_equality() {
82        assert_eq!(RegistryError::RegistryLock, RegistryError::RegistryLock);
83        assert_ne!(
84            RegistryError::RegistryLock,
85            RegistryError::TypeNotFound {
86                type_name: "String"
87            }
88        );
89    }
90
91    #[test]
92    fn test_error_trait() {
93        let err: &dyn std::error::Error = &RegistryError::TypeNotFound {
94            type_name: "String",
95        };
96        assert_eq!(err.to_string(), "Type not found in registry: String");
97    }
98}