summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: b2afd60f57416dd87e82d26e6252d149d3c76984 (plain)
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
use std::path::PathBuf;

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

pub struct Error {
    #[allow(dead_code)]
    pub kind: ErrorKind,
    pub error: String,
}
pub enum ErrorKind {
    Malformed,
    StackExchange,
    Permissions,
    OperatingSystem,
    Panic,
    EmptySites,
    NoResults,
    Termimad(termimad::Error),
}

impl From<&str> for Error {
    fn from(err: &str) -> Self {
        Error {
            kind: ErrorKind::Panic,
            error: String::from(err),
        }
    }
}

impl From<termimad::Error> for Error {
    fn from(err: termimad::Error) -> Self {
        Error {
            kind: ErrorKind::Termimad(err),
            error: String::from(""),
        }
    }
}

// TODO add others
impl Error {
    pub fn malformed(path: &PathBuf) -> Self {
        Error {
            kind: ErrorKind::Malformed,
            error: format!("File {} is malformed; try removing it.", path.display()),
        }
    }
    pub fn se(err: String) -> Self {
        Error {
            kind: ErrorKind::StackExchange,
            error: err,
        }
    }
    pub fn create_dir(path: &PathBuf) -> Self {
        Error {
            kind: ErrorKind::Permissions,
            error: format!(
                "Couldn't create directory {}; please check the permissions
                on the parent directory",
                path.display()
            ),
        }
    }
    pub fn create_file(path: &PathBuf) -> Self {
        Error {
            kind: ErrorKind::Permissions,
            error: format!(
                "Couldn't create file {}; please check the directory permissions",
                path.display()
            ),
        }
    }
    pub fn write_file(path: &PathBuf) -> Self {
        Error {
            kind: ErrorKind::Permissions,
            error: format!(
                "Couldn't write to file {}; please check its permissions",
                path.display()
            ),
        }
    }
    pub fn os(err: &str) -> Self {
        Error {
            kind: ErrorKind::OperatingSystem,
            error: String::from(err),
        }
    }
    pub fn no_results() -> Self {
        Error {
            kind: ErrorKind::NoResults,
            error: String::from("Sorry, no answers found for your question. Try another query."),
        }
    }
}