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
14pub enum SmoothError<'a> {
16 NormalizeError(NormalizeError),
18 Pandoc(PandocError<'a>),
21 LibreOffice(LibreOfficeError),
23 InputFileNotFound(&'a str, PathBuf),
25 MetadataRead(&'a str),
28 JsonTemplateExists(PathBuf),
31 CreateJsonTemplateFailed(PathBuf, IOError),
34 MetadataParseFailure(JsonError),
36 RemoveJsonTemplateFailed(PathBuf, IOError),
39 TemplateNotFound(PathBuf),
42 ReferenceNotFound(PathBuf),
45 BibliographyNotFound(PathBuf),
48 CitationStyleNotFound(PathBuf),
51 TemporaryFile(IOError),
53 ReadSourceFailed(PathBuf, IOError),
55 FileCreateFailed(PathBuf, IOError),
59 WriteFailed(PathBuf, IOError),
61 NoParentFolder(PathBuf),
63 Tera(TeraError),
65 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<'_> {}