1use std::error;
2use std::fmt;
3use std::io;
4
5#[derive(Debug)]
7pub enum Error {
8 Io(io::Error),
10
11 Ares(c_ares::Error),
13}
14
15impl fmt::Display for Error {
16 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17 match *self {
18 Self::Io(ref err) => err.fmt(f),
19 Self::Ares(ref err) => err.fmt(f),
20 }
21 }
22}
23
24impl error::Error for Error {
25 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
26 match *self {
27 Self::Io(ref err) => Some(err),
28 Self::Ares(ref err) => Some(err),
29 }
30 }
31}
32
33impl From<io::Error> for Error {
34 fn from(err: io::Error) -> Self {
35 Self::Io(err)
36 }
37}
38
39impl From<c_ares::Error> for Error {
40 fn from(err: c_ares::Error) -> Self {
41 Self::Ares(err)
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 use std::error::Error as StdError;
49
50 fn assert_send<T: Send>() {}
51 fn assert_sync<T: Sync>() {}
52
53 #[test]
54 fn error_is_send() {
55 assert_send::<Error>();
56 }
57
58 #[test]
59 fn error_is_sync() {
60 assert_sync::<Error>();
61 }
62
63 #[test]
64 fn display_io_error() {
65 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
66 let err = Error::Io(io_err);
67 let display = format!("{err}");
68 assert!(display.contains("file not found"));
69 }
70
71 #[test]
72 fn display_ares_error() {
73 let ares_err = c_ares::Error::ENODATA;
74 let err = Error::Ares(ares_err);
75 let display = format!("{err}");
76 assert!(!display.is_empty());
77 }
78
79 #[test]
80 fn source_io_error() {
81 let io_err = io::Error::new(io::ErrorKind::NotFound, "test error");
82 let err = Error::Io(io_err);
83 assert!(err.source().is_some());
84 }
85
86 #[test]
87 fn source_ares_error() {
88 let ares_err = c_ares::Error::ENODATA;
89 let err = Error::Ares(ares_err);
90 assert!(err.source().is_some());
91 }
92
93 #[test]
94 fn from_io_error() {
95 let io_err = io::Error::other("test");
96 let err: Error = io_err.into();
97 assert!(matches!(err, Error::Io(_)));
98 }
99
100 #[test]
101 fn from_ares_error() {
102 let ares_err = c_ares::Error::ENOTFOUND;
103 let err: Error = ares_err.into();
104 assert!(matches!(err, Error::Ares(_)));
105 }
106
107 #[test]
108 fn debug_format() {
109 let io_err = io::Error::other("test");
110 let err = Error::Io(io_err);
111 let debug = format!("{err:?}");
112 assert!(debug.contains("Io"));
113 }
114}