Skip to content

Commit 35cb39d

Browse files
committed
Misc. cleanup
1. Replace to_string with to_owned 2. Removed some casts that were no longer necessary. 3. Removed unused import and unnecessary `string::` usage. 4. Tidy the code with the lastest rustfmt. 5. Set up travis-cargo to autogenerate docs, generate covarage reports, etc.
1 parent 3ee461f commit 35cb39d

File tree

7 files changed

+328
-227
lines changed

7 files changed

+328
-227
lines changed

.travis.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: rust
22

3+
sudo: false
4+
35
rust:
46
- stable
57
- beta
@@ -9,5 +11,32 @@ matrix:
911
allow_failure:
1012
- rust: nightly
1113

14+
before_script:
15+
- |
16+
pip install 'travis-cargo<0.2' --user &&
17+
export PATH=$HOME/.local/bin:$PATH
18+
1219
script:
13-
- make all check
20+
- |
21+
travis-cargo build &&
22+
travis-cargo test &&
23+
travis-cargo bench &&
24+
travis-cargo --only stable doc
25+
26+
addons:
27+
apt:
28+
packages:
29+
- libcurl4-openssl-dev
30+
- libelf-dev
31+
- libdw-dev
32+
33+
after_success:
34+
- travis-cargo --only stable doc-upload
35+
- travis-cargo coveralls --no-sudo
36+
37+
env:
38+
global:
39+
- TRAVIS_CARGO_NIGHTLY_FEATURE=dev
40+
secure: ! 'TjsN3dER6tIXXsWu8/CWu+wJ6U+vLchsRHKhoRxmvxIxscDsU2oHTgDqTQ4P
41+
6v2alyKkzj4OMguc0Jjrx46BPTgDvjokpiKFy96NVkBHVm0IDHcJUjcBIkcv
42+
NsG0KS9ONUriRGadMTAlGZQk3zs8mmWHcyd+J6xNvuH7QQEtHDc='

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "maxminddb"
4-
version = "0.5.5"
4+
version = "0.6.0"
55
description = "Library for reading MaxMind DB format used by GeoIP2"
66
documentation = "http://oschwald.github.io/maxminddb-rust/maxminddb/struct.Reader.html"
77
readme = "README.md"
@@ -21,3 +21,8 @@ path = "src/maxminddb/lib.rs"
2121

2222
log = "0.3.2"
2323
rustc-serialize = "0.3.16"
24+
clippy = {version = "0.0.21", optional = true}
25+
26+
[features]
27+
default = []
28+
dev = ["clippy"]

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ target/lookup: example/lookup.rs $(libmaxminddb_so)
1919
examples: target/lookup
2020

2121
.PHONY: check
22-
check: $(libmaxminddb_so)
22+
check:
23+
$(CARGO) build --features "dev"
2324
$(CARGO) test
2425

2526
.PHONY: clean

src/maxminddb/decoder.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ extern crate rustc_serialize;
44
use std::string;
55

66
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};
99
use super::MaxMindDBError::DecodingError;
1010

1111
macro_rules! expect(
@@ -18,21 +18,21 @@ macro_rules! expect(
1818
($e:expr, $t:ident) => ({
1919
match $e {
2020
$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))))
2223
}
2324
})
2425
);
2526

27+
#[derive(Debug)]
2628
pub struct Decoder {
2729
pub stack: Vec<DataRecord>,
2830
}
2931

3032
impl Decoder {
3133
/// Creates a new decoder instance for decoding the specified JSON value.
3234
pub fn new(record: DataRecord) -> Decoder {
33-
Decoder {
34-
stack: vec!(record),
35-
}
35+
Decoder { stack: vec![record] }
3636
}
3737
}
3838

@@ -90,12 +90,12 @@ impl rustc_serialize::Decoder for Decoder {
9090

9191
fn read_i16(&mut self) -> DecodeResult<i16> {
9292
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()))
9494
}
9595

9696
fn read_i8(&mut self) -> DecodeResult<i8> {
9797
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()))
9999
}
100100

101101
fn read_isize(&mut self) -> DecodeResult<isize> {
@@ -120,23 +120,20 @@ impl rustc_serialize::Decoder for Decoder {
120120

121121
fn read_char(&mut self) -> DecodeResult<char> {
122122
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)))
130128
}
131-
Err(DecodingError(format!("char {:?}", s)))
132129
}
133130

134131
fn read_str(&mut self) -> DecodeResult<string::String> {
135132
debug!("read_str");
136133
Ok(try!(expect!(self.pop(), String)))
137134
}
138135

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>
140137
where F: FnOnce(&mut Decoder) -> DecodeResult<T>
141138
{
142139
debug!("read_enum({:?})", name);
@@ -151,30 +148,28 @@ impl rustc_serialize::Decoder for Decoder {
151148
let name = match self.pop() {
152149
String(s) => s,
153150
Map(mut o) => {
154-
let n = match o.remove(&"variant".to_string()) {
151+
let n = match o.remove(&"variant".to_owned()) {
155152
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())),
158155
};
159-
match o.remove(&"fields".to_string()) {
156+
match o.remove(&"fields".to_owned()) {
160157
Some(Array(l)) => {
161158
for field in l.into_iter().rev() {
162159
self.stack.push(field.clone());
163160
}
164-
},
161+
}
165162
Some(val) => return Err(DecodingError(format!("enum {:?}", val))),
166-
None => return Err(DecodingError("fields".to_string()))
163+
None => return Err(DecodingError("fields".to_owned())),
167164
}
168165
n
169166
}
170-
json => return Err(DecodingError( format!("enum {:?}", json)))
167+
json => return Err(DecodingError(format!("enum {:?}", json))),
171168
};
172169
let idx = match names.iter()
173-
.position(|n| {
174-
*n == name
175-
}) {
170+
.position(|n| *n == name) {
176171
Some(idx) => idx,
177-
None => return Err(DecodingError(name))
172+
None => return Err(DecodingError(name)),
178173
};
179174
f(self, idx)
180175
}
@@ -201,7 +196,9 @@ impl rustc_serialize::Decoder for Decoder {
201196
-> DecodeResult<T>
202197
where F: FnOnce(&mut Decoder) -> DecodeResult<T>
203198
{
204-
debug!("read_enum_struct_variant_field(name={:?}, idx={:?})", name, idx);
199+
debug!("read_enum_struct_variant_field(name={:?}, idx={:?})",
200+
name,
201+
idx);
205202
self.read_enum_variant_arg(idx, f)
206203
}
207204

@@ -220,14 +217,15 @@ impl rustc_serialize::Decoder for Decoder {
220217
debug!("read_struct_field(name={:?}, idx={:?})", name, idx);
221218
let mut obj = try!(expect!(self.pop(), Map));
222219

223-
let value = match obj.remove(&name.to_string()) {
220+
let value = match obj.remove(&name.to_owned()) {
224221
None => {
225222
self.stack.push(Null);
226223
match f(self) {
227224
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()))),
229227
}
230-
},
228+
}
231229
Some(record) => {
232230
self.stack.push(record);
233231
try!(f(self))
@@ -277,7 +275,10 @@ impl rustc_serialize::Decoder for Decoder {
277275
debug!("read_option()");
278276
match self.pop() {
279277
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+
}
281282
}
282283
}
283284

@@ -328,6 +329,6 @@ impl rustc_serialize::Decoder for Decoder {
328329
}
329330

330331
fn error(&mut self, err: &str) -> MaxMindDBError {
331-
DecodingError(err.to_string())
332+
DecodingError(err.to_owned())
332333
}
333334
}

src/maxminddb/geoip2.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate rustc_serialize;
33

44
/// GeoIP2 Country record
5-
#[derive(RustcDecodable, Debug)]
5+
#[derive(RustcDecodable, Clone, Debug)]
66
pub struct Country {
77
pub continent: Option<model::Continent>,
88
pub country: Option<model::Country>,
@@ -12,7 +12,7 @@ pub struct Country {
1212
}
1313

1414
/// GeoIP2 City record
15-
#[derive(RustcDecodable, Debug)]
15+
#[derive(RustcDecodable, Clone, Debug)]
1616
pub struct City {
1717
pub city: Option<model::City>,
1818
pub continent: Option<model::Continent>,
@@ -26,7 +26,7 @@ pub struct City {
2626
}
2727

2828
/// GeoIP2 ISP record
29-
#[derive(RustcDecodable, Debug)]
29+
#[derive(RustcDecodable, Clone, Debug)]
3030
pub struct Isp {
3131
pub autonomous_system_number: Option<u32>,
3232
pub autonomous_system_organization: Option<String>,
@@ -35,70 +35,69 @@ pub struct Isp {
3535
}
3636

3737
/// GeoIP2 Connection-Type record
38-
#[derive(RustcDecodable, Debug)]
38+
#[derive(RustcDecodable, Clone, Debug)]
3939
pub struct ConnectionType {
4040
pub connection_type: Option<String>,
4141
}
4242

4343
/// GeoIP2 Anonymous Ip record
44-
#[derive(RustcDecodable, Debug)]
44+
#[derive(RustcDecodable, Clone, Debug)]
4545
pub struct AnonymousIp {
4646
pub is_anonymous: Option<bool>,
47-
pub is_public_proxy: Option<bool>
47+
pub is_public_proxy: Option<bool>,
4848
}
4949

5050
pub mod model {
5151
use std::collections::BTreeMap;
5252

53-
#[derive(RustcDecodable, Debug)]
53+
#[derive(RustcDecodable, Clone, Debug)]
5454
pub struct City {
5555
pub geoname_id: Option<u32>,
5656
pub names: Option<BTreeMap<String, String>>,
5757
}
5858

59-
#[derive(RustcDecodable, Debug)]
59+
#[derive(RustcDecodable, Clone, Debug)]
6060
pub struct Continent {
6161
pub code: Option<String>,
6262
pub geoname_id: Option<u32>,
6363
pub names: Option<BTreeMap<String, String>>,
6464
}
6565

66-
#[derive(RustcDecodable, Debug)]
66+
#[derive(RustcDecodable, Clone, Debug)]
6767
pub struct Country {
6868
pub geoname_id: Option<u32>,
6969
pub iso_code: Option<String>,
7070
pub names: Option<BTreeMap<String, String>>,
7171
}
7272

73-
#[derive(RustcDecodable, Debug)]
73+
#[derive(RustcDecodable, Clone, Debug)]
7474
pub struct Location {
7575
pub latitude: Option<f64>,
7676
pub longitude: Option<f64>,
7777
pub metro_code: Option<u16>,
7878
pub time_zone: Option<String>,
7979
}
8080

81-
#[derive(RustcDecodable, Debug)]
81+
#[derive(RustcDecodable, Clone, Debug)]
8282
pub struct Postal {
8383
pub code: Option<String>,
8484
}
8585

86-
#[derive(RustcDecodable, Debug)]
86+
#[derive(RustcDecodable, Clone, Debug)]
8787
pub struct RepresentedCountry {
8888
pub geoname_id: Option<u32>,
8989
pub iso_code: Option<String>,
90-
pub names: Option<BTreeMap<String, String>>,
91-
// pub type: Option<String>,
90+
pub names: Option<BTreeMap<String, String>>, // pub type: Option<String>,
9291
}
9392

94-
#[derive(RustcDecodable, Debug)]
93+
#[derive(RustcDecodable, Clone, Debug)]
9594
pub struct Subdivision {
9695
pub geoname_id: Option<u32>,
9796
pub iso_code: Option<String>,
9897
pub names: Option<BTreeMap<String, String>>,
9998
}
10099

101-
#[derive(RustcDecodable, Debug)]
100+
#[derive(RustcDecodable, Clone, Debug)]
102101
pub struct Traits {
103102
pub is_anonymous_proxy: Option<bool>,
104103
pub is_satellite_provider: Option<bool>,

0 commit comments

Comments
 (0)