use ciborium::Value as CborValue;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
List,
Single,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Op {
Eq,
Ne,
Lt,
Lte,
Gt,
Gte,
Like,
In,
IsNull,
IsNotNull,
}
impl Op {
pub fn name(self) -> &'static str {
match self {
Op::Eq => "eq",
Op::Ne => "ne",
Op::Lt => "lt",
Op::Lte => "lte",
Op::Gt => "gt",
Op::Gte => "gte",
Op::Like => "like",
Op::In => "in",
Op::IsNull => "null",
Op::IsNotNull => "notnull",
}
}
pub fn parse(s: &str) -> Option<Op> {
match s {
"eq" => Some(Op::Eq),
"ne" => Some(Op::Ne),
"lt" => Some(Op::Lt),
"lte" => Some(Op::Lte),
"gt" => Some(Op::Gt),
"gte" => Some(Op::Gte),
"like" => Some(Op::Like),
"in" => Some(Op::In),
"null" => Some(Op::IsNull),
"notnull" => Some(Op::IsNotNull),
_ => None,
}
}
pub fn is_nullary(self) -> bool {
matches!(self, Op::IsNull | Op::IsNotNull)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Asc,
Desc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateOp {
Sum,
Max,
Min,
Count,
}
impl AggregateOp {
pub fn parse(s: &str) -> Option<Self> {
match s {
"sum" => Some(AggregateOp::Sum),
"max" => Some(AggregateOp::Max),
"min" => Some(AggregateOp::Min),
"count" => Some(AggregateOp::Count),
_ => None,
}
}
pub fn name(self) -> &'static str {
match self {
AggregateOp::Sum => "sum",
AggregateOp::Max => "max",
AggregateOp::Min => "min",
AggregateOp::Count => "count",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selector {
pub sort: Option<(String, Direction)>,
pub slice: Option<Slice>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Slice {
Index(usize),
Range { start: usize, end: Option<usize> },
}
impl Selector {
pub fn is_empty(&self) -> bool {
self.sort.is_none() && self.slice.is_none()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
ModelName(String, Option<Selector>),
Locator(String),
OpCondition {
field: String,
op: Op,
value: Option<CborValue>,
selector: Option<Selector>,
},
Relation(String, Option<Selector>),
Bracket(Selector),
Columns(Vec<String>, Option<Selector>),
Search(String),
Aggregate {
op: AggregateOp,
field: Option<String>,
},
}