scraper/
error.rs

1//! Custom error types for diagnostics
2//! Includes re-exported error types from dependencies
3
4use cssparser::{BasicParseErrorKind, ParseErrorKind, Token};
5use selectors::parser::SelectorParseErrorKind;
6
7/// Error type that is returned when calling `Selector::parse`
8#[derive(Debug, Clone)]
9pub enum SelectorErrorKind<'a> {
10    /// A `Token` was not expected
11    UnexpectedToken(Token<'a>),
12
13    /// End-Of-Line was unexpected
14    EndOfLine,
15
16    /// `@` rule is invalid
17    InvalidAtRule(String),
18
19    /// The body of an `@` rule is invalid
20    InvalidAtRuleBody,
21
22    /// The qualified rule is invalid
23    QualRuleInvalid,
24
25    /// Expected a `::` for a pseudoelement
26    ExpectedColonOnPseudoElement(Token<'a>),
27
28    /// Expected an identity for a pseudoelement
29    ExpectedIdentityOnPseudoElement(Token<'a>),
30
31    /// A `SelectorParseErrorKind` error that isn't really supposed to happen did
32    UnexpectedSelectorParseError(SelectorParseErrorKind<'a>),
33}
34
35impl<'a> From<cssparser::ParseError<'a, SelectorParseErrorKind<'a>>> for SelectorErrorKind<'a> {
36    fn from(original: cssparser::ParseError<'a, SelectorParseErrorKind<'a>>) -> Self {
37        // NOTE: This could be improved, but I dont
38        // exactly know how
39        match original.kind {
40            ParseErrorKind::Basic(err) => SelectorErrorKind::from(err),
41            ParseErrorKind::Custom(err) => SelectorErrorKind::from(err),
42        }
43    }
44}
45
46impl<'a> From<BasicParseErrorKind<'a>> for SelectorErrorKind<'a> {
47    fn from(err: BasicParseErrorKind<'a>) -> Self {
48        match err {
49            BasicParseErrorKind::UnexpectedToken(token) => Self::UnexpectedToken(token),
50            BasicParseErrorKind::EndOfInput => Self::EndOfLine,
51            BasicParseErrorKind::AtRuleInvalid(rule) => Self::InvalidAtRule(rule.to_string()),
52            BasicParseErrorKind::AtRuleBodyInvalid => Self::InvalidAtRuleBody,
53            BasicParseErrorKind::QualifiedRuleInvalid => Self::QualRuleInvalid,
54        }
55    }
56}
57
58impl<'a> From<SelectorParseErrorKind<'a>> for SelectorErrorKind<'a> {
59    fn from(err: SelectorParseErrorKind<'a>) -> Self {
60        match err {
61            SelectorParseErrorKind::PseudoElementExpectedColon(token) => {
62                Self::ExpectedColonOnPseudoElement(token)
63            }
64            SelectorParseErrorKind::PseudoElementExpectedIdent(token) => {
65                Self::ExpectedIdentityOnPseudoElement(token)
66            }
67            other => Self::UnexpectedSelectorParseError(other),
68        }
69    }
70}