icydb_core/db/query/
expr.rs1use crate::{
2 db::query::{
3 plan::{OrderDirection, OrderSpec, PlanError, validate::validate_order},
4 predicate::{self, Predicate, SchemaInfo, ValidateError, normalize},
5 },
6 traits::EntityKind,
7};
8use thiserror::Error as ThisError;
9
10#[derive(Clone, Debug)]
17pub struct FilterExpr(pub Predicate);
18
19impl FilterExpr {
20 pub fn lower<E: EntityKind>(&self) -> Result<Predicate, ValidateError> {
22 let schema = SchemaInfo::from_entity_model(E::MODEL)?;
23 predicate::validate(&schema, &self.0)?;
24
25 Ok(normalize(&self.0))
26 }
27}
28
29#[derive(Clone, Debug)]
36pub struct SortExpr {
37 pub fields: Vec<(String, OrderDirection)>,
38}
39
40impl SortExpr {
41 pub fn lower<E: EntityKind>(&self) -> Result<OrderSpec, SortLowerError> {
43 let schema = SchemaInfo::from_entity_model(E::MODEL)?;
44 let spec = OrderSpec {
45 fields: self.fields.clone(),
46 };
47
48 validate_order(&schema, &spec)?;
49
50 Ok(spec)
51 }
52}
53
54#[derive(Debug, ThisError)]
60pub enum SortLowerError {
61 #[error("{0}")]
62 Validate(#[from] ValidateError),
63
64 #[error("{0}")]
65 Plan(#[from] PlanError),
66}