zen_expression/variable/types/
mod.rs

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