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(
148 "choose one of: `IsDefined`, `IsString`, `IsNumber`, `IsStringOrNumber`, `IsBool`, `IsValidSortTag`"
149 )]
150 ParseAssertPrecondition,
151
152 #[error("choose one of: `off`, `short` or `long`")]
154 ParseLocalLinkKind,
155
156 #[error(
160 "The ISO 639-1 language subtag `{language_code}`\n\
161 in the configuration file variable\n\
162 `tmpl.filter.get_lang.language_candidates` or in the\n\
163 environment variable `TPNOTE_LANG_DETECTION` is not\n\
164 supported. All listed codes must be part of the set:\n\
165 {all_langs}."
166 )]
167 ParseLanguageCode {
168 language_code: String,
169 all_langs: String,
170 },
171
172 #[error(
177 "Not enough languages to choose from.\n\
178 The list of ISO 639-1 language subtags\n\
179 currently contains only one item: `{language_code}`.\n\
180 Add one more language to the configuration \n\
181 file variable `tmpl.filter.get_lang` or to the\n\
182 environment variable `TPNOTE_LANG_DETECTION`\n\
183 to prevent this error from occurring."
184 )]
185 NotEnoughLanguageCodes { language_code: String },
186
187 #[error(
189 "Configuration file error in section `[tmp_html]` in line:\n\
190 \t{var} = \"{value}\"\n\
191 The theme must be one of the following set:\n\
192 {available}"
193 )]
194 HighlightingThemeName {
195 var: String,
196 value: String,
197 available: String,
198 },
199
200 #[error(transparent)]
201 Deserialize(#[from] toml::de::Error),
202}
203
204#[derive(Debug, Error)]
205pub enum NoteError {
207 #[error("<NONE FOUND: {path}...>")]
209 CanNotExpandShorthandLink { path: String },
210
211 #[error(
213 "Invalid header variable value: no scheme `{scheme_val}` found.\n\
214 \t---\n\
215 \t{scheme_key}: {scheme_val}\n\
216 \t---\n\n\
217 Available schemes in configuration file:\n\
218 {schemes}
219 "
220 )]
221 SchemeNotFound {
222 scheme_val: String,
223 scheme_key: String,
224 schemes: String,
225 },
226
227 #[error(
229 "The `sort_tag` header variable contains invalid\n\
230 character(s):\n\n\
231 \t---\n\
232 \tsort_tag: {sort_tag}\n\
233 \t---\n\n\
234 Only the characters: \"{sort_tag_extra_chars}\", `0..9`\n\
235 and `a..z` (maximum {filename_sort_tag_letters_in_succession_max} in \
236 succession) are allowed."
237 )]
238 FrontMatterFieldIsInvalidSortTag {
239 sort_tag: String,
240 sort_tag_extra_chars: String,
241 filename_sort_tag_letters_in_succession_max: u8,
242 },
243
244 #[error(
246 "This `sort_tag` header variable is a sequential sort-tag:\n\
247 \t---\n\
248 \tsort_tag: {sort_tag}\n\
249 \t---\n\n\
250 A file with this sort-tag exists already on disk:\n\n\
251 \t`{existing_file}`\n\n\
252 For sequential sort-tags no duplicates are allowed.\n\
253 Please choose another sort-tag.
254 "
255 )]
256 FrontMatterFieldIsDuplicateSortTag {
257 sort_tag: String,
258 existing_file: String,
259 },
260
261 #[error(
263 "The type of the front matter field `{field_name}:`\n\
264 must not be a compound type. Use a simple type, \n\
265 i.e. `String`, `Number` or `Bool` instead. Example:\n\
266 \n\
267 \t~~~~~~~~~~~~~~\n\
268 \t---\n\
269 \t{field_name}: My simple type\n\
270 \t---\n\
271 \tsome text\n\
272 \t~~~~~~~~~~~~~~"
273 )]
274 FrontMatterFieldIsCompound { field_name: String },
275
276 #[error(
278 "The (sub)type of the front matter field `{field_name}:`\n\
279 must be a non empty `String`. Example:\n\
280 \n\
281 \t~~~~~~~~~~~~~~\n\
282 \t---\n\
283 \t{field_name}: My string\n\
284 \t---\n\
285 \tsome text\n\
286 \t~~~~~~~~~~~~~~"
287 )]
288 FrontMatterFieldIsEmptyString { field_name: String },
289
290 #[error(
292 "The (sub)type of the front matter field `{field_name}:`\n\
293 must be `Bool`. Example:\n\
294 \n\
295 \t~~~~~~~~~~~~~~\n\
296 \t---\n\
297 \t{field_name}: false\n\
298 \t---\n\
299 \tsome text\n\
300 \t~~~~~~~~~~~~~~\n\
301 \n\
302 Hint: try to remove possible quotes."
303 )]
304 FrontMatterFieldIsNotBool { field_name: String },
305
306 #[error(
308 "The (sub)type of the front matter field `{field_name}:`\n\
309 must be `Number`. Example:\n\
310 \n\
311 \t~~~~~~~~~~~~~~\n\
312 \t---\n\
313 \t{field_name}: 142\n\
314 \t---\n\
315 \tsome text\n\
316 \t~~~~~~~~~~~~~~\n\
317 \n\
318 Hint: try to remove possible quotes."
319 )]
320 FrontMatterFieldIsNotNumber { field_name: String },
321
322 #[error(
324 "The (sub)type of the front matter field `{field_name}:`\n\
325 must be `String`. Example:\n\
326 \n\
327 \t~~~~~~~~~~~~~~\n\
328 \t---\n\
329 \t{field_name}: My string\n\
330 \t---\n\
331 \tsome text\n\
332 \t~~~~~~~~~~~~~~\n\
333 \n\
334 Hint: try to enclose with quotes."
335 )]
336 FrontMatterFieldIsNotString { field_name: String },
337
338 #[error(
340 "The file extension:\n\
341 \t---\n\
342 \tfile_ext: {extension}\n\
343 \t---\n\
344 is not registered as Tp-Note file in\n\
345 your configuration file:\n\
346 \t{extensions}\n\
347 \n\
348 Choose one of the listed above or add more extensions to the\n\
349 `filename.extensions` variable in your configuration file."
350 )]
351 FrontMatterFieldIsNotTpnoteExtension {
352 extension: String,
353 extensions: String,
354 },
355
356 #[error(
358 "The document is missing a `{field_name}:`\n\
359 field in its front matter:\n\
360 \n\
361 \t~~~~~~~~~~~~~~\n\
362 \t---\n\
363 \t{field_name}: \"My note\"\n\
364 \t---\n\
365 \tsome text\n\
366 \t~~~~~~~~~~~~~~\n\
367 \n\
368 Please correct the front matter if this is\n\
369 supposed to be a Tp-Note file. Ignore otherwise."
370 )]
371 FrontMatterFieldMissing { field_name: String },
372
373 #[error(
375 "The document (or template) has no front matter\n\
376 section. Is one `---` missing?\n\n\
377 \t~~~~~~~~~~~~~~\n\
378 \t---\n\
379 \t{compulsory_field}: My note\n\
380 \t---\n\
381 \tsome text\n\
382 \t~~~~~~~~~~~~~~\n\
383 \n\
384 Please correct the front matter if this is\n\
385 supposed to be a Tp-Note file. Ignore otherwise."
386 )]
387 FrontMatterMissing { compulsory_field: String },
388
389 #[error(
391 "Can not parse front matter:\n\
392 \n\
393 {front_matter}\
394 \n\
395 {source_error}"
396 )]
397 InvalidFrontMatterYaml {
398 front_matter: String,
399 source_error: serde_yaml::Error,
400 },
401
402 #[error(
404 "Invalid YAML field(s) in the {tmpl_var} input\n\
405 stream data found:\n\
406 {source_str}"
407 )]
408 InvalidInputYaml {
409 tmpl_var: String,
410 source_str: String,
411 },
412
413 #[error(
415 "Invalid HTML in the input stream data found:\n\
416 {source_str}"
417 )]
418 InvalidHtml { source_str: String },
419
420 #[error(
422 "Filter `html_to_markup` is disabled for this \n\
423 `extension_default` in table `scheme.filename.extensions.1`."
424 )]
425 HtmlToMarkupDisabled,
426
427 #[error("<INVALID: {path}>")]
429 InvalidLocalPath { path: String },
430
431 #[error("Can not read file:\n\t {path:?}\n{source}")]
433 Read { path: PathBuf, source: io::Error },
434
435 #[error("Can not parse reStructuredText input:\n{msg}")]
437 #[cfg(feature = "renderer")]
438 RstParse { msg: String },
439
440 #[error(
442 "Tera error:\n\
443 {source}"
444 )]
445 Tera {
446 #[from]
447 source: tera::Error,
448 },
449
450 #[error(
452 "Tera template error in configuration file\n\
453 variable \"{template_str}\":\n {source_str}"
454 )]
455 TeraTemplate {
456 source_str: String,
457 template_str: String,
458 },
459
460 #[error(transparent)]
461 File(#[from] FileError),
462
463 #[error(transparent)]
464 Io(#[from] std::io::Error),
465
466 #[error(transparent)]
467 ParseLanguageCode(#[from] LibCfgError),
468
469 #[error(transparent)]
470 Utf8Conversion {
471 #[from]
472 source: core::str::Utf8Error,
473 },
474}
475
476#[macro_export]
478macro_rules! note_error_tera_template {
479 ($e:ident, $t:expr) => {
480 NoteError::TeraTemplate {
481 source_str: std::error::Error::source(&$e)
482 .unwrap_or(&tera::Error::msg(""))
483 .to_string()
484 .trim_end_matches("in context while rendering '__tera_one_off'")
486 .to_string(),
487 template_str: $t,
488 }
489 };
490}