rs_jsonnet/
ast.rs

1//! Abstract Syntax Tree for Jsonnet
2
3use serde::{Deserialize, Serialize};
4
5/// Expression in Jsonnet
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum Expr {
8    /// Identifier
9    Identifier(String),
10    /// String literal
11    String(String),
12    /// Number literal
13    Number(f64),
14    /// Boolean literal
15    Boolean(bool),
16    /// Null literal
17    Null,
18    /// Object literal
19    Object(Vec<(String, Expr)>),
20    /// Array literal
21    Array(Vec<Expr>),
22    /// Function call
23    Call(Box<Expr>, Vec<Expr>),
24    /// Array indexing
25    ArrayAccess(Box<Expr>, Box<Expr>),
26    /// Object field access
27    FieldAccess(Box<Expr>, String),
28    /// Local variable bindings
29    Local(Vec<(String, Expr)>, Box<Expr>),
30    /// Function definition
31    Function(Vec<String>, Box<Expr>),
32    /// String interpolation
33    StringInterpolation(Vec<StringPart>),
34    /// Array comprehension
35    ArrayComprehension {
36        expr: Box<Expr>,
37        var_name: String,
38        array_expr: Box<Expr>,
39        condition: Option<Box<Expr>>,
40    },
41    /// Conditional expression
42    Conditional(Box<Expr>, Box<Expr>, Box<Expr>),
43    /// Binary operation
44    BinaryOp(BinaryOp, Box<Expr>, Box<Expr>),
45    /// Unary operation
46    UnaryOp(UnaryOp, Box<Expr>),
47}
48
49/// Statement in Jsonnet
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub enum Stmt {
52    /// Expression statement
53    Expr(Expr),
54    /// Local variable declaration
55    Local(String, Expr),
56    /// Assignment
57    Assign(String, Expr),
58}
59
60/// Binary operators
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62pub enum BinaryOp {
63    /// Addition
64    Add,
65    /// Subtraction
66    Sub,
67    /// Multiplication
68    Mul,
69    /// Division
70    Div,
71    /// Modulo
72    Mod,
73    /// Equal
74    Eq,
75    /// Not equal
76    Ne,
77    /// Less than
78    Lt,
79    /// Less than or equal
80    Le,
81    /// Greater than
82    Gt,
83    /// Greater than or equal
84    Ge,
85    /// And
86    And,
87    /// Or
88    Or,
89}
90
91/// Unary operators
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93pub enum UnaryOp {
94    /// Negation
95    Neg,
96    /// Not
97    Not,
98    /// Plus
99    Plus,
100}
101
102/// Parts of a string interpolation
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub enum StringPart {
105    /// Literal text
106    Literal(String),
107    /// Variable interpolation
108    Interpolation(Expr),
109}