zen_expression/variable/
conv.rs

1use crate::variable::Variable;
2use crate::vm::helpers::date_time;
3use crate::vm::VMError;
4use chrono::NaiveDateTime;
5use rust_decimal::prelude::ToPrimitive;
6use rust_decimal::Decimal;
7use serde_json::{Number, Value};
8use std::rc::Rc;
9
10impl From<Value> for Variable {
11    fn from(value: Value) -> Self {
12        match value {
13            Value::Null => Variable::Null,
14            Value::Bool(b) => Variable::Bool(b),
15            Value::Number(n) => Variable::Number(
16                Decimal::from_str_exact(n.as_str())
17                    .or_else(|_| Decimal::from_scientific(n.as_str()))
18                    .expect("Allowed number"),
19            ),
20            Value::String(s) => Variable::String(Rc::from(s.as_str())),
21            Value::Array(arr) => {
22                Variable::from_array(arr.into_iter().map(Variable::from).collect())
23            }
24            Value::Object(obj) => Variable::from_object(
25                obj.into_iter()
26                    .map(|(k, v)| (Rc::from(k.as_str()), Variable::from(v)))
27                    .collect(),
28            ),
29        }
30    }
31}
32
33impl From<&Value> for Variable {
34    fn from(value: &Value) -> Self {
35        match value {
36            Value::Null => Variable::Null,
37            Value::Bool(b) => Variable::Bool(*b),
38            Value::Number(n) => Variable::Number(
39                Decimal::from_str_exact(n.as_str())
40                    .or_else(|_| Decimal::from_scientific(n.as_str()))
41                    .expect("Allowed number"),
42            ),
43            Value::String(s) => Variable::String(Rc::from(s.as_str())),
44            Value::Array(arr) => Variable::from_array(arr.iter().map(Variable::from).collect()),
45            Value::Object(obj) => Variable::from_object(
46                obj.iter()
47                    .map(|(k, v)| (Rc::from(k.as_str()), Variable::from(v)))
48                    .collect(),
49            ),
50        }
51    }
52}
53
54impl From<Variable> for Value {
55    fn from(value: Variable) -> Self {
56        match value {
57            Variable::Null => Value::Null,
58            Variable::Bool(b) => Value::Bool(b),
59            Variable::Number(n) => Value::Number(Number::from_string_unchecked(n.to_string())),
60            Variable::String(s) => Value::String(s.to_string()),
61            Variable::Array(arr) => {
62                let vec = Rc::try_unwrap(arr)
63                    .map(|a| a.into_inner())
64                    .unwrap_or_else(|s| {
65                        let borrowed = s.borrow();
66                        borrowed.clone()
67                    });
68
69                Value::Array(vec.into_iter().map(Value::from).collect())
70            }
71            Variable::Object(obj) => {
72                let hmap = Rc::try_unwrap(obj)
73                    .map(|a| a.into_inner())
74                    .unwrap_or_else(|s| {
75                        let borrowed = s.borrow();
76                        borrowed.clone()
77                    });
78
79                Value::Object(
80                    hmap.into_iter()
81                        .map(|(k, v)| (k.to_string(), Value::from(v)))
82                        .collect(),
83                )
84            }
85            Variable::Dynamic(d) => d.to_value(),
86        }
87    }
88}
89
90impl TryFrom<&Variable> for NaiveDateTime {
91    type Error = VMError;
92
93    fn try_from(value: &Variable) -> Result<Self, Self::Error> {
94        match value {
95            Variable::String(a) => date_time(a),
96            #[allow(deprecated)]
97            Variable::Number(a) => NaiveDateTime::from_timestamp_opt(
98                a.to_i64().ok_or_else(|| VMError::OpcodeErr {
99                    opcode: "DateManipulation".into(),
100                    message: "Failed to extract date".into(),
101                })?,
102                0,
103            )
104            .ok_or_else(|| VMError::ParseDateTimeErr {
105                timestamp: a.to_string(),
106            }),
107            _ => Err(VMError::OpcodeErr {
108                opcode: "DateManipulation".into(),
109                message: "Unsupported type".into(),
110            }),
111        }
112    }
113}