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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::pandoc::PandocError;
use crate::libreoffice::LibreOfficeError;
use crate::util::NormalizeError;
use std::convert::From;
use std::error::Error;
use std::fmt;
use std::io::Error as IOError;
use std::path::PathBuf;
use serde_json::error::Error as JsonError;
use tera::Error as TeraError;
/// The error type for errors which can occur while running rsmooth.
pub enum SmoothError<'a> {
/// Normalize Error occurred.
NormalizeError(NormalizeError),
/// Error occurring while calling pandoc contains an PandocError. For more information on the
/// handling of pandoc (-errors) see the pandoc module.
Pandoc(PandocError<'a>),
/// Error occurring while calling LibreOffice.
LibreOffice(LibreOfficeError),
/// The input file was not found under the given path.
InputFileNotFound(&'a str, PathBuf),
/// Couldn't read the Frontmatter YAML Header of the input file. String resembles the path to
/// the input file.
MetadataRead(&'a str),
/// Occurs when the JSON template is already present in the temporary folder. See the matedata
/// module for more information. Contains the path to the template file.
JsonTemplateExists(PathBuf),
/// Error occurring while the creation of the metadata as JSON template. First element contains
/// path to the file, the second element contains the std::io::Error with the cause.
CreateJsonTemplateFailed(PathBuf, IOError),
/// Occurs when the converting metadata JSON cannot be parsed.
MetadataParseFailure(JsonError),
/// Error for failing of the metadata as JSON template removal. Contains the path to the
/// template file and the cause.
RemoveJsonTemplateFailed(PathBuf, IOError),
/// The given template path as specified in the metadata header was not found with the given
/// path.
TemplateNotFound(PathBuf),
/// The given reference path as specified in the metadata header was not found with the given
/// path.
ReferenceNotFound(PathBuf),
/// The given bibliography path as specified in the metadata header was not found with the given
/// path.
BibliographyNotFound(PathBuf),
/// The given citation style file path as specified in the metadata header was not found with the given
/// path.
CitationStyleNotFound(PathBuf),
/// Error while creating a temporary file. Contains the error.
TemporaryFile(IOError),
/// Couldn't read source file.
ReadSourceFailed(PathBuf, IOError),
/// Error occurring while the creation of a file. Should be used when no more specific error is
/// necessary. First element contains path to the file, the second element contains the
/// std::io::Error with the cause.
FileCreateFailed(PathBuf, IOError),
/// Write file failed.
WriteFailed(PathBuf, IOError),
/// Parent element of given path couldn't be determined.
NoParentFolder(PathBuf),
/// Some tera error.
Tera(TeraError),
/// Given reference file isn't compatible with the requested output format. E.g. using a Odt
/// reference file for a docx export. First parameter contains the path to the faulty reference
/// file the second describes the output format.
IncompatibleReferenceFile(PathBuf, &'a str),
}
impl From<NormalizeError> for SmoothError<'_> {
fn from(item: NormalizeError) -> Self {
Self::NormalizeError(item)
}
}
impl fmt::Display for SmoothError<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SmoothError::NormalizeError(err) => write!(f, "path normalize error {}", err),
SmoothError::Pandoc(err) => write!(f, "{}", err),
SmoothError::LibreOffice(err) => write!(f, "{}", err),
SmoothError::InputFileNotFound(given, normalized) => match given == &normalized.as_os_str() {
true => write!(
f,
"input file \"{}\" couldn't be found",
given,
),
false => write!(
f,
"input file \"{}\" couldn't be found under normalized path \"{}\"",
given,
normalized.display(),
),
},
SmoothError::MetadataRead(path) => write!(
f,
"YAML header for input file \"{}\" couldn't be read",
path
),
SmoothError::JsonTemplateExists(path) => write!(
f,
"pandoc template for extracting the metadata as JSON already present under \"{}\" please remove this file manually before proceeding",
path.display()
),
SmoothError::CreateJsonTemplateFailed(path, why) => write!(
f,
"couldn't write temporary metadata-as-JSON template to {} {}",
path.display(),
why
),
SmoothError::MetadataParseFailure(err) => write!(
f,
"couldn't parse frontmatter metadata header of document {}",
err
),
SmoothError::RemoveJsonTemplateFailed(path, why) => write!(
f,
"couldn't remove temporary metadata-as-JSON template under {} {}",
path.display(),
why
),
SmoothError::TemplateNotFound(path) => write!(
f,
"couldn't find template file under {}",
path.display()
),
SmoothError::ReferenceNotFound(path) => write!(
f,
"couldn't find reference file under {}",
path.display()
),
SmoothError::BibliographyNotFound(path) => write!(
f,
"couldn't find bibliography file under {}",
path.display()
),
SmoothError::CitationStyleNotFound(path) => write!(
f,
"couldn't find citation style (csl) file under {}",
path.display()
),
SmoothError::TemporaryFile(err) => write!(
f,
"couldn't create temporary file {}",
err
),
SmoothError::ReadSourceFailed(file, err) => write!(
f,
"couldn't read the content of the given markdown file {} {}",
file.display(),
err
),
SmoothError::FileCreateFailed(file, err) => write!(
f,
"couldn't create file {} {}",
file.display(),
err,
),
SmoothError::WriteFailed(file, err) => write!(
f,
"couldn't write to file {} {}",
file.display(),
err,
),
SmoothError::NoParentFolder(file) => write!(
f,
"no parent folder for path {} found",
file.display(),
),
SmoothError::Tera(error) => write!(
f,
"error in Tera templating engine {}",
error,
),
SmoothError::IncompatibleReferenceFile(file, format) => write!(
f,
"reference file {} isn't compatible to output format {}",
file.display(),
format
),
}
}
}
impl fmt::Debug for SmoothError<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl Error for SmoothError<'_> {}