1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
use std::collections::HashMap;
use crate::{Identifier,IdentifierValue};
pub type Context = HashMap<String,Value>;
pub trait MappedStructure {
fn get_value(&self, id: &Identifier) -> Option<Value>;
}
impl MappedStructure for Context {
fn get_value(&self, id: &Identifier) -> Option<Value> {
if id.len() == 0 {
return None;
}
let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] {
self.get(s)
} else {
return None;
};
if cur_val.is_none() {
return None;
}
for idx in 1..id.len() {
if let IdentifierValue::Name(s) = &id[idx] &&
let Some(Value::Dictionary(d)) = &cur_val {
cur_val.replace( if let Some(v) = d.get(s) {
v
} else {
return None;
});
}
}
return cur_val.cloned();
}
}
#[derive(Debug,Clone)]
pub enum Value {
Dictionary(Context),
List(Vec<Value>),
String(String),
Int(i64),
Float(f64),
Bool(bool),
Identifier(Identifier),
}
impl From<&str> for Value {
fn from(val: &str ) -> Value {
Value::String(val.into())
}
}
impl From<String> for Value {
fn from(val: String) -> Value {
Value::String(val)
}
}
impl From<i64> for Value {
fn from(val: i64) -> Value {
Value::Int(val)
}
}
impl From<f64> for Value {
fn from(val: f64) -> Value {
Value::Float(val)
}
}
impl From<bool> for Value {
fn from(val: bool) -> Value {
Value::Bool(val)
}
}
impl From<Vec<Value>> for Value {
fn from(val: Vec<Value>) -> Value {
Value::List(val)
}
}
impl From<&[Value]> for Value {
fn from(val: &[Value]) -> Value {
Value::List(Vec::from(val))
}
}
impl From<Context> for Value {
fn from(val: Context) -> Value {
Value::Dictionary(val)
}
}
impl<const N: usize> From<[(String,Value); N]> for Value {
fn from(val: [(String,Value); N]) -> Value {
Value::Dictionary(HashMap::from(val))
}
}
/*
impl MappedStructure for Value {
fn get_value(&self, id: &Identifier) -> Option<Value> {
if id.len() == 0 {
return Some(self);
}
match Value {
Value::Dictionary(dict) => {
}
}
let mut cur_val : Option<&Value> = Some(&self);
for idx in 0..id.len() {
if let IdentifierValue::Name(s) = &id[idx] &&
let Some(Value::Dictionary(d)) = &cur_val {
cur_val.replace( if let Some(v) = d.get(s) {
v
} else {
return None;
});
}
}
return cur_val.cloned();
}
}
*/
|