zen_expression/variable/types/
mod.rs

1mod conv;
2mod util;
3
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use std::fmt::Display;
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    Constant(Rc<serde_json::Value>),
18    Array(Rc<VariableType>),
19    Object(HashMap<String, Rc<VariableType>>),
20}
21
22impl Default for VariableType {
23    fn default() -> Self {
24        VariableType::Null
25    }
26}
27
28impl Display for VariableType {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            VariableType::Any => write!(f, "any"),
32            VariableType::Null => write!(f, "null"),
33            VariableType::Bool => write!(f, "bool"),
34            VariableType::String => write!(f, "string"),
35            VariableType::Number => write!(f, "number"),
36            VariableType::Constant(c) => write!(f, "{c}"),
37            VariableType::Array(v) => write!(f, "{v}[]"),
38            VariableType::Object(_) => write!(f, "object"),
39        }
40    }
41}
42
43impl Hash for VariableType {
44    fn hash<H: Hasher>(&self, state: &mut H) {
45        match &self {
46            VariableType::Any => 0.hash(state),
47            VariableType::Null => 1.hash(state),
48            VariableType::Bool => 2.hash(state),
49            VariableType::String => 3.hash(state),
50            VariableType::Number => 4.hash(state),
51            VariableType::Constant(c) => c.hash(state),
52            VariableType::Array(arr) => arr.hash(state),
53            VariableType::Object(obj) => {
54                let mut pairs: Vec<_> = obj.iter().collect();
55                pairs.sort_by_key(|i| i.0);
56
57                Hash::hash(&pairs, state);
58            }
59        }
60    }
61}