use rust_decimal::Decimal;
#[derive(Debug, Clone, PartialEq)]
pub enum PathSegment {
Dot(String),
Index(usize),
Wildcard,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PathDotJoin {
AlwaysPrefix,
PrefixWhenNonempty,
}
impl PathSegment {
pub(crate) fn append_to_path(&self, path: &mut String, dot_join: PathDotJoin) {
match self {
PathSegment::Dot(name) => {
match dot_join {
PathDotJoin::AlwaysPrefix => path.push('.'),
PathDotJoin::PrefixWhenNonempty if !path.is_empty() => path.push('.'),
PathDotJoin::PrefixWhenNonempty => {}
}
path.push_str(name);
}
PathSegment::Index(i) => {
use std::fmt::Write;
let _ = write!(path, "[{i}]");
}
PathSegment::Wildcard => path.push_str("[*]"),
}
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Null,
Boolean(bool),
Number(Decimal),
String(String),
DateLiteral(String),
DateTimeLiteral(String),
Array(Vec<Expr>),
Object(Vec<(String, Expr)>),
FieldRef {
name: Option<String>,
path: Vec<PathSegment>,
},
VarRef {
name: String,
path: Vec<PathSegment>,
},
ContextRef {
name: String,
called: bool,
arg: Option<String>,
tail: Vec<String>,
},
UnaryOp {
op: UnaryOp,
operand: Box<Expr>,
bang: bool,
},
BinaryOp {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Ternary {
condition: Box<Expr>,
then_branch: Box<Expr>,
else_branch: Box<Expr>,
},
IfThenElse {
condition: Box<Expr>,
then_branch: Box<Expr>,
else_branch: Box<Expr>,
},
Membership {
value: Box<Expr>,
container: Box<Expr>,
negated: bool,
},
NullCoalesce {
left: Box<Expr>,
right: Box<Expr>,
},
LetBinding {
name: String,
value: Box<Expr>,
body: Box<Expr>,
},
FunctionCall {
name: String,
args: Vec<Expr>,
},
PostfixAccess {
expr: Box<Expr>,
path: Vec<PathSegment>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Not,
Neg,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Mod,
Concat,
Eq,
NotEq,
Lt,
Gt,
LtEq,
GtEq,
And,
Or,
}