1use std::fmt;
5use std::io;
6
7#[derive(Debug, Clone)]
9pub enum Error {
10 Io(String),
12 NotFound(String),
14 InvalidDevice(String),
16 PermissionDenied(String),
18}
19
20impl fmt::Display for Error {
21 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 match self {
23 Error::Io(msg) => write!(f, "IO error: {}", msg),
24 Error::NotFound(msg) => write!(f, "Not found: {}", msg),
25 Error::InvalidDevice(msg) => write!(f, "Invalid device: {}", msg),
26 Error::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
27 }
28 }
29}
30
31impl From<io::Error> for Error {
32 fn from(err: io::Error) -> Self {
33 Error::Io(err.to_string())
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
42 fn test_error_display_io() {
43 let err = Error::Io("test error".to_string());
44 assert_eq!(err.to_string(), "IO error: test error");
45 }
46
47 #[test]
48 fn test_error_display_not_found() {
49 let err = Error::NotFound("device".to_string());
50 assert_eq!(err.to_string(), "Not found: device");
51 }
52
53 #[test]
54 fn test_error_display_invalid_device() {
55 let err = Error::InvalidDevice("bad device".to_string());
56 assert_eq!(err.to_string(), "Invalid device: bad device");
57 }
58
59 #[test]
60 fn test_error_display_permission_denied() {
61 let err = Error::PermissionDenied("access denied".to_string());
62 assert_eq!(err.to_string(), "Permission denied: access denied");
63 }
64
65 #[test]
66 fn test_error_clone() {
67 let err = Error::NotFound("test".to_string());
68 let cloned = err.clone();
69 assert_eq!(err.to_string(), cloned.to_string());
70 }
71
72 #[test]
73 fn test_error_debug() {
74 let err = Error::NotFound("test".to_string());
75 let debug_str = format!("{:?}", err);
76 assert!(debug_str.contains("NotFound"));
77 }
78}