Skip to main content

oak_css/parser/
element_type.rs

1use crate::lexer::CssTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// CSS element type
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum CssElementType {
10    /// Source file (root)
11    SourceFile,
12    /// Rule set
13    RuleSet,
14    /// Selector list
15    SelectorList,
16    /// Selector
17    Selector,
18    /// Declaration block
19    DeclarationBlock,
20    /// Declaration
21    Declaration,
22    /// Property
23    Property,
24    /// Value
25    Value,
26    /// At-rule
27    AtRule,
28    /// Media query
29    MediaQuery,
30    /// Function
31    Function,
32    /// Url
33    Url,
34    /// Calc expression
35    CalcExpression,
36}
37
38impl ElementType for CssElementType {
39    type Role = UniversalElementRole;
40
41    fn role(&self) -> Self::Role {
42        match self {
43            Self::SourceFile => UniversalElementRole::Root,
44            Self::RuleSet => UniversalElementRole::Container,
45            Self::SelectorList => UniversalElementRole::Detail,
46            Self::DeclarationBlock => UniversalElementRole::Container,
47            Self::Declaration => UniversalElementRole::Statement,
48            _ => UniversalElementRole::None,
49        }
50    }
51}
52
53impl From<CssTokenType> for CssElementType {
54    fn from(token: CssTokenType) -> Self {
55        match token {
56            _ => Self::SourceFile, // Default
57        }
58    }
59}