use super::span::Span;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Node {
Text(TextNode),
Variable(VariableNode),
If(IfNode),
For(ForNode),
Comment(CommentNode),
}
impl Node {
pub fn span(&self) -> &Span {
match self {
Node::Text(n) => &n.span,
Node::Variable(n) => &n.span,
Node::If(n) => &n.span,
Node::For(n) => &n.span,
Node::Comment(n) => &n.span,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextNode {
pub text: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct VariableNode {
pub expression: Expression,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct IfNode {
pub condition: Expression,
pub then_branch: Vec<Node>,
pub elif_branches: Vec<ElifBranch>,
pub else_branch: Option<Vec<Node>>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ElifBranch {
pub condition: Expression,
pub body: Vec<Node>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForNode {
pub item_name: String,
pub iterable: Expression,
pub body: Vec<Node>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CommentNode {
pub text: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Expression {
Path(Path),
Literal(Literal),
Not(Box<Expression>),
Filter(FilterExpression),
Comparison(ComparisonExpression),
Logical(LogicalExpression),
Arithmetic(ArithmeticExpression),
Negate(Box<Expression>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
String(String),
Number(String),
Bool(bool),
Null,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ComparisonExpression {
pub left: Box<Expression>,
pub operator: ComparisonOperator,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComparisonOperator {
Equal,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
}
impl ComparisonOperator {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
ComparisonOperator::Equal => "==",
ComparisonOperator::NotEqual => "!=",
ComparisonOperator::Less => "<",
ComparisonOperator::LessEqual => "<=",
ComparisonOperator::Greater => ">",
ComparisonOperator::GreaterEqual => ">=",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LogicalExpression {
pub left: Box<Expression>,
pub operator: LogicalOperator,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LogicalOperator {
And,
Or,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ArithmeticExpression {
pub left: Box<Expression>,
pub operator: ArithmeticOperator,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArithmeticOperator {
Add,
Subtract,
Multiply,
Divide,
Remainder,
}
impl ArithmeticOperator {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
ArithmeticOperator::Add => "+",
ArithmeticOperator::Subtract => "-",
ArithmeticOperator::Multiply => "*",
ArithmeticOperator::Divide => "/",
ArithmeticOperator::Remainder => "%",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FilterExpression {
pub value: Box<Expression>,
pub filters: Vec<FilterCall>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterCall {
pub name: String,
pub argument: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Path {
pub segments: Vec<PathSegment>,
}
impl Path {
pub fn new(segments: Vec<PathSegment>) -> Self {
Self { segments }
}
pub fn current() -> Self {
Self {
segments: vec![PathSegment::Current],
}
}
#[cfg(test)]
pub fn field(name: impl Into<String>) -> Self {
Self {
segments: vec![PathSegment::Field(name.into())],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathSegment {
Field(String),
Index(usize),
Current,
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use super::*;
#[test]
fn test_path_current() {
let path = Path::current();
assert_eq!(path.segments, vec![PathSegment::Current]);
}
#[test]
fn test_path_field() {
let path = Path::field("name");
assert_eq!(path.segments, vec![PathSegment::Field("name".to_string())]);
}
#[test]
fn test_path_new() {
let path = Path::new(vec![
PathSegment::Field("user".to_string()),
PathSegment::Field("name".to_string()),
]);
assert_eq!(path.segments.len(), 2);
}
#[test]
fn test_expression_not() {
let expr = Expression::Not(Box::new(Expression::Path(Path::field("active"))));
match expr {
Expression::Not(inner) => match *inner {
Expression::Path(_) => {}
_ => panic!("Expected Path inside Not"),
},
_ => panic!("Expected Not expression"),
}
}
#[test]
fn test_expression_filter() {
let expr = Expression::Filter(FilterExpression {
value: Box::new(Expression::Path(Path::field("name"))),
filters: vec![
FilterCall {
name: "trim".to_owned(),
argument: None,
},
FilterCall {
name: "default".to_owned(),
argument: Some("friend".to_owned()),
},
],
});
match expr {
Expression::Filter(filter) => {
assert_eq!(filter.filters.len(), 2);
assert_eq!(filter.filters[1].name, "default");
assert_eq!(filter.filters[1].argument.as_deref(), Some("friend"));
}
_ => panic!("Expected Filter expression"),
}
}
}