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
//! Various Error enums for the tekton program
use core::fmt;
use std::io;

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TektonError {
    /// An error with a custom message as a String
    Reason(String),
    /// An 'error state' that indicates the file needs to process with the `multi_prefix` support
    SwitchModes(bool),
}

impl fmt::Display for TektonError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let string = match self {
            TektonError::Reason(r) => r.clone(),
            TektonError::SwitchModes(b) => b.to_string(),
        };
        write!(f, "{}", string)
    }
}

impl std::error::Error for TektonError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

impl std::convert::From<io::Error> for TektonError {
    fn from(io_err: io::Error) -> Self {
        TektonError::Reason(io_err.to_string())
    }
}

#[test]
fn test_tekton_error_enum() {
    let reason = "test error".to_string();
    let err = TektonError::Reason(reason.clone());
    let string_err = err.to_string();
    assert_eq!(string_err, reason);
}

#[test]
fn test_tekton_switch() {
    let switch = TektonError::SwitchModes(true);
    assert_eq!(switch, TektonError::SwitchModes(true));
}