lightstreamer_rs/utils/
error.rs1use std::error::Error;
2use std::fmt;
3
4#[derive(Debug)]
9pub struct IllegalArgumentException(String);
10
11impl IllegalArgumentException {
12 pub fn new(msg: &str) -> IllegalArgumentException {
20 IllegalArgumentException(msg.to_string())
21 }
22}
23
24impl fmt::Display for IllegalArgumentException {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 write!(f, "{}", self.0)
27 }
28}
29
30impl Error for IllegalArgumentException {
31 fn description(&self) -> &str {
32 &self.0
33 }
34}
35
36#[derive(Debug)]
42pub struct IllegalStateException {
43 details: String,
44}
45
46impl IllegalStateException {
47 pub fn new(msg: &str) -> IllegalStateException {
55 IllegalStateException {
56 details: msg.to_string(),
57 }
58 }
59}
60
61impl fmt::Display for IllegalStateException {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 write!(f, "{}", self.details)
64 }
65}
66
67impl Error for IllegalStateException {
68 fn description(&self) -> &str {
69 &self.details
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use std::error::Error;
77
78 #[test]
80 fn test_illegal_argument_exception_creation() {
81 let error_msg = "Test error message";
83 let exception = IllegalArgumentException::new(error_msg);
84
85 assert_eq!(exception.0, error_msg);
87
88 let debug_output = format!("{:?}", exception);
90 assert!(debug_output.contains(error_msg));
91
92 let display_output = format!("{}", exception);
94 assert_eq!(display_output, error_msg);
95
96 let description = exception.to_string();
98 assert_eq!(description, error_msg);
99 }
100
101 #[test]
103 fn test_illegal_state_exception_creation() {
104 let error_msg = "Test state error";
106 let exception = IllegalStateException::new(error_msg);
107
108 assert_eq!(exception.details, error_msg);
110
111 let debug_output = format!("{:?}", exception);
113 assert!(debug_output.contains(error_msg));
114
115 let display_output = format!("{}", exception);
117 assert_eq!(display_output, error_msg);
118
119 let description = exception.to_string();
121 assert_eq!(description, error_msg);
122 }
123
124 #[test]
126 fn test_error_propagation() {
127 fn function_that_fails() -> Result<(), IllegalArgumentException> {
129 Err(IllegalArgumentException::new("Test propagation"))
130 }
131
132 fn propagate_error() -> Result<(), Box<dyn Error>> {
134 function_that_fails()?;
135 Ok(())
136 }
137
138 let result = propagate_error();
140 assert!(result.is_err());
141 if let Err(boxed_error) = result {
142 let error_message = boxed_error.to_string();
143 assert_eq!(error_message, "Test propagation");
144 }
145 }
146
147 #[test]
149 fn test_error_conversion() {
150 let arg_exception = IllegalArgumentException::new("Invalid argument");
152
153 let boxed_error: Box<dyn Error> = Box::new(arg_exception);
155
156 assert_eq!(boxed_error.to_string(), "Invalid argument");
158
159 let state_exception = IllegalStateException::new("Invalid state");
161
162 let boxed_error: Box<dyn Error> = Box::new(state_exception);
164
165 assert_eq!(boxed_error.to_string(), "Invalid state");
167 }
168
169 #[test]
171 fn test_error_as_return_type() {
172 fn may_fail(value: i32) -> Result<(), Box<dyn Error>> {
174 if value < 0 {
175 Err(Box::new(IllegalArgumentException::new(
176 "Value cannot be negative",
177 )))
178 } else if value > 100 {
179 Err(Box::new(IllegalStateException::new("Value too large")))
180 } else {
181 Ok(())
182 }
183 }
184
185 let result = may_fail(-10);
187 assert!(result.is_err());
188 if let Err(error) = result {
189 assert_eq!(error.to_string(), "Value cannot be negative");
190 }
191
192 let result = may_fail(150);
194 assert!(result.is_err());
195 if let Err(error) = result {
196 assert_eq!(error.to_string(), "Value too large");
197 }
198
199 let result = may_fail(50);
201 assert!(result.is_ok());
202 }
203
204 #[test]
206 fn test_error_trait_methods() {
207 let arg_exception = IllegalArgumentException::new("Test error");
209 let error: &dyn Error = &arg_exception;
210
211 assert!(error.source().is_none());
213
214 assert_eq!(arg_exception.to_string(), "Test error");
216
217 let state_exception = IllegalStateException::new("Test state error");
219 let error: &dyn Error = &state_exception;
220
221 assert!(error.source().is_none());
223
224 assert_eq!(state_exception.to_string(), "Test state error");
226 }
227
228 #[test]
230 fn test_debug_formatting() {
231 let arg_exception = IllegalArgumentException::new("Test arg error");
233 let debug_str = format!("{:?}", arg_exception);
234 assert!(debug_str.contains("IllegalArgumentException"));
235 assert!(debug_str.contains("Test arg error"));
236
237 let state_exception = IllegalStateException::new("Test state error");
239 let debug_str = format!("{:?}", state_exception);
240 assert!(debug_str.contains("IllegalStateException"));
241 assert!(debug_str.contains("Test state error"));
242 }
243}