singleton_registry/
registry_error.rs1use std::fmt;
2
3#[derive(Debug, PartialEq)]
8pub enum RegistryError {
9 RegistryLock,
14
15 TypeMismatch {
19 type_name: &'static str,
21 },
22
23 TypeNotFound {
27 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}