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::io;
use chrono::ParseError;
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) -> Self {
Self {
error: Some(error),
exit_code: code,
}
}
pub fn code(code: i32) -> Self {
Self {
error: None,
exit_code: code,
}
}
}
impl From<anyhow::Error> for CliError {
fn from(err: anyhow::Error) -> Self {
Self::new(err, 101)
}
}
impl From<clap::Error> for CliError {
fn from(err: clap::Error) -> Self {
let code = if err.use_stderr() { 1 } else { 0 };
Self::new(err.into(), code)
}
}
impl From<confy::ConfyError> for CliError {
fn from(err: confy::ConfyError) -> Self {
Self::new(err.into(), 101)
}
}
impl From<DiaryError> for CliError {
fn from(err: DiaryError) -> Self {
Self::new(err.into(), 202)
}
}
impl From<ParseError> for CliError {
fn from(err: ParseError) -> Self {
Self::new(err.into(), 101)
}
}
impl From<io::Error> for CliError {
fn from(err: io::Error) -> Self {
Self::new(err.into(), 1)
}
}
#[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. Use the `init` sub-command.")]
UnInitialised { source: Option<std::io::Error> },
#[error(transparent)]
IOError(#[from] std::io::Error),
#[error("The desired entry has not been found. You can use the `new` command to create today's entry.")]
NoEntry { source: Option<std::io::Error> },
#[error("No content provided, aborting.")]
NoContent,
#[error("Unsupported file type.")]
BadFileType,
#[error(transparent)]
GitError(#[from] git2::Error),
}