LookupResult

Struct LookupResult 

Source
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:

§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>

Source

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.

Source

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.

Source

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.

Source

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 decoded
  • Ok(None) if the IP was not found in the database
  • Err(...) 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");
}
Source

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 decoded
  • Ok(None) if the path doesn’t exist (key missing, index out of bounds)
  • Err(...) if there’s a type mismatch during navigation (e.g., Key on an array)

If has_data() == false, returns Ok(None).

§Path Elements
  • PathElement::Key("name") - Navigate into a map by key
  • PathElement::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>

Source§

fn clone(&self) -> LookupResult<'a, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, S: Debug + AsRef<[u8]>> Debug for LookupResult<'a, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, S: Copy + AsRef<[u8]>> Copy for LookupResult<'a, S>

Auto Trait Implementations§

§

impl<'a, S> Freeze for LookupResult<'a, S>

§

impl<'a, S> RefUnwindSafe for LookupResult<'a, S>
where S: RefUnwindSafe,

§

impl<'a, S> Send for LookupResult<'a, S>
where S: Sync,

§

impl<'a, S> Sync for LookupResult<'a, S>
where S: Sync,

§

impl<'a, S> Unpin for LookupResult<'a, S>

§

impl<'a, S> UnwindSafe for LookupResult<'a, S>
where S: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.