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
use crate::{to_value, Value};
use serde::Serialize;
use std::ops::{Index, IndexMut};
impl Index<usize> for Value {
type Output = Value;
fn index(&self, index: usize) -> &Value {
match self {
Value::Array(arr) => &arr[index],
Value::Ext(_, ext) => {
return ext.index(index);
}
_ => {
panic!("not an array!")
}
}
}
}
impl IndexMut<usize> for Value {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match self {
Value::Array(arr) => &mut arr[index],
Value::Ext(_, ext) => {
return ext.index_mut(index);
}
_ => {
panic!("not an array!")
}
}
}
}
impl Index<&str> for Value {
type Output = Value;
fn index(&self, index: &str) -> &Self::Output {
match self {
Value::Map(m) => {
for (k, v) in m {
if k.as_str().unwrap_or_default().eq(index) {
return v;
}
}
return &Value::Null;
}
Value::Ext(_, ext) => {
return ext.index(index);
}
_ => {
return &Value::Null;
}
}
}
}
impl IndexMut<&str> for Value {
fn index_mut(&mut self, index: &str) -> &mut Self::Output {
match self {
Value::Map(m) => {
for (k, v) in m {
if k.as_str().unwrap_or_default().eq(index) {
return v;
}
}
panic!("not have index={}", index)
}
Value::Ext(_, ext) => {
return ext.index_mut(index);
}
_ => {
panic!("not have index={}", index)
}
}
}
}