Skip to main content

oak_ini/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// INI element types used in the syntax tree.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum IniElementType {
7    /// Whitespace characters.
8    Whitespace,
9    /// Newline character.
10    Newline,
11    /// Comment.
12    Comment,
13    /// Error element.
14    Error,
15    /// End of file.
16    Eof,
17
18    /// `{` symbol.
19    LeftBrace,
20    /// `}` symbol.
21    RightBrace,
22    /// `[` symbol.
23    LeftBracket,
24    /// `]` symbol.
25    RightBracket,
26    /// `[[` symbol.
27    DoubleLeftBracket,
28    /// `]]` symbol.
29    DoubleRightBracket,
30    /// `,` symbol.
31    Comma,
32    /// `.` symbol.
33    Dot,
34    /// `=` operator.
35    Equal,
36
37    /// Identifier.
38    Identifier,
39    /// String literal.
40    String,
41    /// Integer literal.
42    Integer,
43    /// Floating-point literal.
44    Float,
45    /// Boolean literal.
46    Boolean,
47    /// Date and time literal.
48    DateTime,
49
50    /// Root of the INI document.
51    Root,
52    /// The entire INI document.
53    Document,
54    /// A section in the INI file.
55    Section,
56    /// A table definition.
57    Table,
58    /// An array of tables definition.
59    ArrayOfTables,
60    /// A key-value pair.
61    KeyValue,
62    /// The key part of a key-value pair.
63    Key,
64    /// The value part of a key-value pair.
65    Value,
66    /// An array value.
67    Array,
68    /// An inline table value.
69    InlineTable,
70}
71
72impl ElementType for IniElementType {
73    type Role = UniversalElementRole;
74
75    fn role(&self) -> Self::Role {
76        match self {
77            Self::Root | Self::Document => UniversalElementRole::Root,
78            Self::Section | Self::Table | Self::ArrayOfTables | Self::Array | Self::InlineTable => UniversalElementRole::Container,
79            Self::KeyValue => UniversalElementRole::Attribute,
80            Self::Key => UniversalElementRole::AttributeKey,
81            Self::Value => UniversalElementRole::Value,
82            _ => UniversalElementRole::None,
83        }
84    }
85}
86
87impl From<crate::lexer::token_type::IniTokenType> for IniElementType {
88    fn from(token: crate::lexer::token_type::IniTokenType) -> Self {
89        match token {
90            crate::lexer::token_type::IniTokenType::Whitespace => Self::Whitespace,
91            crate::lexer::token_type::IniTokenType::Newline => Self::Newline,
92            crate::lexer::token_type::IniTokenType::Comment => Self::Comment,
93            crate::lexer::token_type::IniTokenType::Error => Self::Error,
94            crate::lexer::token_type::IniTokenType::Eof => Self::Eof,
95            crate::lexer::token_type::IniTokenType::LeftBrace => Self::LeftBrace,
96            crate::lexer::token_type::IniTokenType::RightBrace => Self::RightBrace,
97            crate::lexer::token_type::IniTokenType::LeftBracket => Self::LeftBracket,
98            crate::lexer::token_type::IniTokenType::RightBracket => Self::RightBracket,
99            crate::lexer::token_type::IniTokenType::DoubleLeftBracket => Self::DoubleLeftBracket,
100            crate::lexer::token_type::IniTokenType::DoubleRightBracket => Self::DoubleRightBracket,
101            crate::lexer::token_type::IniTokenType::Comma => Self::Comma,
102            crate::lexer::token_type::IniTokenType::Dot => Self::Dot,
103            crate::lexer::token_type::IniTokenType::Equal => Self::Equal,
104            crate::lexer::token_type::IniTokenType::Identifier => Self::Identifier,
105            crate::lexer::token_type::IniTokenType::String => Self::String,
106            crate::lexer::token_type::IniTokenType::Integer => Self::Integer,
107            crate::lexer::token_type::IniTokenType::Float => Self::Float,
108            crate::lexer::token_type::IniTokenType::Boolean => Self::Boolean,
109            crate::lexer::token_type::IniTokenType::DateTime => Self::DateTime,
110        }
111    }
112}