1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use {
    crate::{
        data::{Row, Value},
        executor::RowContext,
    },
    serde::{Deserialize, Serialize},
    std::collections::HashMap,
};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum DataRow {
    Vec(Vec<Value>),
    Map(HashMap<String, Value>),
}

impl From<Row> for DataRow {
    fn from(row: Row) -> Self {
        match row {
            Row::Vec { values, .. } => Self::Vec(values),
            Row::Map(values) => Self::Map(values),
        }
    }
}

impl From<Vec<Value>> for DataRow {
    fn from(values: Vec<Value>) -> Self {
        Self::Vec(values)
    }
}

impl DataRow {
    pub fn len(&self) -> usize {
        match self {
            Self::Vec(values) => values.len(),
            Self::Map(values) => values.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            Self::Vec(values) => values.is_empty(),
            Self::Map(values) => values.is_empty(),
        }
    }

    pub fn as_context<'a>(&'a self, columns: Option<&'a [String]>) -> RowContext<'a> {
        match self {
            Self::Vec(values) => RowContext::RefVecData {
                columns: columns.unwrap_or(&[]),
                values,
            },
            Self::Map(values) => RowContext::RefMapData(values),
        }
    }
}