pub struct LookupResult<'a, S: AsRef<[u8]>> { /* private fields */ }Expand description
The result of looking up an IP address in a MaxMind DB.
This is a lightweight handle (~40 bytes) that stores the lookup result without immediately decoding the data. You can:
- Check if data exists with
has_data() - Get the network containing the IP with
network() - Decode the full record with
decode() - Decode a specific path with
decode_path()
§Example
use maxminddb::{Reader, geoip2, PathElement};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let result = reader.lookup(ip).unwrap();
if result.has_data() {
// Full decode
let city: geoip2::City = result.decode().unwrap().unwrap();
// Or selective decode via path
let country_code: Option<String> = result.decode_path(&[
PathElement::Key("country"),
PathElement::Key("iso_code"),
]).unwrap();
println!("Country: {:?}", country_code);
}Implementations§
Source§impl<'a, S: AsRef<[u8]>> LookupResult<'a, S>
impl<'a, S: AsRef<[u8]>> LookupResult<'a, S>
Sourcepub fn has_data(&self) -> bool
pub fn has_data(&self) -> bool
Returns true if the database contains data for this IP address.
Note that false means the database has no data for this IP,
which is different from an error during lookup.
Sourcepub fn network(&self) -> Result<IpNetwork, MaxMindDbError>
pub fn network(&self) -> Result<IpNetwork, MaxMindDbError>
Returns the network containing the looked-up IP address.
This is the most specific network in the database that contains the IP, regardless of whether data was found.
The returned network preserves the IP version of the original lookup:
- IPv4 lookups return IPv4 networks (unless prefix < 96, see below)
- IPv6 lookups return IPv6 networks (including IPv4-mapped addresses)
Special case: If an IPv4 address is looked up in an IPv6 database but the matching record is at a prefix length < 96 (e.g., a database with no IPv4 subtree), an IPv6 network is returned since there’s no valid IPv4 representation.
Sourcepub fn offset(&self) -> Option<usize>
pub fn offset(&self) -> Option<usize>
Returns the data section offset if found, for use as a cache key.
Multiple IP addresses often point to the same data record. This offset can be used to deduplicate decoding or cache results.
Returns None if the IP was not found.
Sourcepub fn decode<T>(&self) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'a>,
pub fn decode<T>(&self) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'a>,
Decodes the full record into the specified type.
Returns:
Ok(Some(T))if found and successfully decodedOk(None)if the IP was not found in the databaseErr(...)if decoding fails
§Example
use maxminddb::{Reader, geoip2};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let result = reader.lookup(ip).unwrap();
if let Some(city) = result.decode::<geoip2::City>()? {
println!("Found city data");
}Sourcepub fn decode_path<T>(
&self,
path: &[PathElement<'_>],
) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'a>,
pub fn decode_path<T>(
&self,
path: &[PathElement<'_>],
) -> Result<Option<T>, MaxMindDbError>where
T: Deserialize<'a>,
Decodes a value at a specific path within the record.
Returns:
Ok(Some(T))if the path exists and was successfully decodedOk(None)if the path doesn’t exist (key missing, index out of bounds)Err(...)if there’s a type mismatch during navigation (e.g.,Keyon an array)
If has_data() == false, returns Ok(None).
§Path Elements
PathElement::Key("name")- Navigate into a map by keyPathElement::Index(0)- Navigate into an array by index (0 = first element)PathElement::IndexFromEnd(0)- Navigate from the end (0 = last element)
§Example
use maxminddb::{Reader, PathElement};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let result = reader.lookup(ip).unwrap();
// Navigate to country.iso_code
let iso_code: Option<String> = result.decode_path(&[
PathElement::Key("country"),
PathElement::Key("iso_code"),
]).unwrap();
// Navigate to subdivisions[0].names.en
let subdiv_name: Option<String> = result.decode_path(&[
PathElement::Key("subdivisions"),
PathElement::Index(0),
PathElement::Key("names"),
PathElement::Key("en"),
]).unwrap();Trait Implementations§
Source§impl<'a, S: Clone + AsRef<[u8]>> Clone for LookupResult<'a, S>
impl<'a, S: Clone + AsRef<[u8]>> Clone for LookupResult<'a, S>
Source§fn clone(&self) -> LookupResult<'a, S>
fn clone(&self) -> LookupResult<'a, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more