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
impl GeoIpDb
Sourcepub fn new() -> Self
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());Sourcepub fn from_ripe_delegated_str(content: &str) -> Self
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());Sourcepub fn from_ripe_delegated_file<P: AsRef<Path>>(path: P) -> Result<Self>
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.
Sourcepub fn from_cache_or_embedded<P: AsRef<Path>>(cache_path: P) -> Self
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”.
Sourcepub fn lookup_v4(&self, ip: Ipv4Addr) -> Option<&GeoInfo>
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.
Sourcepub fn lookup_v6(&self, ip: Ipv6Addr) -> Option<&GeoInfo>
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.
Sourcepub fn lookup(&self, ip: IpAddr) -> Option<&GeoInfo>
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");