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
use chrono::prelude::*;
use chrono::serde::ts_seconds;
use indexmap::IndexMap as Map;
use serde_derive::{Deserialize, Serialize};

use crate::value::PqlValue;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TomlValue {
    #[serde(skip_serializing)]
    Null,
    Str(String),
    Boolean(bool),
    Float(f64),
    Int(i64),
    #[serde(with = "ts_seconds")]
    DateTime(DateTime<Utc>),
    Array(Vec<Self>),
    Object(Map<String, Self>),
}

impl From<PqlValue> for TomlValue {
    fn from(pqlv: PqlValue) -> Self {
        match pqlv {
            PqlValue::Missing => unreachable!(),
            PqlValue::Null => Self::Null,
            PqlValue::Str(string) => Self::Str(string),
            PqlValue::Boolean(boolean) => Self::Boolean(boolean),
            PqlValue::Float(float) => Self::Float(float.into_inner()),
            PqlValue::Int(int) => Self::Int(int),
            PqlValue::DateTime(datetime) => Self::DateTime(datetime),
            PqlValue::Array(array) => Self::Array(
                array
                    .into_iter()
                    .filter_map(|v| match v {
                        PqlValue::Null => None,
                        PqlValue::Missing => None,
                        _ => Some(Self::from(v)),
                    })
                    .collect::<Vec<_>>(),
            ),
            PqlValue::Object(map) => Self::Object({
                let mut paris = vec![];
                let mut paris_for_map = vec![];
                for (k, v) in map.into_iter() {
                    match v {
                        PqlValue::Missing => {}
                        PqlValue::Null => {}
                        PqlValue::Object(_) => {
                            paris_for_map.push((k, Self::from(v)));
                        }
                        _ => {
                            paris.push((k, Self::from(v)));
                        }
                    }
                }
                paris.append(&mut paris_for_map);
                paris.into_iter().collect::<Map<_, _>>()
            }),
        }
    }
}