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