Skip to main content

oak_tailwind/parser/
element_type.rs

1//! Element types for the Tailwind AST.
2use oak_core::{ElementType, UniversalElementRole};
3
4/// Element types for the Tailwind AST.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum TailwindElementType {
8    /// Root node.
9    Root,
10    /// A Tailwind class (e.g., `hover:bg-red-500`).
11    Class,
12    /// A modifier part (e.g., `hover:`).
13    Modifier,
14    /// A utility part (e.g., `bg-red-500`).
15    Utility,
16    /// An arbitrary value part (e.g., `[100px]`).
17    ArbitraryValue,
18    /// A directive (e.g., `@tailwind base`).
19    Directive,
20    /// A CSS declaration.
21    Declaration,
22    /// A comment.
23    Comment,
24    /// Error node.
25    ErrorNode,
26}
27
28impl core::fmt::Display for TailwindElementType {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        match self {
31            Self::Root => f.write_str("Root"),
32            Self::Class => f.write_str("Class"),
33            Self::Modifier => f.write_str("Modifier"),
34            Self::Utility => f.write_str("Utility"),
35            Self::ArbitraryValue => f.write_str("ArbitraryValue"),
36            Self::Directive => f.write_str("Directive"),
37            Self::Declaration => f.write_str("Declaration"),
38            Self::Comment => f.write_str("Comment"),
39            Self::ErrorNode => f.write_str("ErrorNode"),
40        }
41    }
42}
43
44impl ElementType for TailwindElementType {
45    type Role = UniversalElementRole;
46
47    fn role(&self) -> Self::Role {
48        match self {
49            Self::Root => UniversalElementRole::Root,
50            Self::Class => UniversalElementRole::Attribute,
51            Self::Modifier => UniversalElementRole::AttributeKey,
52            Self::Utility => UniversalElementRole::Name,
53            Self::ArbitraryValue => UniversalElementRole::Value,
54            Self::Directive => UniversalElementRole::Metadata,
55            Self::Declaration => UniversalElementRole::Metadata,
56            Self::Comment => UniversalElementRole::Documentation,
57            Self::ErrorNode => UniversalElementRole::Error,
58        }
59    }
60}
61
62impl From<crate::lexer::token_type::TailwindTokenType> for TailwindElementType {
63    fn from(token: crate::lexer::token_type::TailwindTokenType) -> Self {
64        match token {
65            crate::lexer::token_type::TailwindTokenType::Root => Self::Root,
66            crate::lexer::token_type::TailwindTokenType::Directive => Self::Directive,
67            crate::lexer::token_type::TailwindTokenType::Modifier => Self::Modifier,
68            crate::lexer::token_type::TailwindTokenType::Utility => Self::Utility,
69            crate::lexer::token_type::TailwindTokenType::ArbitraryValue => Self::ArbitraryValue,
70            crate::lexer::token_type::TailwindTokenType::Comment => Self::Comment,
71            _ => Self::ErrorNode,
72        }
73    }
74}