Skip to content

Commit 269466d

Browse files
committed
Run recent rustfmt
1 parent f47ac38 commit 269466d

File tree

3 files changed

+88
-90
lines changed

3 files changed

+88
-90
lines changed

src/maxminddb/decoder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl rustc_serialize::Decoder for Decoder {
189189
json => return Err(DecodingError(format!("enum {:?}", json))),
190190
};
191191
let idx = match names.iter()
192-
.position(|n| *n == name) {
192+
.position(|n| *n == name) {
193193
Some(idx) => idx,
194194
None => return Err(DecodingError(name)),
195195
};
@@ -244,8 +244,10 @@ impl rustc_serialize::Decoder for Decoder {
244244
self.stack.push(Null);
245245
match f(self) {
246246
Ok(v) => v,
247-
Err(_) => return Err(DecodingError(format!("Unknown struct field {:?}",
248-
name.to_owned()))),
247+
Err(_) => {
248+
return Err(DecodingError(format!("Unknown struct field {:?}",
249+
name.to_owned())))
250+
}
249251
}
250252
}
251253
Some(record) => {

src/maxminddb/lib.rs

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub enum MaxMindDBError {
3232
}
3333

3434
impl From<io::Error> for MaxMindDBError {
35-
3635
fn from(err: io::Error) -> MaxMindDBError {
3736
// clean up and clean up MaxMindDBError generally
3837
MaxMindDBError::IoError(err.description().to_owned())
@@ -78,8 +77,7 @@ impl BinaryDecoder {
7877
fn decode_bool(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
7978
match size {
8079
0 | 1 => (Ok(decoder::DataRecord::Boolean(size != 0)), offset),
81-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))),
82-
0),
80+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))), 0),
8381
}
8482
}
8583

@@ -98,13 +96,12 @@ impl BinaryDecoder {
9896
let new_offset = offset + size;
9997

10098
let value = self.buf[offset..new_offset]
101-
.iter()
102-
.fold(0u32, |acc, &b| (acc << 8) | b as u32);
99+
.iter()
100+
.fold(0u32, |acc, &b| (acc << 8) | b as u32);
103101
let float_value: f32 = unsafe { mem::transmute(value) };
104102
(Ok(decoder::DataRecord::Float(float_value)), new_offset)
105103
}
106-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))),
107-
0),
104+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))), 0),
108105
}
109106
}
110107

@@ -114,13 +111,12 @@ impl BinaryDecoder {
114111
let new_offset = offset + size;
115112

116113
let value = self.buf[offset..new_offset]
117-
.iter()
118-
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
114+
.iter()
115+
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
119116
let float_value: f64 = unsafe { mem::transmute(value) };
120117
(Ok(decoder::DataRecord::Double(float_value)), new_offset)
121118
}
122-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("double of size {:?}", s))),
123-
0),
119+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("double of size {:?}", s))), 0),
124120
}
125121
}
126122

@@ -130,40 +126,39 @@ impl BinaryDecoder {
130126
let new_offset = offset + size;
131127

132128
let value = self.buf[offset..new_offset]
133-
.iter()
134-
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
129+
.iter()
130+
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
135131
(Ok(decoder::DataRecord::Uint64(value)), new_offset)
136132
}
137-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u64 of size {:?}", s))),
138-
0),
133+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u64 of size {:?}", s))), 0),
139134
}
140135
}
141136

142137
fn decode_uint32(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
143138
match size {
144139
s if s <= 4 => {
145140
match self.decode_uint64(size, offset) {
146-
(Ok(decoder::DataRecord::Uint64(u)), o) =>
147-
(Ok(decoder::DataRecord::Uint32(u as u32)), o),
141+
(Ok(decoder::DataRecord::Uint64(u)), o) => {
142+
(Ok(decoder::DataRecord::Uint32(u as u32)), o)
143+
}
148144
e => e,
149145
}
150146
}
151-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u32 of size {:?}", s))),
152-
0),
147+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u32 of size {:?}", s))), 0),
153148
}
154149
}
155150

156151
fn decode_uint16(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
157152
match size {
158153
s if s <= 4 => {
159154
match self.decode_uint64(size, offset) {
160-
(Ok(decoder::DataRecord::Uint64(u)), o) =>
161-
(Ok(decoder::DataRecord::Uint16(u as u16)), o),
155+
(Ok(decoder::DataRecord::Uint64(u)), o) => {
156+
(Ok(decoder::DataRecord::Uint16(u as u16)), o)
157+
}
162158
e => e,
163159
}
164160
}
165-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u16 of size {:?}", s))),
166-
0),
161+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u16 of size {:?}", s))), 0),
167162
}
168163
}
169164

@@ -173,12 +168,11 @@ impl BinaryDecoder {
173168
let new_offset = offset + size;
174169

175170
let value = self.buf[offset..new_offset]
176-
.iter()
177-
.fold(0i32, |acc, &b| (acc << 8) | b as i32);
171+
.iter()
172+
.fold(0i32, |acc, &b| (acc << 8) | b as i32);
178173
(Ok(decoder::DataRecord::Int32(value)), new_offset)
179174
}
180-
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("int32 of size {:?}", s))),
181-
0),
175+
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("int32 of size {:?}", s))), 0),
182176
}
183177
}
184178

@@ -199,10 +193,12 @@ impl BinaryDecoder {
199193

200194
let str_key = match key {
201195
decoder::DataRecord::String(s) => s,
202-
v => return (Err(MaxMindDBError::InvalidDatabaseError(format!("unexpected map \
203-
key type {:?}",
204-
v))),
205-
0),
196+
v => {
197+
return (Err(MaxMindDBError::InvalidDatabaseError(format!("unexpected map \
198+
key type {:?}",
199+
v))),
200+
0)
201+
}
206202
};
207203
values.insert(str_key, val);
208204
}
@@ -239,9 +235,10 @@ impl BinaryDecoder {
239235
let bytes = &self.buf[offset..new_offset];
240236
match from_utf8(bytes) {
241237
Ok(v) => (Ok(decoder::DataRecord::String(v.to_owned())), new_offset),
242-
Err(_) =>
238+
Err(_) => {
243239
(Err(MaxMindDBError::InvalidDatabaseError("error decoding string".to_owned())),
244-
new_offset),
240+
new_offset)
241+
}
245242
}
246243
}
247244

@@ -281,12 +278,8 @@ impl BinaryDecoder {
281278
size = match size {
282279
s if s < 29 => s,
283280
29 => 29usize + size_bytes[0] as usize,
284-
30 => {
285-
285usize + to_usize(0, size_bytes)
286-
}
287-
_ => {
288-
65821usize + to_usize(0, size_bytes)
289-
}
281+
30 => 285usize + to_usize(0, size_bytes),
282+
_ => 65821usize + to_usize(0, size_bytes),
290283
};
291284
(size, new_offset)
292285
}
@@ -311,8 +304,10 @@ impl BinaryDecoder {
311304
11 => self.decode_array(size, offset),
312305
14 => self.decode_bool(size, offset),
313306
15 => self.decode_float(size, offset),
314-
u => (Err(MaxMindDBError::InvalidDatabaseError(format!("Unknown data type: {:?}", u))),
315-
offset),
307+
u => {
308+
(Err(MaxMindDBError::InvalidDatabaseError(format!("Unknown data type: {:?}", u))),
309+
offset)
310+
}
316311
}
317312
}
318313
}
@@ -326,7 +321,6 @@ pub struct Reader {
326321
}
327322

328323
impl Reader {
329-
330324
/// Open a MaxMind DB database file.
331325
///
332326
/// # Example
@@ -352,9 +346,11 @@ impl Reader {
352346

353347
let raw_metadata = match metadata_decoder.decode(metadata_start) {
354348
(Ok(m), _) => m,
355-
m => return Err(MaxMindDBError::InvalidDatabaseError(format!("metadata of wrong \
356-
type: {:?}",
357-
m))),
349+
m => {
350+
return Err(MaxMindDBError::InvalidDatabaseError(format!("metadata of wrong \
351+
type: {:?}",
352+
m)))
353+
}
358354
};
359355

360356
let mut type_decoder = decoder::Decoder::new(raw_metadata);
@@ -401,7 +397,7 @@ impl Reader {
401397
let pointer = try!(self.find_address_in_tree(ip_bytes));
402398
if pointer == 0 {
403399
return Err(MaxMindDBError::AddressNotFoundError("Address not found in database"
404-
.to_owned()));
400+
.to_owned()));
405401
}
406402
let rec = try!(self.resolve_data_pointer(pointer));
407403
let mut decoder = decoder::Decoder::new(rec);
@@ -479,9 +475,11 @@ impl Reader {
479475
let offset = base_offset + index * 4;
480476
to_usize(0, &self.decoder.buf[offset..offset + 4])
481477
}
482-
s => return Err(MaxMindDBError::InvalidDatabaseError(format!("unknown record size: \
483-
{:?}",
484-
s))),
478+
s => {
479+
return Err(MaxMindDBError::InvalidDatabaseError(format!("unknown record size: \
480+
{:?}",
481+
s)))
482+
}
485483
};
486484
Ok(val)
487485
}
@@ -494,7 +492,7 @@ impl Reader {
494492
if resolved > self.decoder.buf.len() {
495493
return Err(MaxMindDBError::InvalidDatabaseError("the MaxMind DB file's search tree \
496494
is corrupt"
497-
.to_owned()));
495+
.to_owned()));
498496
}
499497

500498
let (record, _) = self.decoder.decode(resolved);
@@ -555,7 +553,7 @@ fn find_metadata_start(buf: &[u8]) -> Result<usize, MaxMindDBError> {
555553
}
556554
}
557555
Err(MaxMindDBError::InvalidDatabaseError("Could not find MaxMind DB metadata in file."
558-
.to_owned()))
556+
.to_owned()))
559557
}
560558

561559
mod decoder;

src/maxminddb/reader_test.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ fn test_decoder() {
6666
#[test]
6767
fn test_broken_database() {
6868
let r = Reader::open("test-data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb")
69-
.ok()
70-
.unwrap();
69+
.ok()
70+
.unwrap();
7171
let ip: IpAddr = FromStr::from_str("2001:220::").unwrap();
7272

7373
#[derive(RustcDecodable, Debug)]
7474
struct TestType;
7575
match r.lookup::<TestType>(ip) {
76-
Err(e) => assert_eq!(e,
77-
MaxMindDBError::InvalidDatabaseError("double of size 2".to_string())),
76+
Err(e) => {
77+
assert_eq!(e,
78+
MaxMindDBError::InvalidDatabaseError("double of size 2".to_string()))
79+
}
7880
Ok(_) => panic!("Error expected"),
7981
}
8082
}
@@ -94,10 +96,12 @@ fn test_non_database() {
9496
let r = Reader::open("README.md");
9597
match r {
9698
Ok(_) => panic!("Received Reader when opening a non-MMDB file"),
97-
Err(e) => assert_eq!(e,
98-
MaxMindDBError::InvalidDatabaseError("Could not find MaxMind DB \
99-
metadata in file."
100-
.to_string())),
99+
Err(e) => {
100+
assert_eq!(e,
101+
MaxMindDBError::InvalidDatabaseError("Could not find MaxMind DB metadata \
102+
in file."
103+
.to_string()))
104+
}
101105

102106
}
103107
}
@@ -148,32 +152,25 @@ fn check_metadata(reader: &Reader, ip_version: usize, record_size: usize) {
148152
fn check_ip(reader: &Reader, ip_version: usize) {
149153

150154
let subnets = match ip_version {
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"],
155+
6 => {
156+
["::1:ffff:ffff",
157+
"::2:0:0",
158+
"::2:0:0",
159+
"::2:0:0",
160+
"::2:0:0",
161+
"::2:0:40",
162+
"::2:0:40",
163+
"::2:0:40",
164+
"::2:0:50",
165+
"::2:0:50",
166+
"::2:0:50",
167+
"::2:0:58",
168+
"::2:0:58"]
169+
}
170+
_ => {
171+
["1.1.1.1", "1.1.1.2", "1.1.1.2", "1.1.1.4", "1.1.1.4", "1.1.1.4", "1.1.1.4",
172+
"1.1.1.8", "1.1.1.8", "1.1.1.8", "1.1.1.16", "1.1.1.16", "1.1.1.16"]
173+
}
177174
};
178175

179176
#[derive(RustcDecodable, Debug)]
@@ -194,10 +191,11 @@ fn check_ip(reader: &Reader, ip_version: usize) {
194191
let ip: IpAddr = FromStr::from_str(address).unwrap();
195192
match reader.lookup::<IpType>(ip) {
196193
Ok(v) => panic!("received an unexpected value: {:?}", v),
197-
Err(e) =>
194+
Err(e) => {
198195
assert_eq!(e,
199196
MaxMindDBError::AddressNotFoundError("Address not found in database"
200-
.to_string())),
197+
.to_string()))
198+
}
201199
}
202200
}
203201
}

0 commit comments

Comments
 (0)