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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use super::JSArg;
use serde_json::value::Value as JsonValue;

const DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.f";
const DATE_FORMAT: &str = "%Y-%m-%d";
const TIME_FORMAT: &str = "%H:%M:%S%.f";

#[rustfmt::skip]
pub fn value_to_js_arg(value: &quaint::Value) -> serde_json::Result<JSArg> {
    let res = match &value.typed {
        quaint::ValueType::Numeric(Some(bd)) => JSArg::Value(JsonValue::String(bd.to_string())),
        quaint::ValueType::Json(Some(s)) => JSArg::Value(JsonValue::String(serde_json::to_string(s)?)),
        quaint::ValueType::Bytes(Some(bytes)) => JSArg::Buffer(bytes.to_vec()),
        quaint::ValueType::Date(Some(d)) => JSArg::Value(JsonValue::String(d.format(DATE_FORMAT).to_string())),
        quaint::ValueType::DateTime(Some(dt)) => JSArg::Value(JsonValue::String(dt.format(DATETIME_FORMAT).to_string())),
        quaint::ValueType::Time(Some(t)) => JSArg::Value(JsonValue::String(t.format(TIME_FORMAT).to_string())),
        quaint::ValueType::Array(Some(ref items)) => JSArg::Array(
            items
                .iter()
                .map(value_to_js_arg)
                .collect::<serde_json::Result<Vec<JSArg>>>()?,
        ),
        quaint_value => JSArg::from(JsonValue::from(quaint_value.clone())),
    };
    Ok(res)
}

#[cfg(test)]
mod test {
    use super::*;
    use quaint::bigdecimal::BigDecimal;
    use quaint::chrono::*;
    use quaint::ValueType;
    use std::str::FromStr;

    #[test]
    #[rustfmt::skip]
    fn test_value_to_js_arg() {
            let test_cases = vec![
            (
                ValueType::Numeric(Some(1.into())), 
                JSArg::Value(JsonValue::String("1".to_string()))
            ),
            (
                ValueType::Numeric(Some(BigDecimal::from_str("-1.1").unwrap())),
                JSArg::Value(JsonValue::String("-1.1".to_string()))
            ),
            (
                ValueType::Numeric(None),
                JSArg::Value(JsonValue::Null)
            ),
            (
                ValueType::Json(Some(serde_json::json!({"a": 1}))),
                JSArg::Value(JsonValue::String("{\"a\":1}".to_string()))
            ),
            (
                ValueType::Json(None),
                JSArg::Value(JsonValue::Null)
            ),
            (
                ValueType::Date(Some(NaiveDate::from_ymd_opt(2020, 2, 29).unwrap())),
                JSArg::Value(JsonValue::String("2020-02-29".to_string()))
            ),
            (
                ValueType::Date(None),
                JSArg::Value(JsonValue::Null)
            ),
            (
                ValueType::DateTime(Some(Utc.with_ymd_and_hms(2020, 1, 1, 23, 13, 1).unwrap().with_nanosecond(100).unwrap())),
                JSArg::Value(JsonValue::String("2020-01-01 23:13:01.000000100".to_string()))
            ),
            (
                ValueType::DateTime(None),
                JSArg::Value(JsonValue::Null)
            ),
            (
                ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap().with_nanosecond(1200).unwrap())),
                JSArg::Value(JsonValue::String("23:13:01.000001200".to_string()))
            ),
            (
                ValueType::Time(None),
                JSArg::Value(JsonValue::Null)
            ),
            (
                ValueType::Array(Some(vec!(
                    ValueType::Numeric(Some(1.into())).into_value(),
                    ValueType::Numeric(None).into_value(),
                    ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap())).into_value(),
                ))),
                JSArg::Array(vec!(
                    JSArg::Value(JsonValue::String("1".to_string())),
                    JSArg::Value(JsonValue::Null),
                    JSArg::Value(JsonValue::String("23:13:01".to_string()))
                ))
            ),
            (
                ValueType::Bytes(Some("hello".as_bytes().into())),
                JSArg::Buffer("hello".as_bytes().to_vec())
            ),
        
        ];

        let mut errors: Vec<String> = vec![];
        for (val, expected) in test_cases {
            let actual = value_to_js_arg(&val.clone().into_value()).unwrap();
            if actual != expected {
                errors.push(format!("transforming: {:?}, expected: {:?}, actual: {:?}", &val, expected, actual));
            }
        }
        assert_eq!(errors.len(), 0, "{}", errors.join("\n"));
    }
}