Skip to main content

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

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