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
123
124
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::path::PathBuf;
use std::result;
use tera;
use toml;
pub(crate) fn new_error(kind: ErrorKind) -> Error {
Error { kind, source: None }
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub source: Option<Box<dyn StdError>>,
}
#[derive(Debug)]
pub enum ErrorKind {
MissingTemplateDefinition,
InvalidTemplate,
UnreadableStdin,
Git {
err: io::Error,
},
Io {
err: io::Error,
path: PathBuf,
},
Tera {
err: tera::Error,
path: Option<PathBuf>,
},
Toml {
err: toml::de::Error,
},
#[doc(hidden)]
__Nonexhaustive,
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
new_error(ErrorKind::Io { err, path: PathBuf::new() })
}
}
impl StdError for Error {
fn description(&self) -> &str {
match self.kind {
ErrorKind::Io { ref err, .. } => err.description(),
ErrorKind::Tera { ref err, .. } => err.description(),
ErrorKind::InvalidTemplate => "invalid template",
ErrorKind::MissingTemplateDefinition => "missing template.toml",
ErrorKind::UnreadableStdin => "couldn't read from stdin",
ErrorKind::Git { ref err } => err.description(),
ErrorKind::Toml { ref err } => err.description(),
_ => unreachable!(),
}
}
fn cause(&self) -> Option<&dyn StdError> {
match self.kind {
ErrorKind::Io { ref err, .. } => Some(err),
ErrorKind::Tera { ref err, .. } => Some(err),
ErrorKind::Toml { ref err } => Some(err),
ErrorKind::Git { ref err } => Some(err),
ErrorKind::InvalidTemplate => None,
ErrorKind::MissingTemplateDefinition => None,
ErrorKind::UnreadableStdin => None,
_ => unreachable!(),
}
}
fn source(&self) -> Option<&(dyn StdError + 'static)> {
let mut source = self.source.as_ref().map(|c| &**c);
if source.is_none() {
if let ErrorKind::Tera { ref err, .. } = self.kind {
source = err.source();
}
}
source
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::Io { ref err, ref path } => write!(f, "{}: {:?}", err, path),
ErrorKind::Tera { ref err, ref path } => {
if let Some(p) = path {
write!(f, "{}: {:?}", err, p)
} else {
write!(f, "{}: rendering a one-off template", err)
}
}
ErrorKind::Git { ref err } => write!(f, "Could not clone the repository: {}", err),
ErrorKind::Toml { ref err } => write!(f, "Invalid TOML: {}", err),
ErrorKind::MissingTemplateDefinition => write!(f, "The template.toml is missing"),
ErrorKind::UnreadableStdin => write!(f, "Unable to read from stdin"),
ErrorKind::InvalidTemplate => write!(f, "The template.toml is invalid"),
_ => unreachable!(),
}
}
}