Skip to main content

oak_nginx/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// Root node of the Nginx AST.
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[derive(Debug, Clone)]
7pub struct NginxRoot {
8    /// The source range covered by this root node.
9    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
10    pub range: Range<usize>,
11    /// The list of top-level items in the Nginx configuration.
12    pub items: Vec<NginxItem>,
13}
14
15/// Represents an item in the Nginx configuration.
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[derive(Debug, Clone)]
18pub enum NginxItem {
19    /// A simple directive (e.g., `user nginx;`).
20    Directive(Directive),
21    /// A block directive (e.g., `http { ... }`).
22    Block(Block),
23    /// A comment.
24    Comment(Comment),
25}
26
27/// A simple Nginx directive.
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29#[derive(Debug, Clone)]
30pub struct Directive {
31    /// The name of the directive.
32    pub name: String,
33    /// The parameters of the directive.
34    pub parameters: Vec<String>,
35    /// The source range of the directive.
36    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
37    pub range: Range<usize>,
38}
39
40/// An Nginx block directive.
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42#[derive(Debug, Clone)]
43pub struct Block {
44    /// The name of the block.
45    pub name: String,
46    /// The parameters of the block.
47    pub parameters: Vec<String>,
48    /// The items inside the block.
49    pub items: Vec<NginxItem>,
50    /// The source range of the block.
51    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
52    pub range: Range<usize>,
53}
54
55/// An Nginx comment.
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57#[derive(Debug, Clone)]
58pub struct Comment {
59    /// The text content.
60    pub text: String,
61    /// The source range of the text.
62    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
63    pub range: Range<usize>,
64}
65
66impl NginxRoot {
67    /// Creates a new `NginxRoot` with the specified range.
68    pub fn new(range: Range<usize>, items: Vec<NginxItem>) -> Self {
69        Self { range, items }
70    }
71}