Skip to content

Commit a1a923a

Browse files
committed
Fix visibility of some internal structs and types
1 parent 5419089 commit a1a923a

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

src/maxminddb/decoder.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11

22
extern crate rustc_serialize;
33

4+
use std::collections::BTreeMap;
45
use std::string;
56

6-
use super::{DataRecord, MaxMindDBError};
7-
use super::DataRecord::{Array, Boolean, Byte, Double, Float, Int32, Map, Null, String, Uint16,
8-
Uint32, Uint64};
7+
use super::MaxMindDBError;
98
use super::MaxMindDBError::DecodingError;
109

10+
pub type DbArray = Vec<DataRecord>;
11+
pub type DbMap = BTreeMap<string::String, DataRecord>;
12+
13+
#[derive(Clone, Debug, PartialEq)]
14+
pub enum DataRecord {
15+
String(string::String),
16+
Double(f64),
17+
Byte(u8),
18+
Uint16(u16),
19+
Uint32(u32),
20+
Map(Box<DbMap>),
21+
Int32(i32),
22+
Uint64(u64),
23+
Boolean(bool),
24+
Array(DbArray),
25+
Float(f32),
26+
Null,
27+
}
28+
29+
use self::DataRecord::{Array, Boolean, Byte, Double, Float, Int32, Map, Null, String, Uint16,
30+
Uint32, Uint64};
31+
32+
1133
macro_rules! expect(
1234
($e:expr, Null) => ({
1335
match $e {
@@ -26,7 +48,7 @@ macro_rules! expect(
2648

2749
#[derive(Debug)]
2850
pub struct Decoder {
29-
pub stack: Vec<DataRecord>,
51+
stack: Vec<DataRecord>,
3052
}
3153

3254
impl Decoder {

src/maxminddb/lib.rs

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,6 @@ impl From<io::Error> for MaxMindDBError {
4545

4646
type BinaryDecodeResult<T> = (Result<T, MaxMindDBError>, usize);
4747

48-
#[derive(Clone, Debug, PartialEq)]
49-
pub enum DataRecord {
50-
String(String),
51-
Double(f64),
52-
Byte(u8),
53-
Uint16(u16),
54-
Uint32(u32),
55-
Map(Box<DbMap>),
56-
Int32(i32),
57-
Uint64(u64),
58-
Boolean(bool),
59-
Array(DbArray),
60-
Float(f32),
61-
Null,
62-
}
63-
64-
pub type DbArray = Vec<DataRecord>;
65-
pub type DbMap = BTreeMap<String, DataRecord>;
66-
6748
#[derive(RustcDecodable, Debug)]
6849
pub struct Metadata {
6950
pub binary_format_major_version: u16,
@@ -83,7 +64,7 @@ struct BinaryDecoder {
8364
}
8465

8566
impl BinaryDecoder {
86-
fn decode_array(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
67+
fn decode_array(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
8768
let mut array = Vec::new();
8869
let mut new_offset = offset;
8970

@@ -95,27 +76,27 @@ impl BinaryDecoder {
9576
new_offset = tmp_offset;
9677
array.push(val);
9778
}
98-
(Ok(DataRecord::Array(array)), new_offset)
79+
(Ok(decoder::DataRecord::Array(array)), new_offset)
9980
}
10081

101-
fn decode_bool(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
82+
fn decode_bool(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
10283
match size {
103-
0 | 1 => (Ok(DataRecord::Boolean(size != 0)), offset),
84+
0 | 1 => (Ok(decoder::DataRecord::Boolean(size != 0)), offset),
10485
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))),
10586
0),
10687
}
10788
}
10889

109-
fn decode_bytes(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
90+
fn decode_bytes(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
11091
let new_offset = offset + size;
11192
let u8_slice = &self.buf[offset..new_offset];
11293

113-
let bytes = u8_slice.iter().map(|&b| DataRecord::Byte(b)).collect();
94+
let bytes = u8_slice.iter().map(|&b| decoder::DataRecord::Byte(b)).collect();
11495

115-
(Ok(DataRecord::Array(bytes)), new_offset)
96+
(Ok(decoder::DataRecord::Array(bytes)), new_offset)
11697
}
11798

118-
fn decode_float(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
99+
fn decode_float(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
119100
match size {
120101
4 => {
121102
let new_offset = offset + size;
@@ -124,14 +105,14 @@ impl BinaryDecoder {
124105
.iter()
125106
.fold(0u32, |acc, &b| (acc << 8) | b as u32);
126107
let float_value: f32 = unsafe { mem::transmute(value) };
127-
(Ok(DataRecord::Float(float_value)), new_offset)
108+
(Ok(decoder::DataRecord::Float(float_value)), new_offset)
128109
}
129110
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("float of size {:?}", s))),
130111
0),
131112
}
132113
}
133114

134-
fn decode_double(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
115+
fn decode_double(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
135116
match size {
136117
8 => {
137118
let new_offset = offset + size;
@@ -140,33 +121,34 @@ impl BinaryDecoder {
140121
.iter()
141122
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
142123
let float_value: f64 = unsafe { mem::transmute(value) };
143-
(Ok(DataRecord::Double(float_value)), new_offset)
124+
(Ok(decoder::DataRecord::Double(float_value)), new_offset)
144125
}
145126
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("double of size {:?}", s))),
146127
0),
147128
}
148129
}
149130

150-
fn decode_uint64(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
131+
fn decode_uint64(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
151132
match size {
152133
s if s <= 8 => {
153134
let new_offset = offset + size;
154135

155136
let value = self.buf[offset..new_offset]
156137
.iter()
157138
.fold(0u64, |acc, &b| (acc << 8) | b as u64);
158-
(Ok(DataRecord::Uint64(value)), new_offset)
139+
(Ok(decoder::DataRecord::Uint64(value)), new_offset)
159140
}
160141
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("u64 of size {:?}", s))),
161142
0),
162143
}
163144
}
164145

165-
fn decode_uint32(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
146+
fn decode_uint32(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
166147
match size {
167148
s if s <= 4 => {
168149
match self.decode_uint64(size, offset) {
169-
(Ok(DataRecord::Uint64(u)), o) => (Ok(DataRecord::Uint32(u as u32)), o),
150+
(Ok(decoder::DataRecord::Uint64(u)), o) =>
151+
(Ok(decoder::DataRecord::Uint32(u as u32)), o),
170152
e => e,
171153
}
172154
}
@@ -175,11 +157,12 @@ impl BinaryDecoder {
175157
}
176158
}
177159

178-
fn decode_uint16(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
160+
fn decode_uint16(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
179161
match size {
180162
s if s <= 4 => {
181163
match self.decode_uint64(size, offset) {
182-
(Ok(DataRecord::Uint64(u)), o) => (Ok(DataRecord::Uint16(u as u16)), o),
164+
(Ok(decoder::DataRecord::Uint64(u)), o) =>
165+
(Ok(decoder::DataRecord::Uint16(u as u16)), o),
183166
e => e,
184167
}
185168
}
@@ -188,22 +171,22 @@ impl BinaryDecoder {
188171
}
189172
}
190173

191-
fn decode_int(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
174+
fn decode_int(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
192175
match size {
193176
s if s <= 4 => {
194177
let new_offset = offset + size;
195178

196179
let value = self.buf[offset..new_offset]
197180
.iter()
198181
.fold(0i32, |acc, &b| (acc << 8) | b as i32);
199-
(Ok(DataRecord::Int32(value)), new_offset)
182+
(Ok(decoder::DataRecord::Int32(value)), new_offset)
200183
}
201184
s => (Err(MaxMindDBError::InvalidDatabaseError(format!("int32 of size {:?}", s))),
202185
0),
203186
}
204187
}
205188

206-
fn decode_map(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
189+
fn decode_map(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
207190
let mut values = Box::new(BTreeMap::new());
208191
let mut new_offset = offset;
209192

@@ -219,19 +202,22 @@ impl BinaryDecoder {
219202
new_offset = tmp_offset;
220203

221204
let str_key = match key {
222-
DataRecord::String(s) => s,
205+
decoder::DataRecord::String(s) => s,
223206
v => return (Err(MaxMindDBError::InvalidDatabaseError(format!("unexpected map \
224207
key type {:?}",
225208
v))),
226209
0),
227210
};
228211
values.insert(str_key, val);
229212
}
230-
(Ok(DataRecord::Map(values)), new_offset)
213+
(Ok(decoder::DataRecord::Map(values)), new_offset)
231214
}
232215

233216

234-
fn decode_pointer(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
217+
fn decode_pointer(&self,
218+
size: usize,
219+
offset: usize)
220+
-> BinaryDecodeResult<decoder::DataRecord> {
235221
let pointer_value_offset = [0, 0, 2048, 526336, 0];
236222
let pointer_size = ((size >> 3) & 0x3) + 1;
237223
let new_offset = offset + pointer_size;
@@ -250,20 +236,20 @@ impl BinaryDecoder {
250236
(result, new_offset)
251237
}
252238

253-
fn decode_string(&self, size: usize, offset: usize) -> BinaryDecodeResult<DataRecord> {
239+
fn decode_string(&self, size: usize, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
254240
use std::str::from_utf8;
255241

256242
let new_offset: usize = offset + size;
257243
let bytes = &self.buf[offset..new_offset];
258244
match from_utf8(bytes) {
259-
Ok(v) => (Ok(DataRecord::String(v.to_owned())), new_offset),
245+
Ok(v) => (Ok(decoder::DataRecord::String(v.to_owned())), new_offset),
260246
Err(_) =>
261247
(Err(MaxMindDBError::InvalidDatabaseError("error decoding string".to_owned())),
262248
new_offset),
263249
}
264250
}
265251

266-
fn decode(&self, offset: usize) -> BinaryDecodeResult<DataRecord> {
252+
fn decode(&self, offset: usize) -> BinaryDecodeResult<decoder::DataRecord> {
267253
let mut new_offset = offset + 1;
268254
let ctrl_byte = self.buf[offset];
269255

@@ -313,7 +299,7 @@ impl BinaryDecoder {
313299
data_type: u8,
314300
size: usize,
315301
offset: usize)
316-
-> BinaryDecodeResult<DataRecord> {
302+
-> BinaryDecodeResult<decoder::DataRecord> {
317303
match data_type {
318304
1 => self.decode_pointer(size, offset),
319305
2 => self.decode_string(size, offset),
@@ -504,7 +490,7 @@ impl Reader {
504490
Ok(val)
505491
}
506492

507-
fn resolve_data_pointer(&self, pointer: usize) -> Result<DataRecord, MaxMindDBError> {
493+
fn resolve_data_pointer(&self, pointer: usize) -> Result<decoder::DataRecord, MaxMindDBError> {
508494
let search_tree_size = (self.metadata.record_size as usize) * self.metadata.node_count / 4;
509495

510496
let resolved = pointer - self.metadata.node_count + search_tree_size;

0 commit comments

Comments
 (0)