pub struct Reader<S: AsRef<[u8]>> {
pub metadata: Metadata,
/* private fields */
}
Expand description
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.
Fields§
§metadata: Metadata
Implementations§
Source§impl Reader<Vec<u8>>
impl Reader<Vec<u8>>
Sourcepub fn open_readfile<P: AsRef<Path>>(
database: P,
) -> Result<Reader<Vec<u8>>, MaxMindDbError>
pub fn open_readfile<P: AsRef<Path>>( database: P, ) -> Result<Reader<Vec<u8>>, MaxMindDbError>
Open a MaxMind DB database file by loading it into memory.
§Example
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
Source§impl<'de, S: AsRef<[u8]>> Reader<S>
impl<'de, S: AsRef<[u8]>> Reader<S>
Sourcepub fn from_source(buf: S) -> Result<Reader<S>, MaxMindDbError>
pub fn from_source(buf: S) -> Result<Reader<S>, MaxMindDbError>
Sourcepub fn lookup<T>(
&'de self,
address: IpAddr,
) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'de>,
pub fn lookup<T>(
&'de self,
address: IpAddr,
) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'de>,
Lookup the socket address in the opened MaxMind DB.
Returns Ok(None)
if the address is not found in the database.
Example:
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb")?;
let ip: IpAddr = FromStr::from_str("89.160.20.128").unwrap();
if let Some(city) = reader.lookup::<geoip2::City>(ip)? {
println!("{:?}", city);
} else {
println!("Address not found");
}
Sourcepub fn lookup_prefix<T>(
&'de self,
address: IpAddr,
) -> Result<(Option<T>, usize), MaxMindDbError>where
T: Deserialize<'de>,
pub fn lookup_prefix<T>(
&'de self,
address: IpAddr,
) -> Result<(Option<T>, usize), MaxMindDbError>where
T: Deserialize<'de>,
Lookup the socket address in the opened MaxMind DB, returning the found value (if any) and the prefix length of the network associated with the lookup.
Returns Ok((None, prefix_len))
if the address is found in the tree but has no data record.
Returns Err(...)
for database errors (IO, corruption, decoding).
Example:
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb")?;
let ip: IpAddr = "89.160.20.128".parse().unwrap(); // Known IP
let ip_unknown: IpAddr = "10.0.0.1".parse().unwrap(); // Unknown IP
let (city_option, prefix_len) = reader.lookup_prefix::<geoip2::City>(ip)?;
if let Some(city) = city_option {
println!("Found {:?} at prefix length {}", city.city.unwrap().names.unwrap().get("en").unwrap(), prefix_len);
} else {
// This case is less likely with lookup_prefix if the IP resolves in the tree
println!("IP found in tree but no data (prefix_len: {})", prefix_len);
}
let (city_option_unknown, prefix_len_unknown) = reader.lookup_prefix::<geoip2::City>(ip_unknown)?;
assert!(city_option_unknown.is_none());
println!("Unknown IP resolved to prefix_len: {}", prefix_len_unknown);
Sourcepub fn within<T>(
&'de self,
cidr: IpNetwork,
) -> Result<Within<'de, T, S>, MaxMindDbError>where
T: Deserialize<'de>,
pub fn within<T>(
&'de self,
cidr: IpNetwork,
) -> Result<Within<'de, T, S>, MaxMindDbError>where
T: Deserialize<'de>,
Iterate over blocks of IP networks in the opened MaxMind DB
Example:
use ipnetwork::IpNetwork;
use maxminddb::{geoip2, Within};
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip_net = IpNetwork::V6("::/0".parse().unwrap());
let mut iter: Within<geoip2::City, _> = reader.within(ip_net).unwrap();
while let Some(next) = iter.next() {
let item = next.unwrap();
println!("ip_net={}, city={:?}", item.ip_net, item.info);
}
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Reader<S>where
S: Freeze,
impl<S> RefUnwindSafe for Reader<S>where
S: RefUnwindSafe,
impl<S> Send for Reader<S>where
S: Send,
impl<S> Sync for Reader<S>where
S: Sync,
impl<S> Unpin for Reader<S>where
S: Unpin,
impl<S> UnwindSafe for Reader<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more