Skip to main content

oak_nginx/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for Nginx configuration.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum NginxElementType {
7    /// The root of the configuration.
8    Root,
9    /// A configuration directive.
10    Directive,
11    /// A configuration block.
12    Block,
13    /// A parameter within a directive.
14    Parameter,
15    /// A value within a directive or parameter.
16    Value,
17    /// A comment.
18    Comment,
19    /// An error element.
20    Error,
21}
22
23impl NginxElementType {
24    /// Returns true if the element type represents a structural element.
25    pub fn is_element(&self) -> bool {
26        matches!(self, Self::Root | Self::Directive | Self::Block | Self::Parameter | Self::Value | Self::Comment)
27    }
28
29    /// Returns true if the element type represents a lexical token.
30    pub fn is_token(&self) -> bool {
31        !self.is_element()
32    }
33}
34
35impl ElementType for NginxElementType {
36    type Role = UniversalElementRole;
37
38    fn role(&self) -> Self::Role {
39        match self {
40            Self::Root => UniversalElementRole::Root,
41            Self::Directive => UniversalElementRole::Statement,
42            Self::Block => UniversalElementRole::Container,
43            Self::Parameter => UniversalElementRole::AttributeKey,
44            Self::Value => UniversalElementRole::Value,
45            Self::Comment => UniversalElementRole::Documentation,
46            Self::Error => UniversalElementRole::Error,
47        }
48    }
49}
50
51impl From<crate::lexer::token_type::NginxTokenType> for NginxElementType {
52    fn from(token: crate::lexer::token_type::NginxTokenType) -> Self {
53        use crate::lexer::token_type::NginxTokenType as T;
54        match token {
55            T::Root => Self::Root,
56            T::Directive => Self::Directive,
57            T::Block => Self::Block,
58            T::Parameter => Self::Parameter,
59            T::Value => Self::Value,
60            T::Comment => Self::Comment,
61            _ => Self::Error,
62        }
63    }
64}