rbs/
index.rs

1use crate::Value;
2use std::ops::{Index, IndexMut};
3
4impl Index<usize> for Value {
5    type Output = Value;
6
7    fn index(&self, index: usize) -> &Value {
8        match self {
9            Value::Array(arr) => &arr[index],
10            Value::Ext(_, ext) => {
11                return ext.index(index);
12            }
13            _ => &Value::Null,
14        }
15    }
16}
17
18impl IndexMut<usize> for Value {
19    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
20        match self {
21            Value::Array(arr) => &mut arr[index],
22            Value::Ext(_, ext) => {
23                return ext.index_mut(index);
24            }
25            _ => {
26                panic!("not an array!")
27            }
28        }
29    }
30}
31
32impl Index<&str> for Value {
33    type Output = Value;
34    fn index(&self, index: &str) -> &Self::Output {
35        match self {
36            Value::Map(m) => {
37                m.index(index)
38            }
39            Value::Ext(_, ext) => {
40                ext.index(index)
41            }
42            _ => {
43                &Value::Null
44            }
45        }
46    }
47}
48
49impl IndexMut<&str> for Value {
50    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
51        match self {
52            Value::Map(m) => m.index_mut(index),
53            Value::Ext(_, ext) => {
54                return ext.index_mut(index);
55            }
56            _ => {
57                panic!("not map type")
58            }
59        }
60    }
61}
62
63
64impl Index<Value> for Value {
65    type Output = Value;
66
67    fn index(&self, index: Value) -> &Self::Output {
68        return match self {
69            Value::Array(arr) => {
70                let idx = index.as_u64().unwrap_or_default() as usize;
71                arr.index(idx)
72            }
73            Value::Map(map) => {
74                let s = index.as_str().unwrap_or_default();
75                map.index(s)
76            }
77            Value::Ext(_, ext) => {
78                ext.index(index)
79            }
80            _ => {
81                &Value::Null
82            }
83        };
84    }
85}
86
87
88impl Index<&Value> for Value {
89    type Output = Value;
90
91    fn index(&self, index: &Value) -> &Self::Output {
92        return match self {
93            Value::Array(arr) => {
94                let idx = index.as_u64().unwrap_or_default() as usize;
95                arr.index(idx)
96            }
97            Value::Map(map) => {
98                let s = index.as_str().unwrap_or_default();
99                map.index(s)
100            }
101            Value::Ext(_, ext) => {
102                ext.index(index)
103            }
104            _ => {
105                &Value::Null
106            }
107        };
108    }
109}
110
111
112impl IndexMut<Value> for Value {
113    fn index_mut(&mut self, index: Value) -> &mut Self::Output {
114        match self {
115            Value::Array(arr) => {
116                let idx = index.as_u64().unwrap_or_default() as usize;
117                arr.index_mut(idx)
118            }
119            Value::Map(map) => {
120                let s = index.as_str().unwrap_or_default();
121                map.index_mut(s)
122            }
123            Value::Ext(_, ext) => {
124                ext.index_mut(index)
125            }
126            _ => {
127                panic!("not map/array type")
128            }
129        }
130    }
131}
132
133
134impl IndexMut<&Value> for Value {
135    fn index_mut(&mut self, index: &Value) -> &mut Self::Output {
136        match self {
137            Value::Array(arr) => {
138                let idx = index.as_u64().unwrap_or_default() as usize;
139                arr.index_mut(idx)
140            }
141            Value::Map(map) => {
142                let s = index.as_str().unwrap_or_default();
143                map.index_mut(s)
144            }
145            Value::Ext(_, ext) => {
146                ext.index_mut(index)
147            }
148            _ => {
149                panic!("not map/array type")
150            }
151        }
152    }
153}
154
155impl Value {
156    pub fn insert(&mut self, key: Value, value: Value) -> Option<Value> {
157        match self {
158            Value::Null => None,
159            Value::Bool(_) => None,
160            Value::I32(_) => None,
161            Value::I64(_) => None,
162            Value::U32(_) => None,
163            Value::U64(_) => None,
164            Value::F32(_) => None,
165            Value::F64(_) => None,
166            Value::String(_) => None,
167            Value::Binary(_) => None,
168            Value::Array(arr) => {
169                arr.insert(key.as_u64().unwrap_or_default() as usize, value);
170                None
171            }
172            Value::Map(m) => m.insert(key, value),
173            Value::Ext(_, m) => m.insert(key, value),
174        }
175    }
176
177    pub fn remove(&mut self, key: &Value) -> Value {
178        match self {
179            Value::Null => Value::Null,
180            Value::Bool(_) => Value::Null,
181            Value::I32(_) => Value::Null,
182            Value::I64(_) => Value::Null,
183            Value::U32(_) => Value::Null,
184            Value::U64(_) => Value::Null,
185            Value::F32(_) => Value::Null,
186            Value::F64(_) => Value::Null,
187            Value::String(_) => Value::Null,
188            Value::Binary(_) => Value::Null,
189            Value::Array(arr) => {
190                let index = key.as_u64().unwrap_or_default() as usize;
191                if index >= arr.len() {
192                    return Value::Null;
193                }
194                arr.remove(index)
195            }
196            Value::Map(m) => m.remove(key),
197            Value::Ext(_, e) => e.remove(key),
198        }
199    }
200}