Skip to main content

dactyl_db/
rows.rs

1//! Row projection returned by [`crate::read`] and [`crate::write`].
2
3use serde::Serialize;
4
5/// A collection of result rows.
6#[derive(Debug, Clone, Default, Serialize)]
7pub struct Rows(pub Vec<Row>);
8
9impl Rows {
10    /// Borrow the rows as a slice.
11    pub fn as_slice(&self) -> &[Row] {
12        &self.0
13    }
14
15    /// Iterate over rows.
16    pub fn iter(&self) -> std::slice::Iter<'_, Row> {
17        self.0.iter()
18    }
19
20    /// Number of rows.
21    pub fn len(&self) -> usize {
22        self.0.len()
23    }
24
25    /// Whether the result is empty.
26    pub fn is_empty(&self) -> bool {
27        self.0.is_empty()
28    }
29}
30
31impl IntoIterator for Rows {
32    type Item = Row;
33    type IntoIter = std::vec::IntoIter<Row>;
34
35    fn into_iter(self) -> Self::IntoIter {
36        self.0.into_iter()
37    }
38}
39
40/// One result row. Carries the column names (shared across the result) plus
41/// the per-cell JSON values.
42#[derive(Debug, Clone, Serialize)]
43pub struct Row {
44    /// Column names, in the order the adapter emitted them.
45    pub columns: Vec<String>,
46    /// Per-cell values, parallel to `columns`.
47    pub values: Vec<serde_json::Value>,
48}
49
50impl Row {
51    /// Look up a cell by column name.
52    pub fn get(&self, column: &str) -> Option<&serde_json::Value> {
53        let idx = self.columns.iter().position(|c| c == column)?;
54        self.values.get(idx)
55    }
56}