summaryrefslogtreecommitdiff
path: root/src/context.rs
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2026-06-11 11:35:24 -0700
committerMike Buland <mike@xagasoft.com>2026-06-11 11:35:24 -0700
commitcffacc4dc4a46b525250772e87fd69a2f1c64dc2 (patch)
tree37ef3b84667668540bdb2bc64576f7eee9bc9c3c /src/context.rs
parent92dbff3cda66a2a677f0c2306bb8508c64884445 (diff)
downloadcrimtag-cffacc4dc4a46b525250772e87fd69a2f1c64dc2.tar.gz
crimtag-cffacc4dc4a46b525250772e87fd69a2f1c64dc2.tar.bz2
crimtag-cffacc4dc4a46b525250772e87fd69a2f1c64dc2.tar.xz
crimtag-cffacc4dc4a46b525250772e87fd69a2f1c64dc2.zip
Split things out, made things easier to use.
CrimError now has several constructors for different cases, so it's easier to use, using constructors is normalized now. Value has a load of From implementations now so it's much, much, much easier to use in practice.
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/context.rs b/src/context.rs
new file mode 100644
index 0000000..78a7940
--- /dev/null
+++ b/src/context.rs
@@ -0,0 +1,124 @@
1use std::collections::HashMap;
2
3use crate::{Identifier,IdentifierValue};
4
5pub type Context = HashMap<String,Value>;
6
7pub trait MappedStructure {
8 fn get_value(&self, id: &Identifier) -> Option<Value>;
9}
10
11impl MappedStructure for Context {
12 fn get_value(&self, id: &Identifier) -> Option<Value> {
13 if id.len() == 0 {
14 return None;
15 }
16 let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] {
17 self.get(s)
18 } else {
19 return None;
20 };
21 if cur_val.is_none() {
22 return None;
23 }
24
25 for idx in 1..id.len() {
26 if let IdentifierValue::Name(s) = &id[idx] &&
27 let Some(Value::Dictionary(d)) = &cur_val {
28 cur_val.replace( if let Some(v) = d.get(s) {
29 v
30 } else {
31 return None;
32 });
33 }
34 }
35
36 return cur_val.cloned();
37 }
38}
39
40#[derive(Debug,Clone)]
41pub enum Value {
42 Dictionary(Context),
43 List(Vec<Value>),
44 String(String),
45 Int(i64),
46 Float(f64),
47 Identifier(Identifier),
48}
49
50impl From<&str> for Value {
51 fn from(val: &str ) -> Value {
52 Value::String(val.into())
53 }
54}
55
56impl From<String> for Value {
57 fn from(val: String) -> Value {
58 Value::String(val)
59 }
60}
61
62impl From<i64> for Value {
63 fn from(val: i64) -> Value {
64 Value::Int(val)
65 }
66}
67
68impl From<f64> for Value {
69 fn from(val: f64) -> Value {
70 Value::Float(val)
71 }
72}
73
74impl From<Vec<Value>> for Value {
75 fn from(val: Vec<Value>) -> Value {
76 Value::List(val)
77 }
78}
79
80impl From<&[Value]> for Value {
81 fn from(val: &[Value]) -> Value {
82 Value::List(Vec::from(val))
83 }
84}
85
86impl From<Context> for Value {
87 fn from(val: Context) -> Value {
88 Value::Dictionary(val)
89 }
90}
91
92impl<const N: usize> From<[(String,Value); N]> for Value {
93 fn from(val: [(String,Value); N]) -> Value {
94 Value::Dictionary(HashMap::from(val))
95 }
96}
97
98/*
99impl MappedStructure for Value {
100 fn get_value(&self, id: &Identifier) -> Option<Value> {
101 if id.len() == 0 {
102 return Some(self);
103 }
104 match Value {
105 Value::Dictionary(dict) => {
106 }
107 }
108 let mut cur_val : Option<&Value> = Some(&self);
109
110 for idx in 0..id.len() {
111 if let IdentifierValue::Name(s) = &id[idx] &&
112 let Some(Value::Dictionary(d)) = &cur_val {
113 cur_val.replace( if let Some(v) = d.get(s) {
114 v
115 } else {
116 return None;
117 });
118 }
119 }
120
121 return cur_val.cloned();
122 }
123}
124*/