Skip to main content

oak_typst/parser/
element_type.rs

1use oak_core::{ElementType, Parser, UniversalElementRole};
2
3/// Represents an element type in a Typst document.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum TypstElementType {
7    /// Root node of the AST.
8    Root,
9    /// A heading element.
10    Heading,
11    /// A quote element.
12    Quote,
13    /// A math element.
14    Math,
15    /// Strong text.
16    Strong,
17    /// Emphasized text.
18    Emphasis,
19    /// A list item.
20    ListItem,
21    /// An enumeration item.
22    EnumItem,
23    /// Raw content (e.g., code blocks).
24    Raw,
25}
26
27impl ElementType for TypstElementType {
28    type Role = UniversalElementRole;
29
30    fn role(&self) -> Self::Role {
31        match self {
32            Self::Root => UniversalElementRole::Root,
33            Self::Heading => UniversalElementRole::Statement,
34            Self::Math => UniversalElementRole::Expression,
35            _ => UniversalElementRole::None,
36        }
37    }
38}
39
40impl From<crate::lexer::token_type::TypstTokenType> for TypstElementType {
41    fn from(token: crate::lexer::token_type::TypstTokenType) -> Self {
42        use crate::lexer::token_type::TypstTokenType::*;
43        match token {
44            Heading => Self::Heading,
45            Quote => Self::Quote,
46            Math | InlineMath | DisplayMath => Self::Math,
47            Strong => Self::Strong,
48            Emphasis => Self::Emphasis,
49            ListItem => Self::ListItem,
50            EnumItem => Self::EnumItem,
51            Raw | Code => Self::Raw,
52            _ => Self::Root,
53        }
54    }
55}