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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use anyhow::anyhow;
use std::fmt::Display;

use enum_as_inner::EnumAsInner;
use serde::{Deserialize, Serialize};

#[derive(Debug, EnumAsInner, PartialEq, Clone, Serialize, Deserialize)]
pub enum Literal {
    Null,
    Integer(i64),
    Float(f64),
    Boolean(bool),
    String(String),
    Date(String),
    Time(String),
    Timestamp(String),
    ValueAndUnit(ValueAndUnit),
}

// Compound units, such as "2 days 3 hours" can be represented as `2days + 3hours`
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ValueAndUnit {
    pub n: i64,       // Do any DBs use floats or decimals for this?
    pub unit: String, // Could be an enum IntervalType,
}

impl From<Literal> for anyhow::Error {
    fn from(item: Literal) -> Self {
        anyhow!("Failed to convert `{item}`")
    }
}

impl Display for Literal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Literal::Null => write!(f, "null")?,
            Literal::Integer(i) => write!(f, "{i}")?,
            Literal::Float(i) => write!(f, "{i}")?,

            Literal::String(s) => {
                match s.find('"') {
                    Some(_) => {
                        match s.find('\'') {
                            Some(_) => {
                                let mut min_quote = 3;
                                // find minimum number of double quotes when string contains
                                // both single and double quotes
                                loop {
                                    let stop = s
                                        .find(&"\"".repeat(min_quote))
                                        .map(|_| s.contains(&"\"".repeat(min_quote + 1)))
                                        .unwrap_or(true);
                                    if stop {
                                        break;
                                    } else {
                                        min_quote += 1;
                                    }
                                }
                                let quotes = "\"".repeat(min_quote);
                                write!(f, "{quotes}{s}{quotes}")?;
                            }

                            None => write!(f, "'{s}'")?,
                        };
                    }

                    None => write!(f, "\"{s}\"")?,
                };
            }

            Literal::Boolean(b) => {
                f.write_str(if *b { "true" } else { "false" })?;
            }

            Literal::Date(inner) | Literal::Time(inner) | Literal::Timestamp(inner) => {
                write!(f, "@{inner}")?;
            }

            Literal::ValueAndUnit(i) => {
                write!(f, "{}{}", i.n, i.unit)?;
            }
        }
        Ok(())
    }
}