Skip to content

Commit f47ac38

Browse files
committed
Switch back to IpAddr
1 parent c22c070 commit f47ac38

File tree

5 files changed

+55
-58
lines changed

5 files changed

+55
-58
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log #
2+
3+
## 0.7.0 - 2016-05-15
4+
5+
* API CHANGE: `lookup` takes an `IpAddr` again instead of a `SocketAddr`. We
6+
previously switched to `SocketAddr` after `IpAddr` had been deprecated, but
7+
it has since been re-added.

Cargo.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "maxminddb"
3-
version = "0.6.2"
3+
version = "0.7.0"
44
description = "Library for reading MaxMind DB format used by GeoIP2"
55
documentation = "http://oschwald.github.io/maxminddb-rust/maxminddb/struct.Reader.html"
66
readme = "README.md"
@@ -16,10 +16,5 @@ crate_type = ["lib", "dylib", "staticlib"]
1616
path = "src/maxminddb/lib.rs"
1717

1818
[dependencies]
19-
log = "0.3.5"
20-
rustc-serialize = "0.3.16"
21-
clippy = {version = "0.0.37", optional = true}
22-
23-
[features]
24-
default = []
25-
dev = ["clippy"]
19+
log = "0.3.6"
20+
rustc-serialize = "0.3.19"

example/lookup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extern crate maxminddb;
22
extern crate rustc_serialize;
33

4-
use std::net::SocketAddr;
4+
use std::net::IpAddr;
55
use std::str::FromStr;
66

77
use maxminddb::geoip2;
88

99
fn main() {
1010
let reader = maxminddb::Reader::open("/usr/local/share/GeoIP/GeoIP2-City.mmdb").unwrap();
11-
let ip: SocketAddr = FromStr::from_str("128.101.101.101:0").unwrap();
11+
let ip: IpAddr = FromStr::from_str("128.101.101.101").unwrap();
1212
let city: geoip2::City = reader.lookup(ip).unwrap();
1313
print!("{:?}\n", city);
1414
}

src/maxminddb/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
unstable_features,
77
unused_import_braces)]
88

9-
#![cfg_attr(feature = "dev", allow(unstable_features))]
10-
#![cfg_attr(feature = "dev", feature(plugin))]
11-
#![cfg_attr(feature = "dev", plugin(clippy))]
12-
139
#[macro_use]
1410
extern crate log;
1511

@@ -21,7 +17,7 @@ use std::io::prelude::*;
2117
use std::io;
2218
use std::error::Error;
2319
use std::mem;
24-
use std::net::SocketAddr;
20+
use std::net::IpAddr;
2521
use std::path::Path;
2622

2723
use rustc_serialize::Decodable;
@@ -387,23 +383,23 @@ impl Reader {
387383
///
388384
/// ```
389385
/// use maxminddb::geoip2;
390-
/// use std::net::SocketAddr;
386+
/// use std::net::IpAddr;
391387
/// use std::str::FromStr;
392388
///
393389
/// let reader = maxminddb::Reader::open("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
394390
///
395-
/// let ip: SocketAddr = FromStr::from_str("89.160.20.128:0").unwrap();
391+
/// let ip: IpAddr = FromStr::from_str("89.160.20.128").unwrap();
396392
/// let city: geoip2::City = reader.lookup(ip).unwrap();
397393
/// print!("{:?}", city);
398394
/// ```
399395
///
400396
/// Note that SocketAddr requires a port, which is not needed to look up
401397
/// the address in the database. This library will likely switch to IpAddr
402398
/// if the feature gate for that is removed.
403-
pub fn lookup<T: Decodable>(&self, address: SocketAddr) -> Result<T, MaxMindDBError> {
399+
pub fn lookup<T: Decodable>(&self, address: IpAddr) -> Result<T, MaxMindDBError> {
404400
let ip_bytes = ip_to_bytes(address);
405401
let pointer = try!(self.find_address_in_tree(ip_bytes));
406-
if pointer <= 0 {
402+
if pointer == 0 {
407403
return Err(MaxMindDBError::AddressNotFoundError("Address not found in database"
408404
.to_owned()));
409405
}
@@ -512,11 +508,11 @@ fn to_usize(base: u8, bytes: &[u8]) -> usize {
512508
bytes.iter().fold(base as usize, |acc, &b| (acc << 8) | b as usize)
513509
}
514510

515-
fn ip_to_bytes(address: SocketAddr) -> Vec<u8> {
511+
fn ip_to_bytes(address: IpAddr) -> Vec<u8> {
516512
match address {
517-
SocketAddr::V4(a) => a.ip().octets().to_vec(),
518-
SocketAddr::V6(a) => {
519-
let s = a.ip().segments();
513+
IpAddr::V4(a) => a.octets().to_vec(),
514+
IpAddr::V6(a) => {
515+
let s = a.segments();
520516
vec![(s[0] >> 8) as u8,
521517
s[0] as u8,
522518
(s[1] >> 8) as u8,

src/maxminddb/reader_test.rs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{MaxMindDBError, Reader};
22

33
use std::str::FromStr;
4-
use std::net::SocketAddr;
4+
use std::net::IpAddr;
55

66
#[test]
77
fn test_decoder() {
@@ -36,7 +36,7 @@ fn test_decoder() {
3636
}
3737

3838
let r = Reader::open("test-data/test-data/MaxMind-DB-test-decoder.mmdb").ok().unwrap();
39-
let ip: SocketAddr = FromStr::from_str("1.1.1.0:0").unwrap();
39+
let ip: IpAddr = FromStr::from_str("1.1.1.0").unwrap();
4040
let result: TestType = r.lookup(ip).unwrap();
4141

4242
assert_eq!(result.array, vec![1usize, 2usize, 3usize]);
@@ -68,7 +68,7 @@ fn test_broken_database() {
6868
let r = Reader::open("test-data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb")
6969
.ok()
7070
.unwrap();
71-
let ip: SocketAddr = FromStr::from_str("[2001:220::]:0").unwrap();
71+
let ip: IpAddr = FromStr::from_str("2001:220::").unwrap();
7272

7373
#[derive(RustcDecodable, Debug)]
7474
struct TestType;
@@ -148,51 +148,50 @@ fn check_metadata(reader: &Reader, ip_version: usize, record_size: usize) {
148148
fn check_ip(reader: &Reader, ip_version: usize) {
149149

150150
let subnets = match ip_version {
151-
6 => [["[::1:ffff:ffff]", "::1:ffff:ffff"],
152-
["[::2:0:0]", "::2:0:0"],
153-
["[::2:0:1]", "::2:0:0"],
154-
["[::2:0:33]", "::2:0:0"],
155-
["[::2:0:39]", "::2:0:0"],
156-
["[::2:0:40]", "::2:0:40"],
157-
["[::2:0:41]", "::2:0:40"],
158-
["[::2:0:49]", "::2:0:40"],
159-
["[::2:0:50]", "::2:0:50"],
160-
["[::2:0:52]", "::2:0:50"],
161-
["[::2:0:57]", "::2:0:50"],
162-
["[::2:0:58]", "::2:0:58"],
163-
["[::2:0:59]", "::2:0:58"]],
164-
_ => [["1.1.1.1", "1.1.1.1"],
165-
["1.1.1.2", "1.1.1.2"],
166-
["1.1.1.3", "1.1.1.2"],
167-
["1.1.1.4", "1.1.1.4"],
168-
["1.1.1.5", "1.1.1.4"],
169-
["1.1.1.6", "1.1.1.4"],
170-
["1.1.1.7", "1.1.1.4"],
171-
["1.1.1.8", "1.1.1.8"],
172-
["1.1.1.9", "1.1.1.8"],
173-
["1.1.1.15", "1.1.1.8"],
174-
["1.1.1.16", "1.1.1.16"],
175-
["1.1.1.17", "1.1.1.16"],
176-
["1.1.1.31", "1.1.1.16"]],
151+
6 => ["::1:ffff:ffff",
152+
"::2:0:0",
153+
"::2:0:0",
154+
"::2:0:0",
155+
"::2:0:0",
156+
"::2:0:40",
157+
"::2:0:40",
158+
"::2:0:40",
159+
"::2:0:50",
160+
"::2:0:50",
161+
"::2:0:50",
162+
"::2:0:58",
163+
"::2:0:58"],
164+
_ => ["1.1.1.1",
165+
"1.1.1.2",
166+
"1.1.1.2",
167+
"1.1.1.4",
168+
"1.1.1.4",
169+
"1.1.1.4",
170+
"1.1.1.4",
171+
"1.1.1.8",
172+
"1.1.1.8",
173+
"1.1.1.8",
174+
"1.1.1.16",
175+
"1.1.1.16",
176+
"1.1.1.16"],
177177
};
178178

179179
#[derive(RustcDecodable, Debug)]
180180
struct IpType {
181181
ip: String,
182182
}
183183

184-
for values in subnets.iter() {
185-
let addr = format!("{}:0", values[0]);
186-
let ip: SocketAddr = FromStr::from_str(&addr).unwrap();
184+
for subnet in subnets.iter() {
185+
let ip: IpAddr = FromStr::from_str(&subnet).unwrap();
187186
let value: IpType = reader.lookup(ip).unwrap();
188187

189-
assert_eq!(value.ip, values[1].to_string());
188+
assert_eq!(value.ip, subnet.to_string());
190189
}
191190

192-
let no_record = ["1.1.1.33:0", "255.254.253.123:0", "[89fa::]:0"];
191+
let no_record = ["1.1.1.33", "255.254.253.123", "89fa::"];
193192

194193
for &address in no_record.iter() {
195-
let ip: SocketAddr = FromStr::from_str(address).unwrap();
194+
let ip: IpAddr = FromStr::from_str(address).unwrap();
196195
match reader.lookup::<IpType>(ip) {
197196
Ok(v) => panic!("received an unexpected value: {:?}", v),
198197
Err(e) =>

0 commit comments

Comments
 (0)