Skip to main content

oak_handlebars/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7pub enum HandlebarsElementType {
8    // --- 词法标记 (Tokens) ---
9    // 空白和换行
10    Whitespace,
11    Newline,
12
13    // 注释
14    Comment,
15
16    // Handlebars 特殊标记
17    Open,              // {{
18    Close,             // }}
19    OpenUnescaped,     // {{{
20    CloseUnescaped,    // }}}
21    OpenRawBlock,      // {{{{
22    CloseRawBlock,     // }}}}
23    OpenEndRawBlock,   // {{{{/
24    OpenBlock,         // {{#
25    OpenInverseBlock,  // {{^
26    CloseBlock,        // {{/
27    OpenPartial,       // {{>
28    OpenComment,       // {{!
29    OpenCommentBlock,  // {{!--
30    CloseCommentBlock, // --}}
31
32    // 关键字
33    Else, // else
34
35    // 标识符和字面量
36    Identifier,
37    StringLiteral,
38    NumberLiteral,
39    BooleanLiteral,
40    Dot,          // .
41    Slash,        // /
42    Hash,         // #
43    At,           // @
44    Pipe,         // |
45    Equal,        // =
46    LeftParen,    // (
47    RightParen,   // )
48    LeftBracket,  // [
49    RightBracket, // ]
50    Caret,        // ^
51
52    // 内容
53    Content, // HTML/text content outside of handlebars expressions
54
55    // --- 节点种类 (Elements) ---
56    Root,
57    Mustache,
58    Block,
59    InverseBlock,
60    Partial,
61    CommentNode, // Avoid conflict with Comment token
62    ContentNode, // Avoid conflict with Content token
63    Expression,
64    SubExpression,
65    Path,
66    Parameter,
67    ElseBlock,
68
69    // 特殊
70    Error,
71    Eof,
72}
73
74impl ElementType for HandlebarsElementType {
75    type Role = UniversalElementRole;
76
77    fn role(&self) -> Self::Role {
78        match self {
79            Self::Error => UniversalElementRole::Error,
80            _ => UniversalElementRole::None,
81        }
82    }
83}
84
85impl From<crate::lexer::token_type::HandlebarsTokenType> for HandlebarsElementType {
86    fn from(token: crate::lexer::token_type::HandlebarsTokenType) -> Self {
87        unsafe { std::mem::transmute(token) }
88    }
89}