panopticon_core/data/
entry.rs1use crate::imports::*;
2
3#[derive(Debug, Clone, PartialEq)]
16pub enum StoreEntry {
17 Var {
19 value: Value,
21 ty: Type,
23 },
24 Array(Vec<StoreEntry>),
26 Map(HashMap<String, StoreEntry>),
28}
29
30impl StoreEntry {
31 pub fn get_key(&self, key: &str) -> Result<&StoreEntry, AccessError> {
35 match self {
36 StoreEntry::Var { .. } => Err(AccessError::NotAMap("Var")),
37 StoreEntry::Array(_) => Err(AccessError::NotAMap("Array")),
38 StoreEntry::Map(entries) => entries
39 .get(key)
40 .ok_or_else(|| AccessError::NotFound(key.into())),
41 }
42 }
43 pub fn get_index(&self, index: usize) -> Result<&StoreEntry, AccessError> {
48 match self {
49 StoreEntry::Var { .. } => Err(AccessError::NotAnArray("Var")),
50 StoreEntry::Array(items) => {
51 items.get(index).ok_or(AccessError::IndexOutOfBounds(index))
52 }
53 StoreEntry::Map(_) => Err(AccessError::NotAnArray("Map")),
54 }
55 }
56 pub fn get_value(&self) -> Result<&Value, AccessError> {
59 match self {
60 StoreEntry::Var { value, .. } => Ok(value),
61 StoreEntry::Array(_) => Err(AccessError::NotAVar("Array")),
62 StoreEntry::Map(_) => Err(AccessError::NotAVar("Map")),
63 }
64 }
65 pub fn as_var(&self) -> Result<(&Value, &Type), AccessError> {
68 match self {
69 StoreEntry::Var { value, ty } => Ok((value, ty)),
70 StoreEntry::Array(_) => Err(AccessError::NotAVar("Array")),
71 StoreEntry::Map(_) => Err(AccessError::NotAVar("Map")),
72 }
73 }
74 pub fn as_array(&self) -> Result<&Vec<StoreEntry>, AccessError> {
78 match self {
79 StoreEntry::Var { .. } => Err(AccessError::NotAnArray("Var")),
80 StoreEntry::Array(items) => Ok(items),
81 StoreEntry::Map(_) => Err(AccessError::NotAnArray("Map")),
82 }
83 }
84 pub fn as_map(&self) -> Result<&HashMap<String, StoreEntry>, AccessError> {
88 match self {
89 StoreEntry::Var { .. } => Err(AccessError::NotAMap("Var")),
90 StoreEntry::Array(_) => Err(AccessError::NotAMap("Array")),
91 StoreEntry::Map(entries) => Ok(entries),
92 }
93 }
94}
95
96impl std::cmp::Eq for StoreEntry {}
97
98impl std::hash::Hash for StoreEntry {
99 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
100 std::mem::discriminant(self).hash(state);
101 match self {
102 StoreEntry::Var { value, ty } => {
103 value.hash(state);
104 ty.hash(state);
105 }
106 StoreEntry::Array(items) => items.hash(state),
107 StoreEntry::Map(map) => {
108 let mut keys: Vec<_> = map.keys().collect();
109 keys.sort();
110 for key in keys {
111 key.hash(state);
112 map[key].hash(state);
113 }
114 }
115 }
116 }
117}
118
119impl<T: Into<Value>> From<T> for StoreEntry {
120 fn from(v: T) -> Self {
121 let value = v.into();
122 StoreEntry::Var {
123 ty: value.get_type(),
124 value,
125 }
126 }
127}
128
129impl From<&Value> for StoreEntry {
130 fn from(v: &Value) -> Self {
131 let value = v.clone();
132 StoreEntry::Var {
133 ty: value.get_type(),
134 value,
135 }
136 }
137}
138
139impl From<Vec<StoreEntry>> for StoreEntry {
140 fn from(items: Vec<StoreEntry>) -> Self {
141 StoreEntry::Array(items)
142 }
143}