lib/
error.rs

1use crate::pandoc::PandocError;
2use crate::libreoffice::LibreOfficeError;
3use crate::util::NormalizeError;
4
5use std::convert::From;
6use std::error::Error;
7use std::fmt;
8use std::io::Error as IOError;
9use std::path::PathBuf;
10
11use serde_json::error::Error as JsonError;
12use tera::Error as TeraError;
13
14/// The error type for errors which can occur while running rsmooth.
15pub enum SmoothError<'a> {
16    /// Normalize Error occurred.
17    NormalizeError(NormalizeError),
18    /// Error occurring while calling pandoc contains an PandocError. For more information on the
19    /// handling of pandoc (-errors) see the pandoc module.
20    Pandoc(PandocError<'a>),
21    /// Error occurring while calling LibreOffice.
22    LibreOffice(LibreOfficeError),
23    /// The input file was not found under the given path.
24    InputFileNotFound(&'a str, PathBuf),
25    /// Couldn't read the Frontmatter YAML Header of the input file. String resembles the path to
26    /// the input file.
27    MetadataRead(&'a str),
28    /// Occurs when the JSON template is already present in the temporary folder. See the matedata
29    /// module for more information. Contains the path to the template file.
30    JsonTemplateExists(PathBuf),
31    /// Error occurring while the creation of the metadata as JSON template. First element contains
32    /// path to the file, the second element contains the std::io::Error with the cause.
33    CreateJsonTemplateFailed(PathBuf, IOError),
34    /// Occurs when the converting metadata JSON cannot be parsed.
35    MetadataParseFailure(JsonError),
36    /// Error for failing of the metadata as JSON template removal. Contains the path to the
37    /// template file and the cause.
38    RemoveJsonTemplateFailed(PathBuf, IOError),
39    /// The given template path as specified in the metadata header was not found with the given
40    /// path.
41    TemplateNotFound(PathBuf),
42    /// The given reference path as specified in the metadata header was not found with the given
43    /// path.
44    ReferenceNotFound(PathBuf),
45    /// The given bibliography path as specified in the metadata header was not found with the given
46    /// path.
47    BibliographyNotFound(PathBuf),
48    /// The given citation style file path as specified in the metadata header was not found with the given
49    /// path.
50    CitationStyleNotFound(PathBuf),
51    /// Error while creating a temporary file. Contains the error.
52    TemporaryFile(IOError),
53    /// Couldn't read source file.
54    ReadSourceFailed(PathBuf, IOError),
55    /// Error occurring while the creation of a file. Should be used when no more specific error is
56    /// necessary. First element contains path to the file, the second element contains the
57    /// std::io::Error with the cause.
58    FileCreateFailed(PathBuf, IOError),
59    /// Write file failed.
60    WriteFailed(PathBuf, IOError),
61    /// Parent element of given path couldn't be determined.
62    NoParentFolder(PathBuf),
63    /// Some tera error.
64    Tera(TeraError),
65    /// Given reference file isn't compatible with the requested output format. E.g. using a Odt
66    /// reference file for a docx export. First parameter contains the path to the faulty reference
67    /// file the second describes the output format.
68    IncompatibleReferenceFile(PathBuf, &'a str),
69}
70
71
72impl From<NormalizeError> for SmoothError<'_> {
73    fn from(item: NormalizeError) -> Self {
74        Self::NormalizeError(item)
75    }
76}
77
78impl fmt::Display for SmoothError<'_> {
79    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80        match self {
81            SmoothError::NormalizeError(err) => write!(f, "path normalize error {}", err),
82            SmoothError::Pandoc(err) => write!(f, "{}", err),
83            SmoothError::LibreOffice(err) => write!(f, "{}", err),
84            SmoothError::InputFileNotFound(given, normalized) => match given == &normalized.as_os_str() {
85                true => write!(
86                    f,
87                    "input file \"{}\" couldn't be found",
88                    given,
89                ),
90                false => write!(
91                    f,
92                    "input file \"{}\" couldn't be found under normalized path \"{}\"",
93                    given,
94                    normalized.display(),
95                ),
96            },
97            SmoothError::MetadataRead(path) => write!(
98                f,
99                "YAML header for input file \"{}\" couldn't be read",
100                path
101            ),
102            SmoothError::JsonTemplateExists(path) => write!(
103                f,
104                "pandoc template for extracting the metadata as JSON already present under \"{}\" please remove this file manually before proceeding",
105                path.display()
106            ),
107            SmoothError::CreateJsonTemplateFailed(path, why) => write!(
108                f,
109                "couldn't write temporary metadata-as-JSON template to {} {}",
110                path.display(),
111                why
112            ),
113            SmoothError::MetadataParseFailure(err) => write!(
114                f,
115                "couldn't parse frontmatter metadata header of document {}",
116                err
117            ),
118            SmoothError::RemoveJsonTemplateFailed(path, why) => write!(
119                f,
120                "couldn't remove temporary metadata-as-JSON template under {} {}",
121                path.display(),
122                why
123            ),
124            SmoothError::TemplateNotFound(path) => write!(
125                f,
126                "couldn't find template file under {}",
127                path.display()
128            ),
129            SmoothError::ReferenceNotFound(path) => write!(
130                f,
131                "couldn't find reference file under {}",
132                path.display()
133            ),
134            SmoothError::BibliographyNotFound(path) => write!(
135                f,
136                "couldn't find bibliography file under {}",
137                path.display()
138            ),
139            SmoothError::CitationStyleNotFound(path) => write!(
140                f,
141                "couldn't find citation style (csl) file under {}",
142                path.display()
143            ),
144            SmoothError::TemporaryFile(err) => write!(
145                f,
146                "couldn't create temporary file {}",
147                err
148            ),
149            SmoothError::ReadSourceFailed(file, err) => write!(
150                f,
151                "couldn't read the content of the given markdown file {} {}",
152                file.display(),
153                err
154            ),
155            SmoothError::FileCreateFailed(file, err) => write!(
156                f,
157                "couldn't create file {} {}",
158                file.display(),
159                err,
160            ),
161            SmoothError::WriteFailed(file, err) => write!(
162                f,
163                "couldn't write to file {} {}",
164                file.display(),
165                err,
166            ),
167            SmoothError::NoParentFolder(file) => write!(
168                f,
169                "no parent folder for path {} found",
170                file.display(),
171            ),
172            SmoothError::Tera(error) => write!(
173                f,
174                "error in Tera templating engine {}",
175                error,
176            ),
177            SmoothError::IncompatibleReferenceFile(file, format) => write!(
178                f,
179                "reference file {} isn't compatible to output format {}",
180                file.display(),
181                format
182            ),
183        }
184    }
185}
186
187impl fmt::Debug for SmoothError<'_> {
188    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189        write!(f, "{}", self)
190    }
191}
192
193impl Error for SmoothError<'_> {}