1use thiserror::Error;
6
7#[derive(Error, Debug, Clone)]
9pub enum TypesError {
10 #[error("Core types not ready")]
12 NotReady,
13
14 #[error("Registration failed: {0}")]
16 RegistrationFailed(String),
17
18 #[error("Internal error: {0}")]
20 Internal(String),
21}
22
23impl TypesError {
24 #[must_use]
26 pub const fn not_ready() -> Self {
27 Self::NotReady
28 }
29
30 #[must_use]
32 pub fn registration_failed(message: impl Into<String>) -> Self {
33 Self::RegistrationFailed(message.into())
34 }
35
36 #[must_use]
38 pub fn internal(message: impl Into<String>) -> Self {
39 Self::Internal(message.into())
40 }
41
42 #[must_use]
44 pub const fn is_not_ready(&self) -> bool {
45 matches!(self, Self::NotReady)
46 }
47
48 #[must_use]
50 pub const fn is_registration_failed(&self) -> bool {
51 matches!(self, Self::RegistrationFailed(_))
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_error_constructors() {
61 let err = TypesError::not_ready();
62 assert!(err.is_not_ready());
63
64 let err = TypesError::registration_failed("schema invalid");
65 assert!(err.is_registration_failed());
66 assert!(err.to_string().contains("schema invalid"));
67
68 let err = TypesError::internal("unexpected");
69 assert!(matches!(err, TypesError::Internal(_)));
70 }
71
72 #[test]
73 fn test_error_display() {
74 let err = TypesError::NotReady;
75 assert_eq!(err.to_string(), "Core types not ready");
76
77 let err = TypesError::RegistrationFailed("failed to parse".to_owned());
78 assert_eq!(err.to_string(), "Registration failed: failed to parse");
79
80 let err = TypesError::Internal("database error".to_owned());
81 assert_eq!(err.to_string(), "Internal error: database error");
82 }
83}