Skip to main content

Reader

Struct Reader 

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

Source

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>

Source

pub fn from_source(buf: S) -> Result<Reader<S>, MaxMindDBError>

Open a MaxMind DB database from anything that implements AsRef<u8>

§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();
Source

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);
Source

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);
Source

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§

Source§

impl<S: Debug + AsRef<[u8]>> Debug for Reader<S>

Source§

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

Formats the value using the given formatter. Read more

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> UnsafeUnpin for Reader<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for Reader<S>
where S: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more