pub struct Reader<S: AsRef<[u8]>> {
pub metadata: Metadata,
/* private fields */
}Expand description
A reader for the MaxMind DB format. The lifetime of the decoded values is tied to the lifetime of the underlying buffer holding the contents of the database file.
Fields§
§metadata: MetadataImplementations§
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.
Uses ReaderOptions::default for resource limits. Use
Reader::open_readfile_with_options to configure them.
§Example
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();§Errors
Returns MaxMindDBError::IoError if the file cannot be read, or an
error from Reader::from_source if its contents are not a valid
database.
Sourcepub fn open_readfile_with_options<P: AsRef<Path>>(
database: P,
options: ReaderOptions,
) -> Result<Reader<Vec<u8>>, MaxMindDBError>
pub fn open_readfile_with_options<P: AsRef<Path>>( database: P, options: ReaderOptions, ) -> Result<Reader<Vec<u8>>, MaxMindDBError>
Open a MaxMind DB database file by loading it into memory, using
options for resource limits.
§Errors
Returns MaxMindDBError::IoError if the file cannot be read, or an
error from Reader::from_source_with_options if its contents are not
a valid database or exceed the configured limits.
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>
Open a MaxMind DB database from anything that implements AsRef<u8>
Uses ReaderOptions::default for resource limits. Use
Reader::from_source_with_options to configure them.
§Example
use std::fs;
let buf = fs::read("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let reader = maxminddb::Reader::from_source(buf).unwrap();§Errors
Returns MaxMindDBError::InvalidDatabaseError if the buffer is not a
valid MaxMind DB file, and MaxMindDBError::ResourceLimit if decoding
the metadata exceeds the configured limits.
Sourcepub fn from_source_with_options(
buf: S,
options: ReaderOptions,
) -> Result<Reader<S>, MaxMindDBError>
pub fn from_source_with_options( buf: S, options: ReaderOptions, ) -> Result<Reader<S>, MaxMindDBError>
Open a MaxMind DB database from anything that implements AsRef<[u8]>,
using options for resource limits.
§Errors
Returns MaxMindDBError::InvalidDatabaseError if the buffer is not a
valid MaxMind DB file (including unsupported format versions, invalid
record sizes or IP versions, and a search tree that does not fit in the
buffer), and MaxMindDBError::ResourceLimit if decoding the metadata
exceeds the configured limits.
Sourcepub fn lookup<T>(&'de self, address: IpAddr) -> Result<T, MaxMindDBError>where
T: Deserialize<'de>,
pub fn lookup<T>(&'de self, address: IpAddr) -> Result<T, MaxMindDBError>where
T: Deserialize<'de>,
Lookup the socket address in the opened MaxMind DB
Example:
use maxminddb::geoip2;
use std::net::IpAddr;
use std::str::FromStr;
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = FromStr::from_str("89.160.20.128").unwrap();
let city: geoip2::City = reader.lookup(ip).unwrap();
print!("{:?}", city);§Errors
Returns MaxMindDBError::AddressNotFoundError if the address has no
record, MaxMindDBError::InvalidInputError for an IPv6 address in an
IPv4-only database, and a decoding error (including
MaxMindDBError::ResourceLimit) if the record cannot be decoded.
Sourcepub fn lookup_prefix<T>(
&'de self,
address: IpAddr,
) -> Result<(T, usize), MaxMindDBError>where
T: Deserialize<'de>,
pub fn lookup_prefix<T>(
&'de self,
address: IpAddr,
) -> Result<(T, usize), MaxMindDBError>where
T: Deserialize<'de>,
Lookup the socket address in the opened MaxMind DB
Example:
use maxminddb::geoip2;
use std::net::IpAddr;
use std::str::FromStr;
let reader = maxminddb::Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let (city, prefix_len) = reader.lookup_prefix::<geoip2::City>(ip).unwrap();
print!("{:?}, prefix length: {}", city, prefix_len);§Errors
Returns the same errors as Reader::lookup. The prefix length of the
matched network is returned alongside the value.
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);
}§Errors
Returns MaxMindDBError::InvalidInputError if cidr is an IPv6 CIDR
and the database is IPv4-only. Problems found while walking the search
tree are yielded as Err items from the returned iterator rather than
panicking.