Skip to main content

icydb_core/db/query/
expr.rs

1use 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///
11/// FilterExpr
12/// Schema-agnostic filter expression for dynamic query input.
13/// Lowered into a validated predicate at the intent boundary.
14///
15
16#[derive(Clone, Debug)]
17pub struct FilterExpr(pub Predicate);
18
19impl FilterExpr {
20    /// Lower the filter expression into a validated predicate for `E`.
21    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///
30/// SortExpr
31/// Schema-agnostic sort expression for dynamic query input.
32/// Lowered into a validated order spec at the intent boundary.
33///
34
35#[derive(Clone, Debug)]
36pub struct SortExpr {
37    pub fields: Vec<(String, OrderDirection)>,
38}
39
40impl SortExpr {
41    /// Lower the sort expression into a validated order spec for `E`.
42    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///
55/// SortLowerError
56/// Errors returned when lowering sort expressions into order specs.
57///
58
59#[derive(Debug, ThisError)]
60pub enum SortLowerError {
61    #[error("{0}")]
62    Validate(#[from] ValidateError),
63
64    #[error("{0}")]
65    Plan(#[from] PlanError),
66}