zen_expression/variable/types/
mod.rs

1mod conv;
2mod util;
3
4use crate::variable::RcCell;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7use std::fmt::{Display, Write};
8use std::hash::{Hash, Hasher};
9use std::rc::Rc;
10
11#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
12pub enum VariableType {
13    Any,
14    Null,
15    Bool,
16    String,
17    Number,
18    Date,
19    Interval,
20    Array(Rc<VariableType>),
21    Object(RcCell<HashMap<Rc<str>, VariableType>>),
22
23    Const(Rc<str>),
24    Enum(Option<Rc<str>>, Vec<Rc<str>>),
25}
26
27impl VariableType {
28    pub fn array(self) -> Self {
29        Self::Array(Rc::new(self))
30    }
31}
32
33impl Default for VariableType {
34    fn default() -> Self {
35        VariableType::Null
36    }
37}
38
39impl Display for VariableType {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            VariableType::Any => write!(f, "any"),
43            VariableType::Null => write!(f, "null"),
44            VariableType::Bool => write!(f, "bool"),
45            VariableType::String => write!(f, "string"),
46            VariableType::Number => write!(f, "number"),
47            VariableType::Date => write!(f, "date"),
48            VariableType::Interval => write!(f, "interval"),
49            VariableType::Const(c) => write!(f, "\"{c}\""),
50            VariableType::Enum(name, e) => {
51                if let Some(name) = name {
52                    return name.fmt(f);
53                }
54
55                let mut first = true;
56                for s in e.iter() {
57                    if !first {
58                        f.write_str(" | ")?;
59                    }
60
61                    f.write_char('"')?;
62                    f.write_str(s)?;
63                    f.write_char('"')?;
64                    first = false;
65                }
66
67                Ok(())
68            }
69            VariableType::Array(v) => write!(f, "{v}[]"),
70            VariableType::Object(_) => write!(f, "object"),
71        }
72    }
73}
74
75impl Hash for VariableType {
76    fn hash<H: Hasher>(&self, state: &mut H) {
77        match &self {
78            VariableType::Any => 0.hash(state),
79            VariableType::Null => 1.hash(state),
80            VariableType::Bool => 2.hash(state),
81            VariableType::String => 3.hash(state),
82            VariableType::Number => 4.hash(state),
83            VariableType::Date => 5.hash(state),
84            VariableType::Interval => 6.hash(state),
85            VariableType::Const(c) => {
86                7.hash(state);
87                c.hash(state)
88            }
89            VariableType::Enum(name, e) => {
90                8.hash(state);
91                name.hash(state);
92                e.hash(state)
93            }
94            VariableType::Array(arr) => {
95                9.hash(state);
96                arr.hash(state)
97            }
98            VariableType::Object(obj) => {
99                10.hash(state);
100
101                let obj = obj.borrow();
102                let mut pairs: Vec<_> = obj.iter().collect();
103                pairs.sort_by_key(|i| i.0);
104
105                Hash::hash(&pairs, state);
106            }
107        }
108    }
109}