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.")]
#[deprecated(note = "unused")]
EmptyNegation,
#[error("Unsupported combinator `{0}` in selector.")]
UnsupportedCombinator(char),
#[error("Unsupported syntax in selector.")]
UnsupportedSyntax,
}
impl From<SelectorParseError<'_>> for SelectorError {
#[cold]
fn from(err: SelectorParseError<'_>) -> Self {
#[deny(clippy::wildcard_enum_match_arm)]
match err.kind {
ParseErrorKind::Basic(err) => match err {
BasicParseErrorKind::UnexpectedToken(_) => Self::UnexpectedToken,
BasicParseErrorKind::EndOfInput => Self::UnexpectedEnd,
BasicParseErrorKind::AtRuleBodyInvalid
| BasicParseErrorKind::AtRuleInvalid(_)
| BasicParseErrorKind::QualifiedRuleInvalid => Self::UnsupportedSyntax,
},
ParseErrorKind::Custom(err) => match err {
SelectorParseErrorKind::NoQualifiedNameInAttributeSelector(_) => {
Self::MissingAttributeName
}
SelectorParseErrorKind::EmptySelector => Self::EmptySelector,
SelectorParseErrorKind::DanglingCombinator => Self::DanglingCombinator,
SelectorParseErrorKind::UnsupportedPseudoClassOrElement(_)
| SelectorParseErrorKind::InvalidPseudoElementInsideWhere
| SelectorParseErrorKind::NonPseudoElementAfterSlotted
| SelectorParseErrorKind::InvalidPseudoElementAfterSlotted
| SelectorParseErrorKind::PseudoElementExpectedColon(_)
| SelectorParseErrorKind::PseudoElementExpectedIdent(_)
| SelectorParseErrorKind::NoIdentForPseudo(_)
| SelectorParseErrorKind::NonCompoundSelector => {
Self::UnsupportedPseudoClassOrElement
}
SelectorParseErrorKind::UnexpectedIdent(_) => {
debug_assert!(false);
Self::UnsupportedSyntax
},
SelectorParseErrorKind::ExpectedNamespace(_) => Self::NamespacedSelector,
SelectorParseErrorKind::ExplicitNamespaceUnexpectedToken(_) => {
Self::UnexpectedToken
}
SelectorParseErrorKind::UnexpectedTokenInAttributeSelector(_)
| SelectorParseErrorKind::ExpectedBarInAttr(_)
| SelectorParseErrorKind::BadValueInAttr(_)
| SelectorParseErrorKind::InvalidQualNameInAttr(_) => {
Self::UnexpectedTokenInAttribute
}
SelectorParseErrorKind::ClassNeedsIdent(_) => Self::InvalidClassName,
SelectorParseErrorKind::InvalidState => {
debug_assert!(false, "invalid state");
Self::UnsupportedSyntax
}
},
}
}
}