pochoir_parser/
error.rs

1use pochoir_common::Spanned;
2use std::result;
3use thiserror::Error;
4
5pub type Result<T> = result::Result<T, Spanned<Error>>;
6
7pub type SelectorParseError<'a> = selectors::parser::SelectorParseError<'a>;
8
9#[derive(Error, Debug, PartialEq, Eq, Clone)]
10pub enum Error {
11    #[error("expected {expected:?}, found {found:?}")]
12    UnexpectedInput { expected: String, found: String },
13
14    #[error("invalid tag name: {0:?}")]
15    InvalidTagName(String),
16
17    #[error("invalid attribute name: {0:?}")]
18    InvalidAttributeName(String),
19
20    #[error("end tag {end_tag:?} found when trying to close {start_tag:?}")]
21    UnexpectedEndTagName { start_tag: String, end_tag: String },
22
23    #[error("unexpected end tag {0:?}")]
24    UnexpectedEndTag(String),
25
26    #[error("expected tag name")]
27    ExpectedTagName,
28
29    #[error("element {0:?} is a void element and must not be closed")]
30    ClosedVoidElement(String),
31
32    #[error("missing whitespace between attributes")]
33    MissingWhitespaceBetweenAttributes,
34
35    #[error("an end \">\" is missing to close the tag")]
36    MissingEndAngleBracket,
37
38    /// An error happening in an event handler.
39    #[error("an error happened when executing an event handler: {0}")]
40    EventHandlerError(String),
41
42    #[error("method not allowed on a node of type `{found}`, expected one of type `{expected}`")]
43    BadNodeType {
44        expected: &'static str,
45        found: &'static str,
46    },
47
48    #[error(transparent)]
49    TemplateError(#[from] pochoir_template_engine::Error),
50
51    #[error(transparent)]
52    StreamParserError(#[from] pochoir_common::Error),
53}
54
55pub(crate) trait AutoError<T>
56where
57    Self: Sized,
58{
59    fn auto_error(self) -> T;
60}
61
62impl<T> AutoError<result::Result<T, Spanned<Error>>>
63    for result::Result<T, Spanned<pochoir_common::Error>>
64{
65    fn auto_error(self) -> result::Result<T, Spanned<Error>> {
66        self.map_err(|e| e.map_spanned(Error::StreamParserError))
67    }
68}
69
70impl<T> AutoError<result::Result<T, Spanned<Error>>>
71    for result::Result<T, Spanned<pochoir_template_engine::Error>>
72{
73    fn auto_error(self) -> result::Result<T, Spanned<Error>> {
74        self.map_err(|e| e.map_spanned(Error::TemplateError))
75    }
76}