Skip to main content

icydb_core/db/query/fluent/load/
result.rs

1use crate::{
2    db::{EntityResponse, PagedGroupedExecutionWithTrace},
3    traits::EntityKind,
4};
5
6use crate::db::query::intent::QueryError;
7
8///
9/// LoadQueryResult
10///
11/// Unified fluent load execution payload for scalar and grouped query shapes.
12/// Scalar queries materialize typed entity rows.
13/// Grouped queries materialize grouped rows plus continuation metadata.
14///
15#[derive(Debug)]
16pub enum LoadQueryResult<E: EntityKind> {
17    Rows(EntityResponse<E>),
18    Grouped(PagedGroupedExecutionWithTrace),
19}
20
21impl<E: EntityKind> LoadQueryResult<E> {
22    /// Return the number of emitted rows or groups.
23    #[must_use]
24    pub fn count(&self) -> u32 {
25        match self {
26            Self::Rows(rows) => rows.count(),
27            Self::Grouped(grouped) => u32::try_from(grouped.rows().len()).unwrap_or(u32::MAX),
28        }
29    }
30
31    /// Return whether no rows or groups were emitted.
32    #[must_use]
33    pub fn is_empty(&self) -> bool {
34        self.count() == 0
35    }
36
37    /// Consume this result and require scalar entity rows.
38    pub fn into_rows(self) -> Result<EntityResponse<E>, QueryError> {
39        match self {
40            Self::Rows(rows) => Ok(rows),
41            Self::Grouped(_) => Err(QueryError::unsupported_query(
42                "grouped queries return grouped rows; call execute() and inspect the grouped result",
43            )),
44        }
45    }
46
47    /// Consume this result and require grouped rows.
48    pub fn into_grouped(self) -> Result<PagedGroupedExecutionWithTrace, QueryError> {
49        match self {
50            Self::Grouped(grouped) => Ok(grouped),
51            Self::Rows(_) => Err(QueryError::unsupported_query(
52                "scalar queries return entity rows; grouped results are not available",
53            )),
54        }
55    }
56}