GeoIpDb

Struct GeoIpDb 

Source
pub struct GeoIpDb { /* private fields */ }
Expand description

Offline, in-memory lookup database for allocation-based IP classification.

The default constructor (new) uses range tables generated at build time. Lookups are performed with binary search and do not allocate.

Implementations§

Source§

impl GeoIpDb

Source

pub fn new() -> Self

Construct a database using the embedded range tables generated at build time.

This is the fastest and most predictable option: no I/O and no parsing at runtime.

§Examples
use offline_ripe_geoip::GeoIpDb;

let db = GeoIpDb::new();
let info = db.lookup("46.4.0.1".parse().unwrap());
assert!(info.is_some());
Source

pub fn from_ripe_delegated_str(content: &str) -> Self

Build a database by parsing RIPE delegated stats content at runtime.

This is useful when you want to load newer data from a cache or ship your own dataset. The resulting ranges are sorted for efficient lookup.

§Examples
use offline_ripe_geoip::GeoIpDb;

let data = "ripencc|DE|ipv4|46.4.0.0|256|20250101|allocated\n";
let db = GeoIpDb::from_ripe_delegated_str(data);
assert!(db.lookup("46.4.0.1".parse().unwrap()).is_some());
Source

pub fn from_ripe_delegated_file<P: AsRef<Path>>(path: P) -> Result<Self>

Load RIPE delegated stats content from a file and build a database.

§Errors

Returns an error if the file cannot be read.

Source

pub fn from_cache_or_embedded<P: AsRef<Path>>(cache_path: P) -> Self

Try to load the database from a cache file, falling back to embedded data.

This is a convenience helper for “use cache if present, otherwise use the built-in tables”.

Source

pub fn lookup_v4(&self, ip: Ipv4Addr) -> Option<&GeoInfo>

Look up a single IPv4 address.

Returns None if the address is not covered by the embedded/loaded ranges.

Source

pub fn lookup_v6(&self, ip: Ipv6Addr) -> Option<&GeoInfo>

Look up a single IPv6 address.

Returns None if the address is not covered by the embedded/loaded ranges.

Source

pub fn lookup(&self, ip: IpAddr) -> Option<&GeoInfo>

Look up an IP address (IPv4 or IPv6).

§Examples
use offline_ripe_geoip::GeoIpDb;

let db = GeoIpDb::new();
let info = db.lookup("46.4.0.1".parse().unwrap()).unwrap();
assert_eq!(info.country_code_str(), "DE");
Source

pub fn is_eu(&self, ip: IpAddr) -> bool

Return true if the IP is covered by the database and classified as EU.

Addresses not found in the database return false.

Source

pub fn stats(&self) -> DbStats

Return basic statistics about the loaded database.

This can be useful for sanity checks (e.g., validating that data loaded correctly).

Trait Implementations§

Source§

impl Default for GeoIpDb

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.

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.