11use crate :: select_value:: { SelectValue , SelectValueType } ;
2- use serde_json:: Value ;
32use ijson:: { IValue , ValueType } ;
3+ use serde_json:: Value ;
44
55impl SelectValue for Value {
66 fn get_type ( & self ) -> SelectValueType {
@@ -11,9 +11,7 @@ impl SelectValue for Value {
1111 Value :: Array ( _) => SelectValueType :: Array ,
1212 Value :: Object ( _) => SelectValueType :: Object ,
1313 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 ( ) {
1715 SelectValueType :: Long
1816 } else if n. is_f64 ( ) {
1917 SelectValueType :: Double
@@ -46,9 +44,9 @@ impl SelectValue for Value {
4644 }
4745 }
4846
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 > > {
5048 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) ) ) ) ,
5250 _ => None ,
5351 }
5452 }
@@ -68,18 +66,15 @@ impl SelectValue for Value {
6866 }
6967 }
7068
71- fn get_index < ' a > ( & ' a self , index : usize ) -> Option < & ' a Self > {
69+ fn get_index ( & self , index : usize ) -> Option < & Self > {
7270 match self {
7371 Value :: Array ( arr) => arr. get ( index) ,
7472 _ => None ,
7573 }
7674 }
7775
7876 fn is_array ( & self ) -> bool {
79- match self {
80- Value :: Array ( _) => true ,
81- _ => false ,
82- }
77+ matches ! ( self , Value :: Array ( _) )
8378 }
8479
8580 fn get_str ( & self ) -> String {
@@ -91,7 +86,7 @@ impl SelectValue for Value {
9186 }
9287 }
9388
94- fn as_str < ' a > ( & ' a self ) -> & ' a str {
89+ fn as_str ( & self ) -> & str {
9590 match self {
9691 Value :: String ( s) => s. as_str ( ) ,
9792 _ => {
@@ -104,8 +99,7 @@ impl SelectValue for Value {
10499 match self {
105100 Value :: Bool ( b) => * b,
106101 _ => {
107- assert ! ( false , "not a bool" ) ;
108- false
102+ panic ! ( "not a bool" ) ;
109103 }
110104 }
111105 }
@@ -116,13 +110,11 @@ impl SelectValue for Value {
116110 if n. is_i64 ( ) || n. is_u64 ( ) {
117111 n. as_i64 ( ) . unwrap ( )
118112 } else {
119- assert ! ( false , "not a long" ) ;
120- 0
113+ panic ! ( "not a long" ) ;
121114 }
122115 }
123116 _ => {
124- assert ! ( false , "not a long" ) ;
125- 0
117+ panic ! ( "not a long" ) ;
126118 }
127119 }
128120 }
@@ -133,13 +125,11 @@ impl SelectValue for Value {
133125 if n. is_f64 ( ) {
134126 n. as_f64 ( ) . unwrap ( )
135127 } else {
136- assert ! ( false , "not a double" ) ;
137- 0.1
128+ panic ! ( "not a double" ) ;
138129 }
139130 }
140131 _ => {
141- assert ! ( false , "not a double" ) ;
142- 0.1
132+ panic ! ( "not a double" ) ;
143133 }
144134 }
145135 }
@@ -159,7 +149,7 @@ impl SelectValue for IValue {
159149 SelectValueType :: Double
160150 } else {
161151 SelectValueType :: Long
162- }
152+ }
163153 }
164154 }
165155 }
@@ -172,9 +162,9 @@ impl SelectValue for IValue {
172162 }
173163
174164 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 ( ) {
176166 Some ( Box :: new ( arr. iter ( ) ) )
177- } else if let Some ( o) = self . as_object ( ) {
167+ } else if let Some ( o) = self . as_object ( ) {
178168 Some ( Box :: new ( o. values ( ) ) )
179169 } else {
180170 None
@@ -188,20 +178,18 @@ impl SelectValue for IValue {
188178 }
189179 }
190180
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 > > {
192182 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) ) ) ) ,
194184 _ => None ,
195185 }
196186 }
197187
198188 fn len ( & self ) -> Option < usize > {
199- if let Some ( arr) = self . as_array ( ) {
189+ if let Some ( arr) = self . as_array ( ) {
200190 Some ( arr. len ( ) )
201- } else if let Some ( obj) = self . as_object ( ) {
202- Some ( obj. len ( ) )
203191 } else {
204- None
192+ self . as_object ( ) . map ( |obj| obj . len ( ) )
205193 }
206194 }
207195
@@ -212,15 +200,15 @@ impl SelectValue for IValue {
212200 }
213201 }
214202
215- fn get_index < ' a > ( & ' a self , index : usize ) -> Option < & ' a Self > {
203+ fn get_index ( & self , index : usize ) -> Option < & Self > {
216204 match self . as_array ( ) {
217205 Some ( arr) => arr. get ( index) ,
218206 _ => None ,
219207 }
220208 }
221209
222210 fn is_array ( & self ) -> bool {
223- self . is_array ( )
211+ self . is_array ( )
224212 }
225213
226214 fn get_str ( & self ) -> String {
@@ -232,7 +220,7 @@ impl SelectValue for IValue {
232220 }
233221 }
234222
235- fn as_str < ' a > ( & ' a self ) -> & ' a str {
223+ fn as_str ( & self ) -> & str {
236224 match self . as_string ( ) {
237225 Some ( s) => s. as_str ( ) ,
238226 _ => {
@@ -245,8 +233,7 @@ impl SelectValue for IValue {
245233 match self . to_bool ( ) {
246234 Some ( b) => b,
247235 _ => {
248- assert ! ( false , "not a bool" ) ;
249- false
236+ panic ! ( "not a bool" ) ;
250237 }
251238 }
252239 }
@@ -255,15 +242,13 @@ impl SelectValue for IValue {
255242 match self . as_number ( ) {
256243 Some ( n) => {
257244 if n. has_decimal_point ( ) {
258- assert ! ( false , "not a long" ) ;
259- 0
245+ panic ! ( "not a long" ) ;
260246 } else {
261247 n. to_i64 ( ) . unwrap ( )
262248 }
263249 }
264250 _ => {
265- assert ! ( false , "not a long" ) ;
266- 0
251+ panic ! ( "not a long" ) ;
267252 }
268253 }
269254 }
@@ -274,13 +259,11 @@ impl SelectValue for IValue {
274259 if n. has_decimal_point ( ) {
275260 n. to_f64 ( ) . unwrap ( )
276261 } else {
277- assert ! ( false , "not a double" ) ;
278- 0.1
262+ panic ! ( "not a double" ) ;
279263 }
280264 }
281265 _ => {
282- assert ! ( false , "not a double" ) ;
283- 0.1
266+ panic ! ( "not a double" ) ;
284267 }
285268 }
286269 }
0 commit comments