Skip to main content

oak_nginx/parser/
element_type.rs

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