1
1
use crate :: select_value:: { SelectValue , SelectValueType } ;
2
- use serde_json:: Value ;
3
2
use ijson:: { IValue , ValueType } ;
3
+ use serde_json:: Value ;
4
4
5
5
impl SelectValue for Value {
6
6
fn get_type ( & self ) -> SelectValueType {
@@ -11,9 +11,7 @@ impl SelectValue for Value {
11
11
Value :: Array ( _) => SelectValueType :: Array ,
12
12
Value :: Object ( _) => SelectValueType :: Object ,
13
13
Value :: Number ( n) => {
14
- if n. is_i64 ( ) {
15
- SelectValueType :: Long
16
- } else if n. is_u64 ( ) {
14
+ if n. is_i64 ( ) || n. is_u64 ( ) {
17
15
SelectValueType :: Long
18
16
} else if n. is_f64 ( ) {
19
17
SelectValueType :: Double
@@ -46,9 +44,9 @@ impl SelectValue for Value {
46
44
}
47
45
}
48
46
49
- fn items < ' a > ( & ' a self ) -> Option < Box < dyn Iterator < Item = ( & ' a str , & ' a Self ) > + ' a > > {
47
+ fn items < ' a > ( & ' a self ) -> Option < Box < dyn Iterator < Item = ( & ' a str , & ' a Self ) > + ' a > > {
50
48
match self {
51
- Value :: Object ( o) => Some ( Box :: new ( o. iter ( ) . map ( |( k, v) | ( & k[ ..] , v) ) ) ) ,
49
+ Value :: Object ( o) => Some ( Box :: new ( o. iter ( ) . map ( |( k, v) | ( & k[ ..] , v) ) ) ) ,
52
50
_ => None ,
53
51
}
54
52
}
@@ -68,18 +66,15 @@ impl SelectValue for Value {
68
66
}
69
67
}
70
68
71
- fn get_index < ' a > ( & ' a self , index : usize ) -> Option < & ' a Self > {
69
+ fn get_index ( & self , index : usize ) -> Option < & Self > {
72
70
match self {
73
71
Value :: Array ( arr) => arr. get ( index) ,
74
72
_ => None ,
75
73
}
76
74
}
77
75
78
76
fn is_array ( & self ) -> bool {
79
- match self {
80
- Value :: Array ( _) => true ,
81
- _ => false ,
82
- }
77
+ matches ! ( self , Value :: Array ( _) )
83
78
}
84
79
85
80
fn get_str ( & self ) -> String {
@@ -91,7 +86,7 @@ impl SelectValue for Value {
91
86
}
92
87
}
93
88
94
- fn as_str < ' a > ( & ' a self ) -> & ' a str {
89
+ fn as_str ( & self ) -> & str {
95
90
match self {
96
91
Value :: String ( s) => s. as_str ( ) ,
97
92
_ => {
@@ -104,8 +99,7 @@ impl SelectValue for Value {
104
99
match self {
105
100
Value :: Bool ( b) => * b,
106
101
_ => {
107
- assert ! ( false , "not a bool" ) ;
108
- false
102
+ panic ! ( "not a bool" ) ;
109
103
}
110
104
}
111
105
}
@@ -116,13 +110,11 @@ impl SelectValue for Value {
116
110
if n. is_i64 ( ) || n. is_u64 ( ) {
117
111
n. as_i64 ( ) . unwrap ( )
118
112
} else {
119
- assert ! ( false , "not a long" ) ;
120
- 0
113
+ panic ! ( "not a long" ) ;
121
114
}
122
115
}
123
116
_ => {
124
- assert ! ( false , "not a long" ) ;
125
- 0
117
+ panic ! ( "not a long" ) ;
126
118
}
127
119
}
128
120
}
@@ -133,13 +125,11 @@ impl SelectValue for Value {
133
125
if n. is_f64 ( ) {
134
126
n. as_f64 ( ) . unwrap ( )
135
127
} else {
136
- assert ! ( false , "not a double" ) ;
137
- 0.1
128
+ panic ! ( "not a double" ) ;
138
129
}
139
130
}
140
131
_ => {
141
- assert ! ( false , "not a double" ) ;
142
- 0.1
132
+ panic ! ( "not a double" ) ;
143
133
}
144
134
}
145
135
}
@@ -159,7 +149,7 @@ impl SelectValue for IValue {
159
149
SelectValueType :: Double
160
150
} else {
161
151
SelectValueType :: Long
162
- }
152
+ }
163
153
}
164
154
}
165
155
}
@@ -172,9 +162,9 @@ impl SelectValue for IValue {
172
162
}
173
163
174
164
fn values < ' a > ( & ' a self ) -> Option < Box < dyn Iterator < Item = & ' a Self > + ' a > > {
175
- if let Some ( arr) = self . as_array ( ) {
165
+ if let Some ( arr) = self . as_array ( ) {
176
166
Some ( Box :: new ( arr. iter ( ) ) )
177
- } else if let Some ( o) = self . as_object ( ) {
167
+ } else if let Some ( o) = self . as_object ( ) {
178
168
Some ( Box :: new ( o. values ( ) ) )
179
169
} else {
180
170
None
@@ -188,21 +178,18 @@ impl SelectValue for IValue {
188
178
}
189
179
}
190
180
191
- fn items < ' a > ( & ' a self ) -> Option < Box < dyn Iterator < Item = ( & ' a str , & ' a Self ) > + ' a > > {
181
+ fn items < ' a > ( & ' a self ) -> Option < Box < dyn Iterator < Item = ( & ' a str , & ' a Self ) > + ' a > > {
192
182
match self . as_object ( ) {
193
- Some ( o) => Some ( Box :: new ( o. iter ( ) . map ( |( k, v) | ( & k[ ..] , v) ) ) ) ,
183
+ Some ( o) => Some ( Box :: new ( o. iter ( ) . map ( |( k, v) | ( & k[ ..] , v) ) ) ) ,
194
184
_ => None ,
195
185
}
196
186
}
197
187
198
188
fn len ( & self ) -> Option < usize > {
199
- if let Some ( arr) = self . as_array ( ) {
200
- Some ( arr. len ( ) )
201
- } else if let Some ( obj) = self . as_object ( ) {
202
- Some ( obj. len ( ) )
203
- } else {
204
- None
205
- }
189
+ self . as_array ( ) . map_or_else (
190
+ || self . as_object ( ) . map ( ijson:: IObject :: len) ,
191
+ |arr| Some ( arr. len ( ) ) ,
192
+ )
206
193
}
207
194
208
195
fn get_key < ' a > ( & ' a self , key : & str ) -> Option < & ' a Self > {
@@ -212,15 +199,15 @@ impl SelectValue for IValue {
212
199
}
213
200
}
214
201
215
- fn get_index < ' a > ( & ' a self , index : usize ) -> Option < & ' a Self > {
202
+ fn get_index ( & self , index : usize ) -> Option < & Self > {
216
203
match self . as_array ( ) {
217
204
Some ( arr) => arr. get ( index) ,
218
205
_ => None ,
219
206
}
220
207
}
221
208
222
209
fn is_array ( & self ) -> bool {
223
- self . is_array ( )
210
+ self . is_array ( )
224
211
}
225
212
226
213
fn get_str ( & self ) -> String {
@@ -232,7 +219,7 @@ impl SelectValue for IValue {
232
219
}
233
220
}
234
221
235
- fn as_str < ' a > ( & ' a self ) -> & ' a str {
222
+ fn as_str ( & self ) -> & str {
236
223
match self . as_string ( ) {
237
224
Some ( s) => s. as_str ( ) ,
238
225
_ => {
@@ -245,8 +232,7 @@ impl SelectValue for IValue {
245
232
match self . to_bool ( ) {
246
233
Some ( b) => b,
247
234
_ => {
248
- assert ! ( false , "not a bool" ) ;
249
- false
235
+ panic ! ( "not a bool" ) ;
250
236
}
251
237
}
252
238
}
@@ -255,15 +241,13 @@ impl SelectValue for IValue {
255
241
match self . as_number ( ) {
256
242
Some ( n) => {
257
243
if n. has_decimal_point ( ) {
258
- assert ! ( false , "not a long" ) ;
259
- 0
244
+ panic ! ( "not a long" ) ;
260
245
} else {
261
246
n. to_i64 ( ) . unwrap ( )
262
247
}
263
248
}
264
249
_ => {
265
- assert ! ( false , "not a long" ) ;
266
- 0
250
+ panic ! ( "not a long" ) ;
267
251
}
268
252
}
269
253
}
@@ -274,13 +258,11 @@ impl SelectValue for IValue {
274
258
if n. has_decimal_point ( ) {
275
259
n. to_f64 ( ) . unwrap ( )
276
260
} else {
277
- assert ! ( false , "not a double" ) ;
278
- 0.1
261
+ panic ! ( "not a double" ) ;
279
262
}
280
263
}
281
264
_ => {
282
- assert ! ( false , "not a double" ) ;
283
- 0.1
265
+ panic ! ( "not a double" ) ;
284
266
}
285
267
}
286
268
}
0 commit comments