@@ -22,6 +22,7 @@ pub enum MaxMindDBError {
2222 IoError ( String ) ,
2323 MapError ( String ) ,
2424 DecodingError ( String ) ,
25+ InvalidNetworkError ( String ) ,
2526}
2627
2728impl From < io:: Error > for MaxMindDBError {
@@ -43,6 +44,9 @@ impl Display for MaxMindDBError {
4344 MaxMindDBError :: IoError ( msg) => write ! ( fmt, "IoError: {}" , msg) ?,
4445 MaxMindDBError :: MapError ( msg) => write ! ( fmt, "MapError: {}" , msg) ?,
4546 MaxMindDBError :: DecodingError ( msg) => write ! ( fmt, "DecodingError: {}" , msg) ?,
47+ MaxMindDBError :: InvalidNetworkError ( msg) => {
48+ write ! ( fmt, "InvalidNetworkError: {}" , msg) ?
49+ }
4650 }
4751 Ok ( ( ) )
4852 }
@@ -143,7 +147,9 @@ impl<'de, T: Deserialize<'de>, S: AsRef<[u8]>> Iterator for Within<'de, T, S> {
143147
144148 if current. node > self . node_count {
145149 // This is a data node, emit it and we're done (until the following next call)
146- let ip_net = bytes_and_prefix_to_net ( & current. ip_bytes , current. prefix_len as u8 ) ;
150+ // TODO: error handling
151+ let ip_net =
152+ bytes_and_prefix_to_net ( & current. ip_bytes , current. prefix_len as u8 ) . unwrap ( ) ;
147153 //println!(" emit: current={:#?}, net={}", current, net);
148154 // TODO: error handling (should this be a method?)
149155 let rec = self . reader . resolve_data_pointer ( current. node ) . unwrap ( ) ;
@@ -271,8 +277,7 @@ impl<'de, S: AsRef<[u8]>> Reader<S> {
271277 //println!(" i={}", i);
272278 let bit = 1 & ( ip_bytes[ i >> 3 ] >> 7 - ( i % 8 ) ) as usize ;
273279 //println!(" bit={}", bit);
274- // TODO: error handling
275- node = self . read_node ( node, bit) . unwrap ( ) ;
280+ node = self . read_node ( node, bit) ?;
276281 //println!(" node={}", node);
277282 if node >= node_count {
278283 // We've hit a dead end before we exhausted our prefix
@@ -417,7 +422,7 @@ fn ip_to_bytes(address: IpAddr) -> Vec<u8> {
417422 }
418423}
419424
420- fn bytes_and_prefix_to_net ( bytes : & Vec < u8 > , prefix : u8 ) -> IpNetwork {
425+ fn bytes_and_prefix_to_net ( bytes : & Vec < u8 > , prefix : u8 ) -> Result < IpNetwork , MaxMindDBError > {
421426 let ip = match bytes. len ( ) {
422427 4 => IpAddr :: V4 ( Ipv4Addr :: new ( bytes[ 0 ] , bytes[ 1 ] , bytes[ 2 ] , bytes[ 3 ] ) ) ,
423428 16 => {
@@ -431,11 +436,14 @@ fn bytes_and_prefix_to_net(bytes: &Vec<u8>, prefix: u8) -> IpNetwork {
431436 let h = ( bytes[ 14 ] as u16 ) << 8 | bytes[ 15 ] as u16 ;
432437 IpAddr :: V6 ( Ipv6Addr :: new ( a, b, c, d, e, f, g, h) )
433438 }
434- // TODO: error handling
435- _ => IpAddr :: V4 ( Ipv4Addr :: new ( 0x00 , 0x00 , 0x00 , 0x00 ) ) ,
439+ // This should never happen
440+ _ => {
441+ return Err ( MaxMindDBError :: InvalidNetworkError (
442+ "invalid address" . to_owned ( ) ,
443+ ) )
444+ }
436445 } ;
437- // TODO: Error handling
438- IpNetwork :: new ( ip, prefix) . unwrap ( )
446+ IpNetwork :: new ( ip, prefix) . map_err ( |e| MaxMindDBError :: InvalidNetworkError ( e. to_string ( ) ) )
439447}
440448
441449fn find_metadata_start ( buf : & [ u8 ] ) -> Result < usize , MaxMindDBError > {
0 commit comments