onetime_cli/
error.rs

1use std::{
2    fmt::{Debug, Display},
3    io,
4};
5
6/// Crate specific error type
7pub enum Error {
8    /// An error that occurred during an I/O operation. See [`IoError`](IoError)
9    IoError(IoError),
10
11    /// An error that is related to input data being invalid. Each function
12    /// that possibly returns this error has detailed information about when
13    /// it returns this error type.
14    InvalidInput(String),
15
16    /// At least one passed buffer does not fulfill its size requirements
17    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
40/// Variant of [`Error`] representing an error that occurred during file I/O
41pub struct IoError {
42    /// The file that the operation was working on or related to
43    pub file: String,
44
45    /// The actual [`std::io::Error`] that occurred
46    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}