Debian Codegraph @

src/error.rs

raw
1 //! # error
2 //!
3 //! The error structure and types.
4 //!
5
6 use std::error::Error;
7 use std::fmt;
8 use std::fmt::Display;
9 use std::io;
10 use std::time::SystemTimeError;
11
12 #[cfg(test)]
13 #[path = "./error_test.rs"]
14 mod error_test;
15
16 #[derive(Debug)]
17 /// Holds the error information
18 pub enum FsIOError {
19 /// Path already exist error type
20 PathAlreadyExists(String),
21 /// Not a file error type
22 NotFile(String),
23 /// IO error type
24 IOError(String, Option<io::Error>),
25 /// System time error type
26 SystemTimeError(String, Option<SystemTimeError>),
27 }
28
29 impl Display for FsIOError {
30 /// Formats the error using the given formatter.
31 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
32 match self {
33 Self::PathAlreadyExists(ref message) => write!(formatter, "{}", message),
34 Self::NotFile(ref message) => write!(formatter, "{}", message),
35 Self::IOError(ref message, ref cause) => {
36 writeln!(formatter, "{}", message)?;
37 match cause {
38 Some(cause_err) => cause_err.fmt(formatter),
39 None => Ok(()),
40 }
41 }
42 Self::SystemTimeError(ref message, ref cause) => {
43 writeln!(formatter, "{}", message)?;
44 match cause {
45 Some(cause_err) => cause_err.fmt(formatter),
46 None => Ok(()),
47 }
48 }
49 }
50 }
51 }
52
53 impl Error for FsIOError {
54 fn source(&self) -> Option<&(dyn Error + 'static)> {
55 match self {
56 Self::PathAlreadyExists(_) => None,
57 Self::NotFile(_) => None,
58 Self::IOError(_, error) => error.as_ref().map(|io_error| {
59 let std_error: &dyn Error = io_error;
60 std_error
61 }),
62 Self::SystemTimeError(_, error) => error.as_ref().map(|system_time_error| {
63 let std_error: &dyn Error = system_time_error;
64 std_error
65 }),
66 }
67 }
68 }