Skip to content

Commit 2d8af90

Browse files
rossoschwald
authored andcommitted
Add example within doc/usage
1 parent 36b13b7 commit 2d8af90

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

examples/within.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ipnetwork::IpNetwork;
2-
use maxminddb::{Within, geoip2};
2+
use maxminddb::{geoip2, Within};
33

44
fn main() -> Result<(), String> {
55
let mut args = std::env::args().skip(1);

src/maxminddb/lib.rs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,6 @@ pub struct Metadata {
7676
pub record_size: u16,
7777
}
7878

79-
/// A reader for the MaxMind DB format. The lifetime `'data` is tied to the lifetime of the underlying buffer holding the contents of the database file.
80-
#[derive(Debug)]
81-
pub struct Reader<S: AsRef<[u8]>> {
82-
buf: S,
83-
pub metadata: Metadata,
84-
ipv4_start: usize,
85-
pointer_base: usize,
86-
}
87-
88-
#[cfg(feature = "mmap")]
89-
impl<'de> Reader<Mmap> {
90-
/// Open a MaxMind DB database file by memory mapping it.
91-
///
92-
/// # Example
93-
///
94-
/// ```
95-
/// let reader = maxminddb::Reader::open_mmap("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
96-
/// ```
97-
pub fn open_mmap<P: AsRef<Path>>(database: P) -> Result<Reader<Mmap>, MaxMindDBError> {
98-
let file_read = File::open(database)?;
99-
let mmap = unsafe { MmapOptions::new().map(&file_read) }?;
100-
Reader::from_source(mmap)
101-
}
102-
}
103-
104-
impl<'de> Reader<Vec<u8>> {
105-
/// Open a MaxMind DB database file by loading it into memory.
106-
///
107-
/// # Example
108-
///
109-
/// ```
110-
/// let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
111-
/// ```
112-
pub fn open_readfile<P: AsRef<Path>>(database: P) -> Result<Reader<Vec<u8>>, MaxMindDBError> {
113-
use std::fs;
114-
115-
let buf: Vec<u8> = fs::read(&database)?;
116-
Reader::from_source(buf)
117-
}
118-
}
119-
12079
#[derive(Debug)]
12180
struct WithinNode {
12281
node: usize,
@@ -201,6 +160,47 @@ impl<'de, T: Deserialize<'de>, S: AsRef<[u8]>> Iterator for Within<'de, T, S> {
201160
}
202161
}
203162

163+
/// A reader for the MaxMind DB format. The lifetime `'data` is tied to the lifetime of the underlying buffer holding the contents of the database file.
164+
#[derive(Debug)]
165+
pub struct Reader<S: AsRef<[u8]>> {
166+
buf: S,
167+
pub metadata: Metadata,
168+
ipv4_start: usize,
169+
pointer_base: usize,
170+
}
171+
172+
#[cfg(feature = "mmap")]
173+
impl<'de> Reader<Mmap> {
174+
/// Open a MaxMind DB database file by memory mapping it.
175+
///
176+
/// # Example
177+
///
178+
/// ```
179+
/// let reader = maxminddb::Reader::open_mmap("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
180+
/// ```
181+
pub fn open_mmap<P: AsRef<Path>>(database: P) -> Result<Reader<Mmap>, MaxMindDBError> {
182+
let file_read = File::open(database)?;
183+
let mmap = unsafe { MmapOptions::new().map(&file_read) }?;
184+
Reader::from_source(mmap)
185+
}
186+
}
187+
188+
impl<'de> Reader<Vec<u8>> {
189+
/// Open a MaxMind DB database file by loading it into memory.
190+
///
191+
/// # Example
192+
///
193+
/// ```
194+
/// let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
195+
/// ```
196+
pub fn open_readfile<P: AsRef<Path>>(database: P) -> Result<Reader<Vec<u8>>, MaxMindDBError> {
197+
use std::fs;
198+
199+
let buf: Vec<u8> = fs::read(&database)?;
200+
Reader::from_source(buf)
201+
}
202+
}
203+
204204
impl<'de, S: AsRef<[u8]>> Reader<S> {
205205
/// Open a MaxMind DB database from anything that implements AsRef<[u8]>
206206
///
@@ -263,6 +263,21 @@ impl<'de, S: AsRef<[u8]>> Reader<S> {
263263
T::deserialize(&mut decoder)
264264
}
265265

266+
/// Iterate over blocks of IP networks in the opened MaxMind DB
267+
///
268+
/// Example:
269+
///
270+
/// ```
271+
/// use ipnetwork::IpNetwork;
272+
/// use maxminddb::{geoip2, Within};
273+
///
274+
/// let ip_net = IpNetwork::V6("::/0");
275+
/// let mut iter: Within<geoip2::City, _> = reader.within(ip_net).map_err(|e| e.to_string())?;
276+
/// while let Some(next) = iter.next() {
277+
/// let item = next.map_err(|e| e.to_string())?;
278+
/// println!("ip_net={}, city={:?}", item.ip_net, city)
279+
/// }
280+
/// ```
266281
pub fn within<T>(&'de self, cidr: IpNetwork) -> Result<Within<T, S>, MaxMindDBError>
267282
where
268283
T: Deserialize<'de>,

0 commit comments

Comments
 (0)