1use core::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18#[non_exhaustive]
19pub enum GuardError {
20 Timeout,
28
29 DeadlineViolated,
37
38 Panicked(String),
44
45 Internal(String),
48
49 Dependency(String),
55}
56
57impl GuardError {
58 pub fn panicked(message: impl Into<String>) -> Self {
63 GuardError::Panicked(message.into())
64 }
65
66 pub fn internal(message: impl Into<String>) -> Self {
68 GuardError::Internal(message.into())
69 }
70
71 pub fn dependency(message: impl Into<String>) -> Self {
73 GuardError::Dependency(message.into())
74 }
75
76 pub fn is_timeout(&self) -> bool {
79 matches!(self, GuardError::Timeout | GuardError::DeadlineViolated)
80 }
81
82 pub fn is_panic(&self) -> bool {
84 matches!(self, GuardError::Panicked(_))
85 }
86
87 pub fn kind(&self) -> &'static str {
92 match self {
93 GuardError::Timeout => "timeout",
94 GuardError::DeadlineViolated => "deadline_violated",
95 GuardError::Panicked(_) => "panicked",
96 GuardError::Internal(_) => "internal",
97 GuardError::Dependency(_) => "dependency",
98 }
99 }
100
101 pub fn message(&self) -> Option<&str> {
103 match self {
104 GuardError::Panicked(message)
105 | GuardError::Internal(message)
106 | GuardError::Dependency(message) => Some(message),
107 GuardError::Timeout | GuardError::DeadlineViolated => None,
108 }
109 }
110}
111
112impl fmt::Display for GuardError {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 match self {
115 GuardError::Timeout => f.write_str("guard timed out"),
116 GuardError::DeadlineViolated => {
117 f.write_str("strict guard exceeded its deadline without yielding")
118 }
119 GuardError::Panicked(message) => write!(f, "guard panicked: {message}"),
120 GuardError::Internal(message) => write!(f, "guard failed internally: {message}"),
121 GuardError::Dependency(message) => write!(f, "guard dependency failed: {message}"),
122 }
123 }
124}
125
126impl std::error::Error for GuardError {}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 fn all() -> Vec<GuardError> {
133 vec![
134 GuardError::Timeout,
135 GuardError::DeadlineViolated,
136 GuardError::Panicked("boom".to_owned()),
137 GuardError::Internal("bad rule".to_owned()),
138 GuardError::Dependency("classifier unreachable".to_owned()),
139 ]
140 }
141
142 #[test]
143 fn kinds_are_unique_and_payload_free() {
144 let mut kinds: Vec<_> = all().iter().map(GuardError::kind).collect();
145 let count = kinds.len();
146 kinds.sort_unstable();
147 kinds.dedup();
148 assert_eq!(kinds.len(), count, "two variants share a kind label");
149 for error in all() {
150 assert!(
151 !error.kind().contains(' '),
152 "kind labels must be usable as metric dimensions"
153 );
154 }
155 }
156
157 #[test]
158 fn timeout_predicates_cover_both_time_variants() {
159 assert!(GuardError::Timeout.is_timeout());
160 assert!(GuardError::DeadlineViolated.is_timeout());
161 assert!(!GuardError::internal("x").is_timeout());
162 assert!(!GuardError::dependency("x").is_timeout());
163 }
164
165 #[test]
166 fn only_panicked_is_a_panic() {
167 assert!(GuardError::Panicked("boom".to_owned()).is_panic());
168 assert!(!GuardError::internal("boom").is_panic());
169 }
170
171 #[test]
172 fn messages_are_carried_by_the_variants_that_have_them() {
173 assert_eq!(GuardError::internal("bad rule").message(), Some("bad rule"));
174 assert_eq!(GuardError::dependency("down").message(), Some("down"));
175 assert_eq!(
176 GuardError::Panicked("boom".to_owned()).message(),
177 Some("boom")
178 );
179 assert_eq!(GuardError::Timeout.message(), None);
180 assert_eq!(GuardError::DeadlineViolated.message(), None);
181 }
182
183 #[test]
184 fn display_includes_the_message_for_diagnostics() {
185 assert_eq!(
186 GuardError::Panicked("index out of bounds".to_owned()).to_string(),
187 "guard panicked: index out of bounds"
188 );
189 assert_eq!(GuardError::Timeout.to_string(), "guard timed out");
190 }
191
192 #[test]
193 fn is_a_std_error() {
194 fn assert_error<E: std::error::Error>(_: &E) {}
195 for error in all() {
196 assert_error(&error);
197 }
198 }
199}