tpnote_lib/
error.rs

1//! Custom error types.
2
3use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// The error `InvalidFrontMatterYaml` prints the front matter section of the
8/// note file. This constant limits the number of text lines that are printed.
9pub const FRONT_MATTER_ERROR_MAX_LINES: usize = 20;
10
11/// Error related to the clipboard or `stdin` input stream.
12#[derive(Debug, Error, PartialEq)]
13pub enum InputStreamError {
14    /// Remedy: Prepend HTML input data with `<!DOCTYPE html>` or `<html>`
15    /// with a doc type other than `<!DOCTYPE html>`.
16    #[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/// Configuration file related file system and syntax errors.
25#[derive(Debug, Error)]
26pub enum FileError {
27    /// Remedy: delete all files in configuration file directory.
28    #[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/// Configuration file related semantic errors.
46#[derive(Debug, Error, Clone, PartialEq)]
47pub enum LibCfgError {
48    /// `CfgVal` can only be deserialized with data whose root element
49    /// is a `Value::Table`.
50    /// This should not happen. Please file a bug report.
51    #[error("Input data root must be a `Value::Table`")]
52    CfgValInputIsNotTable,
53
54    /// Remedy: Choose another scheme.
55    #[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    /// Remedy: Choose a value in the given interval.
69    #[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    /// Remedy: Choose another `sort_tag.extra_separator` character.
80    #[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    /// Remedy: Choose another `extension_default` out of
97    /// `extensions[..].0`.
98    #[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    /// Remedy: Insert `sort_tag.separator` in `sort_tag.extra_chars`.
114    #[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    /// Remedy: Choose a `copy_counter.extra_separator` in the set.
132    #[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    /// Remedy: check the configuration file variable `tmpl.filter.assert_preconditions`.
147    #[error("choose one of: `IsDefined`, `IsString`, `IsNumber`, `IsStringOrNumber`, `IsBool`, `IsValidSortTag`")]
148    ParseAssertPrecondition,
149
150    /// Remedy: check the configuration file variable `arg_default.export_link_rewriting`.
151    #[error("choose one of: `off`, `short` or `long`")]
152    ParseLocalLinkKind,
153
154    /// Remedy: check the ISO 639-1 codes in the configuration variable
155    /// `tmpl.filter.get_lang.language_candidates` and make sure that they are
156    /// supported, by checking `tpnote -V`.
157    #[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    /// Remedy: add one more ISO 639-1 code in the configuration variable
171    /// `tmpl.filter.get_lang.language_candidates` (or in
172    /// `TPNOTE_LANG_DETECTION`) and make sure that the code is supported, by
173    /// checking `tpnote -V`.
174    #[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    /// Remedy: correct the variable by choosing one the available themes.
186    #[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)]
203/// Error type returned form methods in or related to the `note` module.
204pub enum NoteError {
205    /// Remedy: make sure, that a file starting with `path` exists.
206    #[error("<NONE FOUND: {path}...>")]
207    CanNotExpandShorthandLink { path: String },
208
209    /// Remedy: Choose another scheme.
210    #[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    /// Remedy: remove invalid characters.
226    #[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    /// Remedy: choose another sort-tag.
243    #[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    /// Remedy: index the compound type?
260    #[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    /// Remedy: try to enclose with quotes.
275    #[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    /// Remedy: try to remove possible quotes.
289    #[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    /// Remedy: try to remove possible quotes.
305    #[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    /// Remedy: try to enclose with quotes.
321    #[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    /// Remedy: correct the front matter variable `file_ext`.
337    #[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    /// Remedy: add the missing field in the note's front matter.
355    #[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    /// Remedy: check front matter delimiters `----`.
372    #[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    /// Remedy: check YAML syntax in the note's front matter.
388    #[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    /// Remedy: check YAML syntax in the input stream's front matter.
401    #[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    /// Remedy: check HTML syntax in the input stream data.
412    #[error(
413        "Invalid HTML in the input stream data found:\n\
414        {source_str}"
415    )]
416    InvalidHtml { source_str: String },
417
418    /// Remedy: reconfigure `scheme.filename.extensions.1`.
419    #[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    /// Remedy: correct link path.
426    #[error("<INVALID: {path}>")]
427    InvalidLocalPath { path: String },
428
429    /// Remedy: check the file permission of the note file.
430    #[error("Can not read file:\n\t {path:?}\n{source}")]
431    Read { path: PathBuf, source: io::Error },
432
433    /// Remedy: check ReStructuredText syntax.
434    #[error("Can not parse reStructuredText input:\n{msg}")]
435    #[cfg(feature = "renderer")]
436    RstParse { msg: String },
437
438    /// Remedy: restart with `--debug trace`.
439    #[error(
440        "Tera error:\n\
441         {source}"
442    )]
443    Tera {
444        #[from]
445        source: tera::Error,
446    },
447
448    /// Remedy: check the syntax of the Tera template in the configuration file.
449    #[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 to construct a `NoteError::TeraTemplate from a `Tera::Error` .
475#[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                // Remove useless information.
483                .trim_end_matches("in context while rendering '__tera_one_off'")
484                .to_string(),
485            template_str: $t,
486        }
487    };
488}