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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
pub use crate::variable::map::BumpMap;
pub use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
use chrono::NaiveDateTime;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use serde_json::{Number, Value};
use strum_macros::Display;

mod conv;
mod de;
mod map;
mod ser;

use crate::vm::helpers::date_time;
use crate::vm::VMError;
#[allow(unused_imports)]
pub use conv::ToVariable;

#[derive(Debug, PartialEq, Eq, Display)]
pub enum Variable<'arena> {
    Null,
    Bool(bool),
    Number(Decimal),
    String(&'arena str),
    Array(BumpVec<'arena, Variable<'arena>>),
    Object(BumpMap<'arena, &'arena str, Variable<'arena>>),
}

impl<'arena> Variable<'arena> {
    pub fn empty_object(arena: &'arena Bump) -> Self {
        Variable::Object(BumpMap::new_in(arena))
    }

    pub fn empty_array(arena: &'arena Bump) -> Self {
        Variable::Array(BumpVec::new_in(arena))
    }

    pub fn as_str(&self) -> Option<&'arena str> {
        match self {
            Variable::String(s) => Some(*s),
            _ => None,
        }
    }

    pub fn as_array(&self) -> Option<&BumpVec<'arena, Variable<'arena>>> {
        match self {
            Variable::Array(arr) => Some(arr),
            _ => None,
        }
    }

    pub fn as_object(&self) -> Option<&BumpMap<'arena, &'arena str, Variable<'arena>>> {
        match self {
            Variable::Object(obj) => Some(obj),
            _ => None,
        }
    }

    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Variable::Bool(b) => Some(*b),
            _ => None,
        }
    }

    pub fn type_name(&self) -> &'static str {
        match self {
            Variable::Null => "null",
            Variable::Bool(_) => "bool",
            Variable::Number(_) => "number",
            Variable::String(_) => "string",
            Variable::Array(_) => "array",
            Variable::Object(_) => "object",
        }
    }

    pub fn to_value(&self) -> Value {
        match self {
            Variable::Null => Value::Null,
            Variable::Bool(b) => Value::Bool(*b),
            Variable::Number(n) => {
                Value::Number(Number::from_string_unchecked(n.normalize().to_string()))
            }
            Variable::String(str) => Value::String(str.to_string()),
            Variable::Array(arr) => Value::Array(arr.iter().map(|i| i.to_value()).collect()),
            Variable::Object(obj) => Value::Object(
                obj.iter()
                    .map(|(k, v)| (k.to_string(), v.to_value()))
                    .collect(),
            ),
        }
    }

    pub fn clone_in<'new>(&self, arena: &'new Bump) -> Variable<'new> {
        match self {
            Variable::Null => Variable::Null,
            Variable::Bool(b) => Variable::Bool(*b),
            Variable::Number(n) => Variable::Number(*n),
            Variable::String(s) => Variable::String(arena.alloc_str(s)),
            Variable::Array(arr) => Variable::Array(BumpVec::from_iter_in(
                arr.iter().map(|v| v.clone_in(arena)),
                arena,
            )),
            Variable::Object(obj) => Variable::Object(BumpMap::from_iter_in(
                obj.iter()
                    .map(|(k, v)| (&*arena.alloc_str(k), v.clone_in(arena))),
                arena,
            )),
        }
    }

    pub fn dot(&self, key: &str) -> Option<&Variable<'arena>> {
        key.split('.').try_fold(self, |var, part| match var {
            Variable::Object(obj) => obj.get(part),
            _ => None,
        })
    }

    pub fn dot_mut(&mut self, key: &str) -> Option<&mut Variable<'arena>> {
        key.split('.').try_fold(self, |var, part| match var {
            Variable::Object(obj) => obj.get_mut(part),
            _ => None,
        })
    }

    pub fn dot_insert(
        &mut self,
        arena: &'arena Bump,
        key: &str,
        variable: Variable<'arena>,
    ) -> Option<&mut Variable<'arena>> {
        let mut parts: BumpVec<&'arena str> =
            BumpVec::from_iter_in(key.split('.').map(|p| &*arena.alloc_str(p)), arena);
        let Some(last_part) = parts.pop() else {
            return None;
        };

        let head = parts.iter().try_fold(self, |var, part| match var {
            Variable::Object(obj) => {
                if obj.contains_key(part) {
                    obj.get_mut(part)
                } else {
                    obj.insert(part, Self::empty_object(arena));
                    obj.get_mut(part)
                }
            }
            _ => None,
        })?;

        let Variable::Object(head_obj) = head else {
            return None;
        };

        head_obj.insert(last_part, variable);
        head_obj.get_mut(last_part)
    }
}

impl TryFrom<&Variable<'_>> for NaiveDateTime {
    type Error = VMError;

    fn try_from(value: &Variable<'_>) -> Result<Self, Self::Error> {
        match value {
            Variable::String(a) => date_time(a),
            Variable::Number(a) => NaiveDateTime::from_timestamp_opt(
                a.to_i64().ok_or_else(|| VMError::OpcodeErr {
                    opcode: "DateManipulation".into(),
                    message: "Failed to extract date".into(),
                })?,
                0,
            )
            .ok_or_else(|| VMError::ParseDateTimeErr {
                timestamp: a.to_string(),
            }),
            _ => Err(VMError::OpcodeErr {
                opcode: "DateManipulation".into(),
                message: "Unsupported type".into(),
            }),
        }
    }
}