Skip to main content

oak_ini/parser/
element_type.rs

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