Module geoip2

Module geoip2 

Source
Expand description

GeoIP2 and GeoLite2 database record structures

This module provides strongly-typed Rust structures that correspond to the various GeoIP2 and GeoLite2 database record formats.

§Record Types

  • City - Complete city-level geolocation data (most comprehensive)
  • Country - Country-level geolocation data
  • Enterprise - Enterprise database with additional confidence scores
  • Isp - Internet Service Provider information
  • AnonymousIp - Anonymous proxy and VPN detection
  • ConnectionType - Connection type classification
  • Domain - Domain information
  • Asn - Autonomous System Number data
  • DensityIncome - Population density and income data

§Usage Examples

use maxminddb::{Reader, geoip2};
use std::net::IpAddr;

let reader = Reader::open_readfile(
    "test-data/test-data/GeoIP2-City-Test.mmdb")?;
let ip: IpAddr = "89.160.20.128".parse().unwrap();

// City lookup - nested structs are always present (default to empty)
let result = reader.lookup(ip)?;
if let Some(city) = result.decode::<geoip2::City>()? {
    // Direct access to nested structs - no Option unwrapping needed
    if let Some(name) = city.city.names.english {
        println!("City: {}", name);
    }
    if let Some(code) = city.country.iso_code {
        println!("Country: {}", code);
    }
    // Subdivisions is a Vec, empty if not present
    for sub in &city.subdivisions {
        if let Some(code) = sub.iso_code {
            println!("Subdivision: {}", code);
        }
    }
}

// Country-only lookup (smaller/faster)
let result = reader.lookup(ip)?;
if let Some(country) = result.decode::<geoip2::Country>()? {
    if let Some(name) = country.country.names.english {
        println!("Country: {}", name);
    }
}

Modules§

city
City database model structs.
country
Country/City database model structs.
enterprise
Enterprise database model structs.

Structs§

AnonymousIp
GeoIP2 Anonymous IP database record.
Asn
GeoLite2 ASN database record.
City
GeoIP2/GeoLite2 City database record.
ConnectionType
GeoIP2 Connection-Type database record.
Country
GeoIP2/GeoLite2 Country database record.
DensityIncome
GeoIP2 DensityIncome database record.
Domain
GeoIP2 Domain database record.
Enterprise
GeoIP2 Enterprise database record.
Isp
GeoIP2 ISP database record.
Names
Localized names for geographic entities.