accessibility_rs/engine/styles/
rules.rs

1use crate::engine::styles::errors::RuleParseErrorKind;
2use accessibility_scraper::selector::Simple;
3use cssparser::{AtRuleParser, ParseError, QualifiedRuleParser};
4use std::sync::Arc;
5
6#[derive(Debug)]
7/// Css rules to match selectors
8pub enum CssRule {
9    /// the style rules for css
10    StyleRule {
11        /// css selectors list with selector trait
12        selectors: selectors::SelectorList<Simple>,
13        /// a css block parsed
14        block: Arc<String>,
15    },
16}
17
18/// css parser
19pub struct Parser;
20
21impl<'i> selectors::parser::Parser<'i> for Parser {
22    type Impl = Simple;
23    type Error = RuleParseErrorKind<'i>;
24}
25
26/// css selector list
27pub type SelectorList = selectors::SelectorList<Simple>;
28// pub type Selector = selectors::parser::Selector<Simple>;
29
30/// css rules parser
31pub struct RulesParser;
32
33impl<'i> QualifiedRuleParser<'i> for RulesParser {
34    type Prelude = SelectorList;
35    type QualifiedRule = CssRule;
36    type Error = RuleParseErrorKind<'i>;
37
38    fn parse_prelude<'t>(
39        &mut self,
40        parser: &mut cssparser::Parser<'i, 't>,
41    ) -> Result<Self::Prelude, ParseError<'i, Self::Error>> {
42        SelectorList::parse(&Parser, parser)
43    }
44
45    fn parse_block<'t>(
46        &mut self,
47        prelude: Self::Prelude,
48        _location: cssparser::SourceLocation,
49        _parser: &mut cssparser::Parser<'i, 't>,
50    ) -> Result<Self::QualifiedRule, ParseError<'i, Self::Error>> {
51        Ok(CssRule::StyleRule {
52            selectors: prelude,
53            block: Arc::new(String::new()),
54        })
55    }
56}
57
58impl<'i> AtRuleParser<'i> for RulesParser {
59    type PreludeBlock = ();
60    type PreludeNoBlock = ();
61    type AtRule = CssRule;
62    type Error = RuleParseErrorKind<'i>;
63}