encre_css/error.rs
1//! Define a custom [`Error`] type.
2use std::{ops::Range, path::PathBuf};
3use thiserror::Error as ErrorTrait;
4
5/// Shorthand for [`Result`] type.
6///
7/// [`Result`]: std::result::Result
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// The custom error type used everywhere in this crate
11#[derive(ErrorTrait, Debug)]
12pub enum Error {
13 /// Indicate that the configuration file is not found.
14 #[error("configuration file `{0}` not found ({1})")]
15 ConfigFileNotFound(PathBuf, std::io::Error),
16
17 /// Indicate that an error happened when parsing a [TOML](https://toml.io) file.
18 #[error("bad configuration file: `{0}`")]
19 ConfigParsing(#[from] toml::de::Error),
20
21 /// Indicate that an error happened when converting an hexadecimal color to an RGB one.
22 #[error("error when converting the hexadecimal color `{0}` to rgb: {1:?}")]
23 HexToRgbConversion(String, String),
24}
25
26/// The kind of parsing error
27#[derive(ErrorTrait, Debug, PartialEq, Eq)]
28pub enum ParseErrorKind<'a> {
29 /// Indicate that the selector is too short to be an existing selector (the shortest selector has 2 characters).
30 #[error("the selector `{0}` is too short to be an existing selector")]
31 TooShort(&'a str),
32
33 /// Indicate that the selector has some variants but no modifier
34 #[error("the selector `{0}` has some variants but no modifier")]
35 VariantsWithoutModifier(&'a str),
36
37 /// Indicate that no plugins were found to handle the selector
38 #[error("no plugins found to handle the selector `{0}`")]
39 UnknownPlugin(&'a str),
40
41 /// Indicate that a variant of the selector does not exist
42 #[error("the variant `{0}` of the selector `{1}` does not exist")]
43 UnknownVariant(&'a str, &'a str),
44}
45
46/// The custom error type used to indicate that an error happened when parsing selectors.
47///
48/// By default, this kind of error is ignored but if you want to check that a selector is valid,
49/// you can use [`utils::check_selectors`].
50///
51/// [`utils::check_selectors`]: crate::utils::check_selectors
52#[derive(Debug, PartialEq, Eq)]
53pub struct ParseError<'a> {
54 /// The location of the error in the provided content
55 pub span: Range<usize>,
56
57 /// The kind of parsing error
58 pub kind: ParseErrorKind<'a>,
59}
60
61impl<'a> ParseError<'a> {
62 pub(crate) fn new(span: Range<usize>, kind: ParseErrorKind<'a>) -> Self {
63 Self { span, kind }
64 }
65}