use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub struct Filter {
pub expression: Expression,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
And(Box<Expression>, Box<Expression>),
Or(Box<Expression>, Box<Expression>),
Not(Box<Expression>),
Restriction(Restriction),
Sequence(Sequence),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Restriction {
pub field: String,
pub comparator: Comparator,
pub value: Value,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Comparator {
Equal,
NotEqual,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual,
Has, }
#[derive(Debug, Clone, PartialEq)]
pub struct Sequence {
pub parts: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
String(String),
Number(f64),
Boolean(bool),
Null,
}
impl fmt::Display for Comparator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Comparator::Equal => write!(f, "="),
Comparator::NotEqual => write!(f, "!="),
Comparator::GreaterThan => write!(f, ">"),
Comparator::GreaterThanOrEqual => write!(f, ">="),
Comparator::LessThan => write!(f, "<"),
Comparator::LessThanOrEqual => write!(f, "<="),
Comparator::Has => write!(f, ":"),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::String(s) => write!(f, "\"{}\"", s),
Value::Number(n) => write!(f, "{}", n),
Value::Boolean(b) => write!(f, "{}", b),
Value::Null => write!(f, "null"),
}
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expression::And(left, right) => write!(f, "({} AND {})", left, right),
Expression::Or(left, right) => write!(f, "({} OR {})", left, right),
Expression::Not(expr) => write!(f, "NOT {}", expr),
Expression::Restriction(r) => write!(f, "{} {} {}", r.field, r.comparator, r.value),
Expression::Sequence(s) => write!(f, "{}", s.parts.join(".")),
}
}
}
impl fmt::Display for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.expression)
}
}