1use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7pub const FRONT_MATTER_ERROR_MAX_LINES: usize = 20;
10
11#[derive(Debug, Error, PartialEq)]
13pub enum InputStreamError {
14 #[error(
17 "The HTML input stream starts with a doctype other than\n\
18 \"<!DOCTYPE html>\":\n\
19 {html}"
20 )]
21 NonHtmlDoctype { html: String },
22}
23
24#[derive(Debug, Error)]
26pub enum FileError {
27 #[error(
29 "Can not find unused filename in directory:\n\
30 \t{directory:?}\n\
31 (only `COPY_COUNTER_MAX` copies are allowed)."
32 )]
33 NoFreeFileName { directory: PathBuf },
34
35 #[error(transparent)]
36 Io(#[from] std::io::Error),
37
38 #[error(transparent)]
39 Serialize(#[from] toml::ser::Error),
40
41 #[error(transparent)]
42 Deserialize(#[from] toml::de::Error),
43}
44
45#[derive(Debug, Error, Clone, PartialEq)]
47pub enum LibCfgError {
48 #[error("Input data root must be a `Value::Table`")]
52 CfgValInputIsNotTable,
53
54 #[error(
56 "Configuration file error in section:\n\
57 \t[[scheme]]\n\
58 \tscheme_default = \"{scheme_name}\"\n\
59 No scheme found. Available configured schemes:\n\
60 {schemes}
61 "
62 )]
63 SchemeNotFound {
64 scheme_name: String,
65 schemes: String,
66 },
67
68 #[error(
70 "Configuration file error in [base_scheme] or in section:\n\
71 \t[[scheme]]\n\
72 \tname = \"{scheme_name}\"\n\
73 \t[scheme.tmpl]\n\
74 \tfilter.get_lang.relative_distance_min={dist}\n\
75 must be between 0.0 and 0.99."
76 )]
77 MinimumRelativeDistanceInvalid { scheme_name: String, dist: f64 },
78
79 #[error(
81 "Configuration file error in [base_scheme] or in section:\n\
82 \t[[scheme]]\n\
83 \tname = \"{scheme_name}\"
84 \t[scheme.filename]\n\
85 \tsort_tag.extra_separator=\"{extra_separator}\"\n\
86 must not be one of `sort_tag_extra_chars=\"{sort_tag_extra_chars}\"`,\n\
87 `0..9`, `a..z` or `{dot_file_marker}`."
88 )]
89 SortTagExtraSeparator {
90 scheme_name: String,
91 dot_file_marker: char,
92 sort_tag_extra_chars: String,
93 extra_separator: String,
94 },
95
96 #[error(
99 "Configuration file error in [base_scheme] or in section:\n\
100 \t[[scheme]]\n\
101 \tname = \"{scheme_name}\"
102 \t[scheme.filename]\n\
103 \t`extension_default=\"{extension_default}\"\n\
104 must not be one of:`\n\
105 \t{extensions}."
106 )]
107 ExtensionDefault {
108 scheme_name: String,
109 extension_default: String,
110 extensions: String,
111 },
112
113 #[error(
115 "Configuration file error in [base_scheme] or in section:\n\
116 \t[[scheme]]\n\
117 \tname = \"{scheme_name}\"
118 \t[scheme.filename]\n\
119 All characters in `sort_tag.separator=\"{separator}\"\n\
120 must be in the set `sort_tag.extra_chars=\"{chars}\"`,\n\
121 or in `0..9`, `a..z``\n\
122 must NOT start with `{dot_file_marker}`."
123 )]
124 SortTagSeparator {
125 scheme_name: String,
126 dot_file_marker: char,
127 chars: String,
128 separator: String,
129 },
130
131 #[error(
133 "Configuration file error in [base_scheme] or in section:\n\
134 \t[[scheme]]\n\
135 \tname = \"{scheme_name}\"
136 \t[scheme.filename]\n\
137 `copy_counter.extra_separator=\"{extra_separator}\"`\n\
138 must be one of: \"{chars}\""
139 )]
140 CopyCounterExtraSeparator {
141 scheme_name: String,
142 chars: String,
143 extra_separator: String,
144 },
145
146 #[error("choose one of: `IsDefined`, `IsString`, `IsNumber`, `IsStringOrNumber`, `IsBool`, `IsValidSortTag`")]
148 ParseAssertPrecondition,
149
150 #[error("choose one of: `off`, `short` or `long`")]
152 ParseLocalLinkKind,
153
154 #[error(
158 "The ISO 639-1 language subtag `{language_code}`\n\
159 in the configuration file variable\n\
160 `tmpl.filter.get_lang.language_candidates` or in the\n\
161 environment variable `TPNOTE_LANG_DETECTION` is not\n\
162 supported. All listed codes must be part of the set:\n\
163 {all_langs}."
164 )]
165 ParseLanguageCode {
166 language_code: String,
167 all_langs: String,
168 },
169
170 #[error(
175 "Not enough languages to choose from.\n\
176 The list of ISO 639-1 language subtags\n\
177 currently contains only one item: `{language_code}`.\n\
178 Add one more language to the configuration \n\
179 file variable `tmpl.filter.get_lang` or to the\n\
180 environment variable `TPNOTE_LANG_DETECTION`\n\
181 to prevent this error from occurring."
182 )]
183 NotEnoughLanguageCodes { language_code: String },
184
185 #[error(
187 "Configuration file error in section `[tmp_html]` in line:\n\
188 \t{var} = \"{value}\"\n\
189 The theme must be one of the following set:\n\
190 {available}"
191 )]
192 HighlightingThemeName {
193 var: String,
194 value: String,
195 available: String,
196 },
197
198 #[error(transparent)]
199 Deserialize(#[from] toml::de::Error),
200}
201
202#[derive(Debug, Error)]
203pub enum NoteError {
205 #[error("<NONE FOUND: {path}...>")]
207 CanNotExpandShorthandLink { path: String },
208
209 #[error(
211 "Invalid header variable value: no scheme `{scheme_val}` found.\n\
212 \t---\n\
213 \t{scheme_key}: {scheme_val}\n\
214 \t---\n\n\
215 Available schemes in configuration file:\n\
216 {schemes}
217 "
218 )]
219 SchemeNotFound {
220 scheme_val: String,
221 scheme_key: String,
222 schemes: String,
223 },
224
225 #[error(
227 "The `sort_tag` header variable contains invalid\n\
228 character(s):\n\n\
229 \t---\n\
230 \tsort_tag: {sort_tag}\n\
231 \t---\n\n\
232 Only the characters: \"{sort_tag_extra_chars}\", `0..9`\n\
233 and `a..z` (maximum {filename_sort_tag_letters_in_succession_max} in \
234 succession) are allowed."
235 )]
236 FrontMatterFieldIsInvalidSortTag {
237 sort_tag: String,
238 sort_tag_extra_chars: String,
239 filename_sort_tag_letters_in_succession_max: u8,
240 },
241
242 #[error(
244 "This `sort_tag` header variable is a sequential sort-tag:\n\
245 \t---\n\
246 \tsort_tag: {sort_tag}\n\
247 \t---\n\n\
248 A file with this sort-tag exists already on disk:\n\n\
249 \t`{existing_file}`\n\n\
250 For sequential sort-tags no duplicates are allowed.\n\
251 Please choose another sort-tag.
252 "
253 )]
254 FrontMatterFieldIsDuplicateSortTag {
255 sort_tag: String,
256 existing_file: String,
257 },
258
259 #[error(
261 "The type of the front matter field `{field_name}:`\n\
262 must not be a compound type. Use a simple type, \n\
263 i.e. `String`, `Number` or `Bool` instead. Example:\n\
264 \n\
265 \t~~~~~~~~~~~~~~\n\
266 \t---\n\
267 \t{field_name}: My simple type\n\
268 \t---\n\
269 \tsome text\n\
270 \t~~~~~~~~~~~~~~"
271 )]
272 FrontMatterFieldIsCompound { field_name: String },
273
274 #[error(
276 "The (sub)type of the front matter field `{field_name}:`\n\
277 must be a non empty `String`. Example:\n\
278 \n\
279 \t~~~~~~~~~~~~~~\n\
280 \t---\n\
281 \t{field_name}: My string\n\
282 \t---\n\
283 \tsome text\n\
284 \t~~~~~~~~~~~~~~"
285 )]
286 FrontMatterFieldIsEmptyString { field_name: String },
287
288 #[error(
290 "The (sub)type of the front matter field `{field_name}:`\n\
291 must be `Bool`. Example:\n\
292 \n\
293 \t~~~~~~~~~~~~~~\n\
294 \t---\n\
295 \t{field_name}: false\n\
296 \t---\n\
297 \tsome text\n\
298 \t~~~~~~~~~~~~~~\n\
299 \n\
300 Hint: try to remove possible quotes."
301 )]
302 FrontMatterFieldIsNotBool { field_name: String },
303
304 #[error(
306 "The (sub)type of the front matter field `{field_name}:`\n\
307 must be `Number`. Example:\n\
308 \n\
309 \t~~~~~~~~~~~~~~\n\
310 \t---\n\
311 \t{field_name}: 142\n\
312 \t---\n\
313 \tsome text\n\
314 \t~~~~~~~~~~~~~~\n\
315 \n\
316 Hint: try to remove possible quotes."
317 )]
318 FrontMatterFieldIsNotNumber { field_name: String },
319
320 #[error(
322 "The (sub)type of the front matter field `{field_name}:`\n\
323 must be `String`. Example:\n\
324 \n\
325 \t~~~~~~~~~~~~~~\n\
326 \t---\n\
327 \t{field_name}: My string\n\
328 \t---\n\
329 \tsome text\n\
330 \t~~~~~~~~~~~~~~\n\
331 \n\
332 Hint: try to enclose with quotes."
333 )]
334 FrontMatterFieldIsNotString { field_name: String },
335
336 #[error(
338 "The file extension:\n\
339 \t---\n\
340 \tfile_ext: {extension}\n\
341 \t---\n\
342 is not registered as Tp-Note file in\n\
343 your configuration file:\n\
344 \t{extensions}\n\
345 \n\
346 Choose one of the listed above or add more extensions to the\n\
347 `filename.extensions` variable in your configuration file."
348 )]
349 FrontMatterFieldIsNotTpnoteExtension {
350 extension: String,
351 extensions: String,
352 },
353
354 #[error(
356 "The document is missing a `{field_name}:`\n\
357 field in its front matter:\n\
358 \n\
359 \t~~~~~~~~~~~~~~\n\
360 \t---\n\
361 \t{field_name}: \"My note\"\n\
362 \t---\n\
363 \tsome text\n\
364 \t~~~~~~~~~~~~~~\n\
365 \n\
366 Please correct the front matter if this is\n\
367 supposed to be a Tp-Note file. Ignore otherwise."
368 )]
369 FrontMatterFieldMissing { field_name: String },
370
371 #[error(
373 "The document (or template) has no front matter\n\
374 section. Is one `---` missing?\n\n\
375 \t~~~~~~~~~~~~~~\n\
376 \t---\n\
377 \t{compulsory_field}: My note\n\
378 \t---\n\
379 \tsome text\n\
380 \t~~~~~~~~~~~~~~\n\
381 \n\
382 Please correct the front matter if this is\n\
383 supposed to be a Tp-Note file. Ignore otherwise."
384 )]
385 FrontMatterMissing { compulsory_field: String },
386
387 #[error(
389 "Can not parse front matter:\n\
390 \n\
391 {front_matter}\
392 \n\
393 {source_error}"
394 )]
395 InvalidFrontMatterYaml {
396 front_matter: String,
397 source_error: serde_yaml::Error,
398 },
399
400 #[error(
402 "Invalid YAML field(s) in the {tmpl_var} input\n\
403 stream data found:\n\
404 {source_str}"
405 )]
406 InvalidInputYaml {
407 tmpl_var: String,
408 source_str: String,
409 },
410
411 #[error(
413 "Invalid HTML in the input stream data found:\n\
414 {source_str}"
415 )]
416 InvalidHtml { source_str: String },
417
418 #[error(
420 "Filter `html_to_markup` is disabled for this \n\
421 `extension_default` in table `scheme.filename.extensions.1`."
422 )]
423 HtmlToMarkupDisabled,
424
425 #[error("<INVALID: {path}>")]
427 InvalidLocalPath { path: String },
428
429 #[error("Can not read file:\n\t {path:?}\n{source}")]
431 Read { path: PathBuf, source: io::Error },
432
433 #[error("Can not parse reStructuredText input:\n{msg}")]
435 #[cfg(feature = "renderer")]
436 RstParse { msg: String },
437
438 #[error(
440 "Tera error:\n\
441 {source}"
442 )]
443 Tera {
444 #[from]
445 source: tera::Error,
446 },
447
448 #[error(
450 "Tera template error in configuration file\n\
451 variable \"{template_str}\":\n {source_str}"
452 )]
453 TeraTemplate {
454 source_str: String,
455 template_str: String,
456 },
457
458 #[error(transparent)]
459 File(#[from] FileError),
460
461 #[error(transparent)]
462 Io(#[from] std::io::Error),
463
464 #[error(transparent)]
465 ParseLanguageCode(#[from] LibCfgError),
466
467 #[error(transparent)]
468 Utf8Conversion {
469 #[from]
470 source: core::str::Utf8Error,
471 },
472}
473
474#[macro_export]
476macro_rules! note_error_tera_template {
477 ($e:ident, $t:expr) => {
478 NoteError::TeraTemplate {
479 source_str: std::error::Error::source(&$e)
480 .unwrap_or(&tera::Error::msg(""))
481 .to_string()
482 .trim_end_matches("in context while rendering '__tera_one_off'")
484 .to_string(),
485 template_str: $t,
486 }
487 };
488}