Skip to main content

partiql/value/
toml_value.rs

1use chrono::prelude::*;
2use chrono::serde::ts_seconds;
3use indexmap::IndexMap as Map;
4use serde_derive::{Deserialize, Serialize};
5
6use crate::value::PqlValue;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(untagged)]
10pub enum TomlValue {
11    #[serde(skip_serializing)]
12    Null,
13    Str(String),
14    Boolean(bool),
15    Float(f64),
16    Int(i64),
17    #[serde(with = "ts_seconds")]
18    DateTime(DateTime<Utc>),
19    Array(Vec<Self>),
20    Object(Map<String, Self>),
21}
22
23impl From<PqlValue> for TomlValue {
24    fn from(pqlv: PqlValue) -> Self {
25        match pqlv {
26            PqlValue::Null => Self::Null,
27            PqlValue::Str(string) => Self::Str(string),
28            PqlValue::Boolean(boolean) => Self::Boolean(boolean),
29            PqlValue::Float(float) => Self::Float(float.into_inner()),
30            PqlValue::Int(int) => Self::Int(int),
31            PqlValue::DateTime(datetime) => Self::DateTime(datetime),
32            PqlValue::Array(array) => Self::Array(
33                array
34                    .into_iter()
35                    .filter_map(|v| match v {
36                        PqlValue::Null => None,
37                        _ => Some(Self::from(v)),
38                    })
39                    .collect::<Vec<_>>(),
40            ),
41            PqlValue::Object(map) => Self::Object({
42                let mut paris = vec![];
43                let mut paris_for_map = vec![];
44                for (k, v) in map.into_iter() {
45                    match v {
46                        PqlValue::Null => {}
47                        PqlValue::Object(_) => {
48                            paris_for_map.push((k, Self::from(v)));
49                        }
50                        _ => {
51                            paris.push((k, Self::from(v)));
52                        }
53                    }
54                }
55                paris.append(&mut paris_for_map);
56                paris.into_iter().collect::<Map<_, _>>()
57            }),
58        }
59    }
60}