1use std::{
2 fmt::{Debug, Display},
3 io,
4};
5
6pub enum Error {
8 IoError(IoError),
10
11 InvalidInput(String),
15
16 InvalidBufferSizes,
18}
19
20impl Display for Error {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 match &self {
23 Error::IoError(io_e) => f.write_fmt(format_args!("{io_e}")),
24 Error::InvalidInput(e) => f.write_fmt(format_args!("Invalid input: {e}")),
25 Error::InvalidBufferSizes => f.write_str("Invalid buffer sizes"),
26 }
27 }
28}
29
30impl Debug for Error {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 match &self {
33 Error::IoError(io_e) => f.write_fmt(format_args!("IoError ({io_e:?})")),
34 Error::InvalidInput(e) => f.write_fmt(format_args!("InvalidInput ({e:?})")),
35 Error::InvalidBufferSizes => f.write_str("Invalid buffer sizes"),
36 }
37 }
38}
39
40pub struct IoError {
42 pub file: String,
44
45 pub error: io::Error,
47}
48
49impl Display for IoError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 f.write_fmt(format_args!("File: \"{}\"; {}", self.file, self.error))
52 }
53}
54
55impl Debug for IoError {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.write_fmt(format_args!(
58 "IoError {{ file: \"{}\", error: {:?} }}",
59 self.file, self.error
60 ))
61 }
62}
63
64#[cfg(test)]
65mod test {
66 use super::*;
67
68 #[test]
69 fn test_io_error_display() {
70 let e = Error::IoError(IoError {
71 file: "picture.png".to_string(),
72 error: io::Error::from(io::ErrorKind::NotFound),
73 });
74
75 assert_eq!(&format!("{e}"), "File: \"picture.png\"; entity not found")
76 }
77
78 #[test]
79 fn test_invalid_input_error_display() {
80 let e = Error::InvalidInput("param1 is None".to_string());
81
82 assert_eq!(&format!("{e}"), "Invalid input: param1 is None")
83 }
84
85 #[test]
86 fn test_io_error_debug() {
87 let e = Error::IoError(IoError {
88 file: "picture.png".to_string(),
89 error: io::Error::from(io::ErrorKind::NotFound),
90 });
91
92 assert_eq!(
93 &format!("{e:?}"),
94 "IoError (IoError { file: \"picture.png\", error: Kind(NotFound) })"
95 )
96 }
97
98 #[test]
99 fn test_invalid_input_error_debug() {
100 let e = Error::InvalidInput("param1 is None".to_string());
101
102 assert_eq!(&format!("{e:?}"), "InvalidInput (\"param1 is None\")")
103 }
104}