gluesql_core/store/
data_row.rs

1use {
2    crate::{
3        data::{Row, Value},
4        executor::RowContext,
5    },
6    serde::{Deserialize, Serialize},
7    std::collections::BTreeMap,
8};
9
10#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
11pub enum DataRow {
12    Vec(Vec<Value>),
13    Map(BTreeMap<String, Value>),
14}
15
16impl From<Row> for DataRow {
17    fn from(row: Row) -> Self {
18        match row {
19            Row::Vec { values, .. } => Self::Vec(values),
20            Row::Map(values) => Self::Map(values),
21        }
22    }
23}
24
25impl From<Vec<Value>> for DataRow {
26    fn from(values: Vec<Value>) -> Self {
27        Self::Vec(values)
28    }
29}
30
31impl DataRow {
32    pub fn len(&self) -> usize {
33        match self {
34            Self::Vec(values) => values.len(),
35            Self::Map(values) => values.len(),
36        }
37    }
38
39    pub fn is_empty(&self) -> bool {
40        match self {
41            Self::Vec(values) => values.is_empty(),
42            Self::Map(values) => values.is_empty(),
43        }
44    }
45
46    pub fn as_context<'a>(&'a self, columns: Option<&'a [String]>) -> RowContext<'a> {
47        match self {
48            Self::Vec(values) => RowContext::RefVecData {
49                columns: columns.unwrap_or(&[]),
50                values,
51            },
52            Self::Map(values) => RowContext::RefMapData(values),
53        }
54    }
55}