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 filesystem 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(
148        "choose one of: `IsDefined`, `IsString`, `IsNumber`, `IsStringOrNumber`, `IsBool`, `IsValidSortTag`"
149    )]
150    ParseAssertPrecondition,
151
152    /// Remedy: check the configuration file variable `arg_default.export_link_rewriting`.
153    #[error("choose one of: `off`, `short` or `long`")]
154    ParseLocalLinkKind,
155
156    /// Remedy: check the ISO 639-1 codes in the configuration variable
157    /// `tmpl.filter.get_lang.language_candidates` and make sure that they are
158    /// supported, by checking `tpnote -V`.
159    #[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    /// Remedy: add one more ISO 639-1 code in the configuration variable
173    /// `tmpl.filter.get_lang.language_candidates` (or in
174    /// `TPNOTE_LANG_DETECTION`) and make sure that the code is supported, by
175    /// checking `tpnote -V`.
176    #[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    /// Remedy: correct the variable by choosing one the available themes.
188    #[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)]
205/// Error type returned form methods in or related to the `note` module.
206pub enum NoteError {
207    /// Remedy: make sure, that a file starting with `path` exists.
208    #[error("<NONE FOUND: {path}...>")]
209    CanNotExpandShorthandLink { path: String },
210
211    /// Remedy: Choose another scheme.
212    #[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    /// Remedy: remove invalid characters.
228    #[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    /// Remedy: choose another sort-tag.
245    #[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    /// Remedy: index the compound type?
262    #[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    /// Remedy: try to enclose with quotes.
277    #[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    /// Remedy: try to remove possible quotes.
291    #[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    /// Remedy: try to remove possible quotes.
307    #[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    /// Remedy: try to enclose with quotes.
323    #[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    /// Remedy: correct the front matter variable `file_ext`.
339    #[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    /// Remedy: add the missing field in the note's front matter.
357    #[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    /// Remedy: check front matter delimiters `----`.
374    #[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    /// Remedy: check YAML syntax in the note's front matter.
390    #[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    /// Remedy: check YAML syntax in the input stream's front matter.
403    #[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    /// Remedy: check HTML syntax in the input stream data.
414    #[error(
415        "Invalid HTML in the input stream data found:\n\
416        {source_str}"
417    )]
418    InvalidHtml { source_str: String },
419
420    /// Remedy: reconfigure `scheme.filename.extensions.1`.
421    #[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    /// Remedy: correct link path.
428    #[error("<INVALID: {path}>")]
429    InvalidLocalPath { path: String },
430
431    /// Remedy: check the file permission of the note file.
432    #[error("Can not read file:\n\t {path:?}\n{source}")]
433    Read { path: PathBuf, source: io::Error },
434
435    /// Remedy: check ReStructuredText syntax.
436    #[error("Can not parse reStructuredText input:\n{msg}")]
437    #[cfg(feature = "renderer")]
438    RstParse { msg: String },
439
440    /// Remedy: restart with `--debug trace`.
441    #[error(
442        "Tera error:\n\
443         {source}"
444    )]
445    Tera {
446        #[from]
447        source: tera::Error,
448    },
449
450    /// Remedy: check the syntax of the Tera template in the configuration file.
451    #[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 to construct a `NoteError::TeraTemplate from a `Tera::Error` .
477#[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                // Remove useless information.
485                .trim_end_matches("in context while rendering '__tera_one_off'")
486                .to_string(),
487            template_str: $t,
488        }
489    };
490}