teo_parser/value/
index.rs1use std::ops;
2use indexmap::IndexMap;
3use super::value::Value;
4
5pub trait Index {
9 fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>;
10 fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value>;
11 fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value;
12}
13
14impl Index for usize {
15
16 fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
17 match v {
18 Value::Array(vec) => vec.get(*self),
19 Value::Tuple(vec) => vec.get(*self),
20 _ => None,
21 }
22 }
23
24 fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
25 match v {
26 Value::Array(vec) => vec.get_mut(*self),
27 Value::Tuple(vec) => vec.get_mut(*self),
28 _ => None,
29 }
30 }
31
32 fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
33 match v {
34 Value::Array(vec) => {
35 let len = vec.len();
36 vec.get_mut(*self).unwrap_or_else(|| {
37 panic!(
38 "cannot access index {} of parser value array of length {}",
39 self, len
40 )
41 })
42 },
43 Value::Tuple(vec) => {
44 let len = vec.len();
45 vec.get_mut(*self).unwrap_or_else(|| {
46 panic!(
47 "cannot access index {} of parser value tuple of length {}",
48 self, len
49 )
50 })
51 },
52 _ => panic!("cannot access index {} of parser value {}", self, v),
53 }
54 }
55}
56
57impl Index for str {
58
59 fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
60 match v {
61 Value::Dictionary(map) => map.get(self),
62 _ => None,
63 }
64 }
65
66 fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
67 match v {
68 Value::Dictionary(map) => map.get_mut(self),
69 _ => None,
70 }
71 }
72
73 fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
74 if let Value::Null = v {
75 *v = Value::Dictionary(IndexMap::new());
76 }
77 match v {
78 Value::Dictionary(map) => map.entry(self.to_owned()).or_insert(Value::Null),
79 _ => panic!("cannot access key {:?} in parser value {}", self, v),
80 }
81 }
82}
83
84impl Index for String {
85
86 fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
87 self[..].index_into(v)
88 }
89
90 fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
91 self[..].index_into_mut(v)
92 }
93
94 fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
95 self[..].index_or_insert(v)
96 }
97}
98
99impl<'a, T> Index for &'a T where T: ?Sized + Index, {
100
101 fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
102 (**self).index_into(v)
103 }
104
105 fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
106 (**self).index_into_mut(v)
107 }
108
109 fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
110 (**self).index_or_insert(v)
111 }
112}
113
114impl<I> ops::Index<I> for Value where I: Index {
115
116 type Output = Value;
117
118 fn index(&self, index: I) -> &Value {
119 static NULL: Value = Value::Null;
120 index.index_into(self).unwrap_or(&NULL)
121 }
122}
123
124impl<I> ops::IndexMut<I> for Value where I: Index {
125
126 fn index_mut(&mut self, index: I) -> &mut Value {
127 index.index_or_insert(self)
128 }
129}