dotenvy/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::env;
use std::error;
use std::fmt;
use std::io;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    LineParse(String, usize),
    Io(io::Error),
    EnvVar(env::VarError),
}

impl Error {
    pub fn not_found(&self) -> bool {
        if let Error::Io(ref io_error) = *self {
            return io_error.kind() == io::ErrorKind::NotFound;
        }
        false
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::Io(err) => Some(err),
            Error::EnvVar(err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(err) => write!(fmt, "{}", err),
            Error::EnvVar(err) => write!(fmt, "{}", err),
            Error::LineParse(line, error_index) => write!(
                fmt,
                "Error parsing line: '{}', error at line index: {}",
                line, error_index
            ),
        }
    }
}

#[cfg(test)]
mod test {
    use std::env;
    use std::error::Error as StdError;
    use std::io;

    use super::*;

    #[test]
    fn test_io_error_source() {
        let err = Error::Io(io::ErrorKind::PermissionDenied.into());
        let io_err = err.source().unwrap().downcast_ref::<io::Error>().unwrap();
        assert_eq!(io::ErrorKind::PermissionDenied, io_err.kind());
    }

    #[test]
    fn test_envvar_error_source() {
        let err = Error::EnvVar(env::VarError::NotPresent);
        let var_err = err
            .source()
            .unwrap()
            .downcast_ref::<env::VarError>()
            .unwrap();
        assert_eq!(&env::VarError::NotPresent, var_err);
    }

    #[test]
    fn test_lineparse_error_source() {
        let err = Error::LineParse("test line".to_string(), 2);
        assert!(err.source().is_none());
    }

    #[test]
    fn test_error_not_found_true() {
        let err = Error::Io(io::ErrorKind::NotFound.into());
        assert!(err.not_found());
    }

    #[test]
    fn test_error_not_found_false() {
        let err = Error::Io(io::ErrorKind::PermissionDenied.into());
        assert!(!err.not_found());
    }

    #[test]
    fn test_io_error_display() {
        let err = Error::Io(io::ErrorKind::PermissionDenied.into());
        let io_err: io::Error = io::ErrorKind::PermissionDenied.into();

        let err_desc = format!("{}", err);
        let io_err_desc = format!("{}", io_err);
        assert_eq!(io_err_desc, err_desc);
    }

    #[test]
    fn test_envvar_error_display() {
        let err = Error::EnvVar(env::VarError::NotPresent);
        let var_err = env::VarError::NotPresent;

        let err_desc = format!("{}", err);
        let var_err_desc = format!("{}", var_err);
        assert_eq!(var_err_desc, err_desc);
    }

    #[test]
    fn test_lineparse_error_display() {
        let err = Error::LineParse("test line".to_string(), 2);
        let err_desc = format!("{}", err);
        assert_eq!(
            "Error parsing line: 'test line', error at line index: 2",
            err_desc
        );
    }
}