zen_expression/variable/
conv.rs1use 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) => {
60 Value::Number(Number::from_string_unchecked(n.normalize().to_string()))
61 }
62 Variable::String(s) => Value::String(s.to_string()),
63 Variable::Array(arr) => {
64 let vec = Rc::try_unwrap(arr)
65 .map(|a| a.into_inner())
66 .unwrap_or_else(|s| {
67 let borrowed = s.borrow();
68 borrowed.clone()
69 });
70
71 Value::Array(vec.into_iter().map(Value::from).collect())
72 }
73 Variable::Object(obj) => {
74 let hmap = Rc::try_unwrap(obj)
75 .map(|a| a.into_inner())
76 .unwrap_or_else(|s| {
77 let borrowed = s.borrow();
78 borrowed.clone()
79 });
80
81 Value::Object(
82 hmap.into_iter()
83 .map(|(k, v)| (k.to_string(), Value::from(v)))
84 .collect(),
85 )
86 }
87 Variable::Dynamic(d) => d.to_value(),
88 }
89 }
90}
91
92impl TryFrom<&Variable> for NaiveDateTime {
93 type Error = VMError;
94
95 fn try_from(value: &Variable) -> Result<Self, Self::Error> {
96 match value {
97 Variable::String(a) => date_time(a),
98 #[allow(deprecated)]
99 Variable::Number(a) => NaiveDateTime::from_timestamp_opt(
100 a.to_i64().ok_or_else(|| VMError::OpcodeErr {
101 opcode: "DateManipulation".into(),
102 message: "Failed to extract date".into(),
103 })?,
104 0,
105 )
106 .ok_or_else(|| VMError::ParseDateTimeErr {
107 timestamp: a.to_string(),
108 }),
109 _ => Err(VMError::OpcodeErr {
110 opcode: "DateManipulation".into(),
111 message: "Unsupported type".into(),
112 }),
113 }
114 }
115}