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
56
57
//! `CellValue` represents libSQL values and types.
//! Each database row consists of one or more cell values.

/// Value of a single database cell
#[derive(Clone, Debug)]
pub enum CellValue {
    Text(String),
    Float(f64),
    Number(i64),
    Bool(bool),
    Null,
}

impl std::fmt::Display for CellValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CellValue::Text(s) => write!(f, "\"{s}\""),
            CellValue::Float(d) => write!(f, "{d}"),
            CellValue::Number(n) => write!(f, "{n}"),
            CellValue::Bool(b) => write!(f, "{}", if *b { "1" } else { "0" }),
            CellValue::Null => write!(f, "null"),
        }
    }
}

impl From<()> for CellValue {
    fn from(_: ()) -> CellValue {
        CellValue::Null
    }
}

macro_rules! impl_from_cell_value {
    ($typename: ty, $variant: ident) => {
        impl From<$typename> for CellValue {
            fn from(t: $typename) -> CellValue {
                CellValue::$variant(t.into())
            }
        }
    };
}

impl_from_cell_value!(String, Text);
impl_from_cell_value!(&str, Text);

impl_from_cell_value!(i8, Number);
impl_from_cell_value!(i16, Number);
impl_from_cell_value!(i32, Number);
impl_from_cell_value!(i64, Number);

impl_from_cell_value!(u8, Number);
impl_from_cell_value!(u16, Number);
impl_from_cell_value!(u32, Number);

impl_from_cell_value!(f32, Float);
impl_from_cell_value!(f64, Float);

impl_from_cell_value!(bool, Bool);