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,21 +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 ( ) {
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+ )
206193 }
207194
208195 fn get_key < ' a > ( & ' a self , key : & str ) -> Option < & ' a Self > {
@@ -212,15 +199,15 @@ impl SelectValue for IValue {
212199 }
213200 }
214201
215- fn get_index < ' a > ( & ' a self , index : usize ) -> Option < & ' a Self > {
202+ fn get_index ( & self , index : usize ) -> Option < & Self > {
216203 match self . as_array ( ) {
217204 Some ( arr) => arr. get ( index) ,
218205 _ => None ,
219206 }
220207 }
221208
222209 fn is_array ( & self ) -> bool {
223- self . is_array ( )
210+ self . is_array ( )
224211 }
225212
226213 fn get_str ( & self ) -> String {
@@ -232,7 +219,7 @@ impl SelectValue for IValue {
232219 }
233220 }
234221
235- fn as_str < ' a > ( & ' a self ) -> & ' a str {
222+ fn as_str ( & self ) -> & str {
236223 match self . as_string ( ) {
237224 Some ( s) => s. as_str ( ) ,
238225 _ => {
@@ -245,8 +232,7 @@ impl SelectValue for IValue {
245232 match self . to_bool ( ) {
246233 Some ( b) => b,
247234 _ => {
248- assert ! ( false , "not a bool" ) ;
249- false
235+ panic ! ( "not a bool" ) ;
250236 }
251237 }
252238 }
@@ -255,15 +241,13 @@ impl SelectValue for IValue {
255241 match self . as_number ( ) {
256242 Some ( n) => {
257243 if n. has_decimal_point ( ) {
258- assert ! ( false , "not a long" ) ;
259- 0
244+ panic ! ( "not a long" ) ;
260245 } else {
261246 n. to_i64 ( ) . unwrap ( )
262247 }
263248 }
264249 _ => {
265- assert ! ( false , "not a long" ) ;
266- 0
250+ panic ! ( "not a long" ) ;
267251 }
268252 }
269253 }
@@ -274,13 +258,11 @@ impl SelectValue for IValue {
274258 if n. has_decimal_point ( ) {
275259 n. to_f64 ( ) . unwrap ( )
276260 } else {
277- assert ! ( false , "not a double" ) ;
278- 0.1
261+ panic ! ( "not a double" ) ;
279262 }
280263 }
281264 _ => {
282- assert ! ( false , "not a double" ) ;
283- 0.1
265+ panic ! ( "not a double" ) ;
284266 }
285267 }
286268 }
0 commit comments