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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use super::error::ScriptError;
use crate::script_error;
use evalexpr::Value;
use json5_nodes::{Iter, JsonNode, Location};
pub trait JsonNodeExtra {
fn is_null(&self) -> bool;
fn is_bool(&self) -> bool;
fn is_integer(&self) -> bool;
fn is_float(&self) -> bool;
fn is_string(&self) -> bool;
fn is_array(&self) -> bool;
fn is_object(&self) -> bool;
fn get_location(self: &Self) -> Option<Location>;
fn get_object_entry<'a>(self: &'a Self, name: &str) -> Result<&'a JsonNode, ScriptError>;
fn get_object_iter(self: &Self) -> Result<Iter<String, JsonNode>, ScriptError>;
fn get_array_iter(self: &Self) -> Result<std::slice::Iter<JsonNode>, ScriptError>;
fn get_value(self: &Self) -> Value;
fn get_string(self: &Self) -> String;
}
impl JsonNodeExtra for JsonNode {
fn is_null(&self) -> bool {
if let JsonNode::Null(_) = self {
true
} else {
false
}
}
fn is_bool(&self) -> bool {
if let JsonNode::Bool(_, _) = self {
true
} else {
false
}
}
fn is_integer(&self) -> bool {
if let JsonNode::Integer(_, _) = self {
true
} else {
false
}
}
fn is_float(&self) -> bool {
if let JsonNode::Float(_, _) = self {
true
} else {
false
}
}
fn is_string(self: &Self) -> bool {
if let JsonNode::String(_, _) = self {
true
} else {
false
}
}
fn is_array(self: &Self) -> bool {
if let JsonNode::Array(_, _) = self {
true
} else {
false
}
}
fn is_object(self: &Self) -> bool {
if let JsonNode::Object(_, _) = self {
true
} else {
false
}
}
fn get_location(self: &Self) -> Option<Location> {
match self {
JsonNode::Null(location)
| JsonNode::Bool(_, location)
| JsonNode::Integer(_, location)
| JsonNode::Float(_, location)
| JsonNode::String(_, location)
| JsonNode::Array(_, location)
| JsonNode::Object(_, location) => *location,
}
}
fn get_object_entry<'a>(self: &'a Self, name: &str) -> Result<&'a JsonNode, ScriptError> {
if let JsonNode::Object(map, ..) = self {
if let Some(node) = map.get(name) {
Ok(node)
} else {
Err(script_error!(
format!("Object entry '{}' not found", name),
self
))
}
} else {
Err(script_error!("Not an object", self))
}
}
fn get_object_iter(self: &Self) -> Result<Iter<String, JsonNode>, ScriptError> {
if let JsonNode::Object(map, _) = self {
Ok(map.iter())
} else {
Err(script_error!("Not an object", self))
}
}
fn get_array_iter(self: &Self) -> Result<std::slice::Iter<JsonNode>, ScriptError> {
if let JsonNode::Array(array, _) = self {
Ok(array.iter())
} else {
Err(script_error!("Not an array", self))
}
}
fn get_value(self: &Self) -> Value {
match self {
JsonNode::Null(..) => Value::Empty,
JsonNode::Integer(value, ..) => Value::from(*value),
JsonNode::Float(value, ..) => Value::from(*value),
JsonNode::Bool(value, ..) => Value::from(*value),
JsonNode::String(value, ..) => Value::from((*value).to_owned()),
_ => Value::Empty,
}
}
fn get_string(self: &Self) -> String {
match self {
JsonNode::Null(..) => "null".to_string(),
JsonNode::Integer(value, ..) => (*value).to_string(),
JsonNode::Float(value, ..) => (*value).to_string(),
JsonNode::Bool(value, ..) => (*value).to_string(),
JsonNode::String(value, ..) => (*value).to_string(),
_ => String::new(),
}
}
}