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
use thiserror::Error;

pub struct CliError {
    pub error: Option<anyhow::Error>,
    pub exit_code: i32,
}

impl CliError {
    pub fn new(error: anyhow::Error, code: i32) -> CliError {
        CliError {
            error: Some(error),
            exit_code: code,
        }
    }

    pub fn code(code: i32) -> CliError {
        CliError {
            error: None,
            exit_code: code,
        }
    }
}

impl From<anyhow::Error> for CliError {
    fn from(err: anyhow::Error) -> CliError {
        CliError::new(err, 101)
    }
}

impl From<clap::Error> for CliError {
    fn from(err: clap::Error) -> CliError {
        let code = if err.use_stderr() { 1 } else { 0 };
        CliError::new(err.into(), code)
    }
}

impl From<confy::ConfyError> for CliError {
    fn from(err: confy::ConfyError) -> CliError {
        CliError::new(err.into(), 101)
    }
}
impl From<DiaryError> for CliError {
    fn from(err: DiaryError) -> CliError {
        CliError::new(err.into(), 202)
    }
}

#[derive(Error, Debug)]
pub enum DiaryError {
    #[error("Diary folder already exists somewhere.")]
    ExistsElsewhere,

    #[error("Diary folder already exists at the path provided.")]
    ExistsHere,

    #[error("Diary has not been initialised.")]
    UnInitialised { source: std::io::Error },

    #[error(transparent)]
    IOError(#[from] std::io::Error),

    #[error("Today's entry has not yet been created. Use the `new` sub-command.")]
    NoEntry { source: std::io::Error },
}