@@ -4,9 +4,15 @@ extern crate serialize;
4
4
use std:: str;
5
5
6
6
use super :: { Array , Boolean , Byte , DataRecord , DecodingError , Double , Error ,
7
- Float , Int32 , Map , String , Uint16 , Uint32 , Uint64 } ;
7
+ Float , Int32 , Map , Null , String , Uint16 , Uint32 , Uint64 } ;
8
8
9
9
macro_rules! expect(
10
+ ( $e: expr, Null ) => ( {
11
+ match $e {
12
+ Null => Ok ( ( ) ) ,
13
+ other => Err ( DecodingError ( format!( "Error decoding Null as {}" , other) ) )
14
+ }
15
+ } ) ;
10
16
( $e: expr, $t: ident) => ( {
11
17
match $e {
12
18
$t( v) => Ok ( v) ,
@@ -40,7 +46,7 @@ pub type DecodeResult<T> = Result<T, Error>;
40
46
impl serialize:: Decoder < Error > for Decoder {
41
47
fn read_nil ( & mut self ) -> DecodeResult < ( ) > {
42
48
debug ! ( "read_nil" ) ;
43
- Err ( DecodingError ( "nil data not supported by MaxMind DB format" . to_string ( ) ) )
49
+ expect ! ( self . pop ( ) , Null )
44
50
}
45
51
46
52
fn read_u64 ( & mut self ) -> DecodeResult < u64 > {
@@ -213,7 +219,13 @@ impl serialize::Decoder<Error> for Decoder {
213
219
let mut obj = try!( expect ! ( self . pop( ) , Map ) ) ;
214
220
215
221
let value = match obj. pop( & name. to_string( ) ) {
216
- None => return Err ( DecodingError ( format ! ( "Unknown struct field {}" , name. to_string( ) ) ) ) ,
222
+ None => {
223
+ self. stack. push( Null ) ;
224
+ match f ( self ) {
225
+ Ok ( v) => v ,
226
+ Err ( _) => return Err ( DecodingError ( format ! ( "Unknown struct field {}" , name. to_string( ) ) ) ) ,
227
+ }
228
+ } ,
217
229
Some ( record) => {
218
230
self . stack. push( record) ;
219
231
try!( f ( self ) )
@@ -252,9 +264,11 @@ impl serialize::Decoder<Error> for Decoder {
252
264
}
253
265
254
266
fn read_option < T > ( & mut self , f : |& mut Decoder , bool| -> DecodeResult < T > ) -> DecodeResult < T > {
255
- let value = self . pop ( ) ;
256
- self . stack . push ( value) ;
257
- f ( self , true )
267
+ debug ! ( "read_option()" ) ;
268
+ match self . pop ( ) {
269
+ Null => f ( self , false ) ,
270
+ value => { self . stack . push ( value) ; f ( self , true ) }
271
+ }
258
272
}
259
273
260
274
fn read_seq < T > ( & mut self , f : |& mut Decoder , uint| -> DecodeResult < T > ) -> DecodeResult < T > {
0 commit comments