use cssparser::{BasicParseErrorKind, ParseErrorKind};
use selectors::parser::{SelectorParseError, SelectorParseErrorKind};
use thiserror::Error;
#[derive(Error, Debug, Eq, PartialEq, Copy, Clone)]
pub enum SelectorError {
#[error("Unexpected token in selector.")]
UnexpectedToken,
#[error("Unexpected end of selector.")]
UnexpectedEnd,
#[error("Missing attribute name in attribute selector.")]
MissingAttributeName,
#[error("The selector is empty.")]
EmptySelector,
#[error("Dangling combinator in selector.")]
DanglingCombinator,
#[error("Unexpected token in the attribute selector.")]
UnexpectedTokenInAttribute,
#[error("Unsupported pseudo-class or pseudo-element in selector.")]
UnsupportedPseudoClassOrElement,
#[error("Nested negation in selector.")]
NestedNegation,
#[error("Selectors with explicit namespaces are not supported.")]
NamespacedSelector,
#[error("Invalid or unescaped class name in selector.")]
InvalidClassName,
#[error("Empty negation in selector.")]
EmptyNegation,
#[error("Unsupported combinator `{0}` in selector.")]
UnsupportedCombinator(char),
#[error("Unsupported syntax in selector.")]
UnsupportedSyntax,
}
impl From<SelectorParseError<'_>> for SelectorError {
fn from(err: SelectorParseError) -> Self {
#[deny(clippy::wildcard_enum_match_arm)]
match err.kind {
ParseErrorKind::Basic(err) => match err {
BasicParseErrorKind::UnexpectedToken(_) => SelectorError::UnexpectedToken,
BasicParseErrorKind::EndOfInput => SelectorError::UnexpectedEnd,
BasicParseErrorKind::AtRuleBodyInvalid
| BasicParseErrorKind::AtRuleInvalid(_)
| BasicParseErrorKind::QualifiedRuleInvalid => SelectorError::UnsupportedSyntax,
},
ParseErrorKind::Custom(err) => match err {
SelectorParseErrorKind::NoQualifiedNameInAttributeSelector(_) => {
SelectorError::MissingAttributeName
}
SelectorParseErrorKind::EmptySelector => SelectorError::EmptySelector,
SelectorParseErrorKind::DanglingCombinator => SelectorError::DanglingCombinator,
SelectorParseErrorKind::UnsupportedPseudoClassOrElement(_)
| SelectorParseErrorKind::PseudoElementInComplexSelector
| SelectorParseErrorKind::NonPseudoElementAfterSlotted
| SelectorParseErrorKind::InvalidPseudoElementAfterSlotted
| SelectorParseErrorKind::PseudoElementExpectedColon(_)
| SelectorParseErrorKind::PseudoElementExpectedIdent(_)
| SelectorParseErrorKind::NoIdentForPseudo(_)
| SelectorParseErrorKind::NonCompoundSelector
| SelectorParseErrorKind::NonSimpleSelectorInNegation => {
SelectorError::UnsupportedPseudoClassOrElement
}
SelectorParseErrorKind::UnexpectedIdent(_) => SelectorError::NestedNegation,
SelectorParseErrorKind::ExpectedNamespace(_) => SelectorError::NamespacedSelector,
SelectorParseErrorKind::ExplicitNamespaceUnexpectedToken(_) => {
SelectorError::UnexpectedToken
}
SelectorParseErrorKind::UnexpectedTokenInAttributeSelector(_)
| SelectorParseErrorKind::ExpectedBarInAttr(_)
| SelectorParseErrorKind::BadValueInAttr(_)
| SelectorParseErrorKind::InvalidQualNameInAttr(_) => {
SelectorError::UnexpectedTokenInAttribute
}
SelectorParseErrorKind::ClassNeedsIdent(_) => SelectorError::InvalidClassName,
SelectorParseErrorKind::EmptyNegation => SelectorError::EmptyNegation,
SelectorParseErrorKind::InvalidState => panic!("invalid state"),
},
}
}
}