|
| 1 | +//! Custom error types for diagnostics |
| 2 | +//! Includes re-exported error types from dependencies |
| 3 | +
|
| 4 | +mod utils; |
| 5 | + |
| 6 | +use std::{error::Error, fmt::Display}; |
| 7 | + |
| 8 | +use cssparser::{BasicParseErrorKind, ParseErrorKind, Token}; |
| 9 | +use selectors::parser::SelectorParseErrorKind; |
| 10 | + |
| 11 | +/// Error type that is returned when calling `Selector::parse` |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub enum SelectorErrorKind<'a> { |
| 14 | + /// A `Token` was not expected |
| 15 | + UnexpectedToken(Token<'a>), |
| 16 | + |
| 17 | + /// End-Of-Line was unexpected |
| 18 | + EndOfLine, |
| 19 | + |
| 20 | + /// `@` rule is invalid |
| 21 | + InvalidAtRule(String), |
| 22 | + |
| 23 | + /// The body of an `@` rule is invalid |
| 24 | + InvalidAtRuleBody, |
| 25 | + |
| 26 | + /// The qualified rule is invalid |
| 27 | + QualRuleInvalid, |
| 28 | + |
| 29 | + /// Expected a `::` for a pseudoelement |
| 30 | + ExpectedColonOnPseudoElement(Token<'a>), |
| 31 | + |
| 32 | + /// Expected an identity for a pseudoelement |
| 33 | + ExpectedIdentityOnPseudoElement(Token<'a>), |
| 34 | + |
| 35 | + /// A `SelectorParseErrorKind` error that isn't really supposed to happen did |
| 36 | + UnexpectedSelectorParseError(SelectorParseErrorKind<'a>), |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a> From<cssparser::ParseError<'a, SelectorParseErrorKind<'a>>> for SelectorErrorKind<'a> { |
| 40 | + fn from(original: cssparser::ParseError<'a, SelectorParseErrorKind<'a>>) -> Self { |
| 41 | + // NOTE: This could be improved, but I dont |
| 42 | + // exactly know how |
| 43 | + match original.kind { |
| 44 | + ParseErrorKind::Basic(err) => SelectorErrorKind::from(err), |
| 45 | + ParseErrorKind::Custom(err) => SelectorErrorKind::from(err), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'a> From<BasicParseErrorKind<'a>> for SelectorErrorKind<'a> { |
| 51 | + fn from(err: BasicParseErrorKind<'a>) -> Self { |
| 52 | + match err { |
| 53 | + BasicParseErrorKind::UnexpectedToken(token) => Self::UnexpectedToken(token), |
| 54 | + BasicParseErrorKind::EndOfInput => Self::EndOfLine, |
| 55 | + BasicParseErrorKind::AtRuleInvalid(rule) => { |
| 56 | + Self::InvalidAtRule(rule.clone().to_string()) |
| 57 | + } |
| 58 | + BasicParseErrorKind::AtRuleBodyInvalid => Self::InvalidAtRuleBody, |
| 59 | + BasicParseErrorKind::QualifiedRuleInvalid => Self::QualRuleInvalid, |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<'a> From<SelectorParseErrorKind<'a>> for SelectorErrorKind<'a> { |
| 65 | + fn from(err: SelectorParseErrorKind<'a>) -> Self { |
| 66 | + match err { |
| 67 | + SelectorParseErrorKind::PseudoElementExpectedColon(token) => { |
| 68 | + Self::ExpectedColonOnPseudoElement(token) |
| 69 | + } |
| 70 | + SelectorParseErrorKind::PseudoElementExpectedIdent(token) => { |
| 71 | + Self::ExpectedIdentityOnPseudoElement(token) |
| 72 | + } |
| 73 | + other => Self::UnexpectedSelectorParseError(other), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a> Display for SelectorErrorKind<'a> { |
| 79 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 80 | + write!( |
| 81 | + f, |
| 82 | + "{}", |
| 83 | + match self { |
| 84 | + Self::UnexpectedToken(token) => { |
| 85 | + format!("Token {:?} was not expected", utils::render_token(token)) |
| 86 | + } |
| 87 | + Self::EndOfLine => "Unexpected EOL".to_string(), |
| 88 | + Self::InvalidAtRule(rule) => format!("Invalid @-rule {:?}", rule), |
| 89 | + Self::InvalidAtRuleBody => "The body of an @-rule was invalid".to_string(), |
| 90 | + Self::QualRuleInvalid => "The qualified name was invalid".to_string(), |
| 91 | + Self::ExpectedColonOnPseudoElement(token) => format!( |
| 92 | + "Expected a ':' token for pseudoelement, got {:?} instead", |
| 93 | + utils::render_token(token) |
| 94 | + ), |
| 95 | + Self::ExpectedIdentityOnPseudoElement(token) => format!( |
| 96 | + "Expected identity for pseudoelement, got {:?} instead", |
| 97 | + utils::render_token(token) |
| 98 | + ), |
| 99 | + Self::UnexpectedSelectorParseError(err) => format!( |
| 100 | + "Unexpected error occurred. Please report this to the developer\n{:#?}", |
| 101 | + err |
| 102 | + ), |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<'a> Error for SelectorErrorKind<'a> { |
| 109 | + fn description(&self) -> &str { |
| 110 | + match self { |
| 111 | + Self::UnexpectedToken(_) => "Token was not expected", |
| 112 | + Self::EndOfLine => "Unexpected EOL", |
| 113 | + Self::InvalidAtRule(_) => "Invalid @-rule", |
| 114 | + Self::InvalidAtRuleBody => "The body of an @-rule was invalid", |
| 115 | + Self::QualRuleInvalid => "The qualified name was invalid", |
| 116 | + Self::ExpectedColonOnPseudoElement(_) => "Missing colon character on pseudoelement", |
| 117 | + Self::ExpectedIdentityOnPseudoElement(_) => "Missing pseudoelement identity", |
| 118 | + Self::UnexpectedSelectorParseError(_) => "Unexpected error", |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments