@@ -4,8 +4,8 @@ extern crate rustc_serialize;
4
4
use std:: string;
5
5
6
6
use super :: { DataRecord , MaxMindDBError } ;
7
- use super :: DataRecord :: { Array , Boolean , Byte , Double , Float , Int32 , Map , Null ,
8
- String , Uint16 , Uint32 , Uint64 } ;
7
+ use super :: DataRecord :: { Array , Boolean , Byte , Double , Float , Int32 , Map , Null , String , Uint16 ,
8
+ Uint32 , Uint64 } ;
9
9
use super :: MaxMindDBError :: DecodingError ;
10
10
11
11
macro_rules! expect(
@@ -18,21 +18,21 @@ macro_rules! expect(
18
18
( $e: expr, $t: ident) => ( {
19
19
match $e {
20
20
$t( v) => Ok ( v) ,
21
- other => Err ( DecodingError ( format!( "Error decoding {:?} as {:?}" , other, stringify!( $t) ) ) )
21
+ other => Err ( DecodingError ( format!( "Error decoding {:?} as {:?}" ,
22
+ other, stringify!( $t) ) ) )
22
23
}
23
24
} )
24
25
) ;
25
26
27
+ #[ derive( Debug ) ]
26
28
pub struct Decoder {
27
29
pub stack : Vec < DataRecord > ,
28
30
}
29
31
30
32
impl Decoder {
31
33
/// Creates a new decoder instance for decoding the specified JSON value.
32
34
pub fn new ( record : DataRecord ) -> Decoder {
33
- Decoder {
34
- stack : vec ! ( record) ,
35
- }
35
+ Decoder { stack : vec ! [ record] }
36
36
}
37
37
}
38
38
@@ -90,12 +90,12 @@ impl rustc_serialize::Decoder for Decoder {
90
90
91
91
fn read_i16 ( & mut self ) -> DecodeResult < i16 > {
92
92
debug ! ( "read_i16" ) ;
93
- Err ( DecodingError ( "i16 data not supported by MaxMind DB format" . to_string ( ) ) )
93
+ Err ( DecodingError ( "i16 data not supported by MaxMind DB format" . to_owned ( ) ) )
94
94
}
95
95
96
96
fn read_i8 ( & mut self ) -> DecodeResult < i8 > {
97
97
debug ! ( "read_i8" ) ;
98
- Err ( DecodingError ( "i8 data not supported by MaxMind DB format" . to_string ( ) ) )
98
+ Err ( DecodingError ( "i8 data not supported by MaxMind DB format" . to_owned ( ) ) )
99
99
}
100
100
101
101
fn read_isize ( & mut self ) -> DecodeResult < isize > {
@@ -120,23 +120,20 @@ impl rustc_serialize::Decoder for Decoder {
120
120
121
121
fn read_char ( & mut self ) -> DecodeResult < char > {
122
122
let s = try!( self . read_str ( ) ) ;
123
- {
124
- let mut it = s. chars ( ) ;
125
- match ( it. next ( ) , it. next ( ) ) {
126
- // exactly one character
127
- ( Some ( c) , None ) => return Ok ( c) ,
128
- _ => ( )
129
- }
123
+ let mut it = s. chars ( ) ;
124
+ if let ( Some ( c) , None ) = ( it. next ( ) , it. next ( ) ) {
125
+ Ok ( c)
126
+ } else {
127
+ Err ( DecodingError ( format ! ( "char {:?}" , s) ) )
130
128
}
131
- Err ( DecodingError ( format ! ( "char {:?}" , s) ) )
132
129
}
133
130
134
131
fn read_str ( & mut self ) -> DecodeResult < string:: String > {
135
132
debug ! ( "read_str" ) ;
136
133
Ok ( try!( expect ! ( self . pop( ) , String ) ) )
137
134
}
138
135
139
- fn read_enum < T , F > ( & mut self , name : & str , f : F ) -> DecodeResult < T >
136
+ fn read_enum < T , F > ( & mut self , name : & str , f : F ) -> DecodeResult < T >
140
137
where F : FnOnce ( & mut Decoder ) -> DecodeResult < T >
141
138
{
142
139
debug ! ( "read_enum({:?})" , name) ;
@@ -151,30 +148,28 @@ impl rustc_serialize::Decoder for Decoder {
151
148
let name = match self . pop ( ) {
152
149
String ( s) => s,
153
150
Map ( mut o) => {
154
- let n = match o. remove ( & "variant" . to_string ( ) ) {
151
+ let n = match o. remove ( & "variant" . to_owned ( ) ) {
155
152
Some ( String ( s) ) => s,
156
- Some ( val) => return Err ( DecodingError ( format ! ( "enum {:?}" , val) ) ) ,
157
- None => return Err ( DecodingError ( "variant" . to_string ( ) ) )
153
+ Some ( val) => return Err ( DecodingError ( format ! ( "enum {:?}" , val) ) ) ,
154
+ None => return Err ( DecodingError ( "variant" . to_owned ( ) ) ) ,
158
155
} ;
159
- match o. remove ( & "fields" . to_string ( ) ) {
156
+ match o. remove ( & "fields" . to_owned ( ) ) {
160
157
Some ( Array ( l) ) => {
161
158
for field in l. into_iter ( ) . rev ( ) {
162
159
self . stack . push ( field. clone ( ) ) ;
163
160
}
164
- } ,
161
+ }
165
162
Some ( val) => return Err ( DecodingError ( format ! ( "enum {:?}" , val) ) ) ,
166
- None => return Err ( DecodingError ( "fields" . to_string ( ) ) )
163
+ None => return Err ( DecodingError ( "fields" . to_owned ( ) ) ) ,
167
164
}
168
165
n
169
166
}
170
- json => return Err ( DecodingError ( format ! ( "enum {:?}" , json) ) )
167
+ json => return Err ( DecodingError ( format ! ( "enum {:?}" , json) ) ) ,
171
168
} ;
172
169
let idx = match names. iter ( )
173
- . position ( |n| {
174
- * n == name
175
- } ) {
170
+ . position ( |n| * n == name) {
176
171
Some ( idx) => idx,
177
- None => return Err ( DecodingError ( name) )
172
+ None => return Err ( DecodingError ( name) ) ,
178
173
} ;
179
174
f ( self , idx)
180
175
}
@@ -201,7 +196,9 @@ impl rustc_serialize::Decoder for Decoder {
201
196
-> DecodeResult < T >
202
197
where F : FnOnce ( & mut Decoder ) -> DecodeResult < T >
203
198
{
204
- debug ! ( "read_enum_struct_variant_field(name={:?}, idx={:?})" , name, idx) ;
199
+ debug ! ( "read_enum_struct_variant_field(name={:?}, idx={:?})" ,
200
+ name,
201
+ idx) ;
205
202
self . read_enum_variant_arg ( idx, f)
206
203
}
207
204
@@ -220,14 +217,15 @@ impl rustc_serialize::Decoder for Decoder {
220
217
debug ! ( "read_struct_field(name={:?}, idx={:?})" , name, idx) ;
221
218
let mut obj = try!( expect ! ( self . pop( ) , Map ) ) ;
222
219
223
- let value = match obj. remove ( & name. to_string ( ) ) {
220
+ let value = match obj. remove ( & name. to_owned ( ) ) {
224
221
None => {
225
222
self . stack . push ( Null ) ;
226
223
match f ( self ) {
227
224
Ok ( v) => v,
228
- Err ( _) => return Err ( DecodingError ( format ! ( "Unknown struct field {:?}" , name. to_string( ) ) ) ) ,
225
+ Err ( _) => return Err ( DecodingError ( format ! ( "Unknown struct field {:?}" ,
226
+ name. to_owned( ) ) ) ) ,
229
227
}
230
- } ,
228
+ }
231
229
Some ( record) => {
232
230
self . stack . push ( record) ;
233
231
try!( f ( self ) )
@@ -277,7 +275,10 @@ impl rustc_serialize::Decoder for Decoder {
277
275
debug ! ( "read_option()" ) ;
278
276
match self . pop ( ) {
279
277
Null => f ( self , false ) ,
280
- value => { self . stack . push ( value) ; f ( self , true ) }
278
+ value => {
279
+ self . stack . push ( value) ;
280
+ f ( self , true )
281
+ }
281
282
}
282
283
}
283
284
@@ -328,6 +329,6 @@ impl rustc_serialize::Decoder for Decoder {
328
329
}
329
330
330
331
fn error ( & mut self , err : & str ) -> MaxMindDBError {
331
- DecodingError ( err. to_string ( ) )
332
+ DecodingError ( err. to_owned ( ) )
332
333
}
333
334
}
0 commit comments