Skip to main content

oak_nginx/parser/
element_type.rs

1use oak_core::{ElementType, Parser, 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 NginxElementType {
8    Root,
9    Directive,
10    Block,
11    Parameter,
12    Value,
13    Comment,
14    Error,
15}
16
17impl NginxElementType {
18    pub fn is_element(&self) -> bool {
19        matches!(self, Self::Root | Self::Directive | Self::Block | Self::Parameter | Self::Value | Self::Comment)
20    }
21
22    pub fn is_token(&self) -> bool {
23        !self.is_element()
24    }
25}
26
27impl ElementType for NginxElementType {
28    type Role = UniversalElementRole;
29
30    fn role(&self) -> Self::Role {
31        match self {
32            Self::Root => UniversalElementRole::Root,
33            Self::Directive => UniversalElementRole::Statement,
34            Self::Block => UniversalElementRole::Container,
35            Self::Parameter => UniversalElementRole::AttributeKey,
36            Self::Value => UniversalElementRole::Value,
37            Self::Comment => UniversalElementRole::Documentation,
38            Self::Error => UniversalElementRole::Error,
39        }
40    }
41}
42
43impl From<crate::lexer::token_type::NginxTokenType> for NginxElementType {
44    fn from(token: crate::lexer::token_type::NginxTokenType) -> Self {
45        use crate::lexer::token_type::NginxTokenType as T;
46        match token {
47            T::Root => Self::Root,
48            T::Directive => Self::Directive,
49            T::Block => Self::Block,
50            T::Parameter => Self::Parameter,
51            T::Value => Self::Value,
52            T::Comment => Self::Comment,
53            _ => Self::Error,
54        }
55    }
56}