Skip to main content

Crate ipflag

Crate ipflag 

Source
Expand description

§IP Flag (ipflag)

IP Flag is a small, resolver-pluggable Rust crate that displays a human-friendly country indicator for an IP address (flag emoji + country code).

This crate is intentionally not a geolocation database and does not contact external services. It only provides:

  • IP parsing (parse_ip)
  • IP scope classification (classify_ip)
  • A resolver interface (IpResolver) for IP → CountryCode lookup
  • Display helpers (IpTag, TagFormat)

§Important concept: “Resolver”

This crate does not know what country an IP belongs to by itself. To get real country results, you must provide a resolver implementation.

A resolver can be backed by anything:

  • A local GeoIP database (e.g., MaxMind mmdb)
  • A custom IP-to-country mapping
  • A reverse-proxy header mapping (if you trust your infra)
  • Any other internal source

The core stays stable and maintenance-free; data sources can be built as separate crates.

§Quick start (no country resolution)

Even without a resolver, IP Flag can still provide useful UI output for private/special IPs.

use ipflag::{tag_ip, NoopResolver};

let t1 = tag_ip(&NoopResolver, "192.168.0.1").unwrap();
assert_eq!(t1.to_string(), "🏠 PRIVATE");

let t2 = tag_ip(&NoopResolver, "8.8.8.8").unwrap();
assert_eq!(t2.to_string(), "🌐 UNKNOWN");

§Quick start (with a resolver)

You provide a resolver that returns a CountryCode for public IPs. This example is intentionally hard-coded (demo only):

use ipflag::{CountryCode, IpResolver, tag_ip};
use std::net::IpAddr;

struct DemoResolver;
impl IpResolver for DemoResolver {
    type Error = core::convert::Infallible;
    fn resolve(&self, _ip: IpAddr) -> Result<Option<CountryCode>, Self::Error> {
        Ok(CountryCode::new("KR"))
    }
}

let t = tag_ip(&DemoResolver, "8.8.8.8").unwrap();
assert_eq!(t.to_string(), "🇰🇷 KR");

In real usage, your resolver should consult real data (e.g., GeoIP database).

§Non-goals

IP Flag does not:

  • Detect manipulation / label behavior as suspicious
  • Identify people or guarantee nationality
  • Provide geolocation data by itself

It only helps make IP-based patterns easier to scan visually.

Re-exports§

pub use country::CountryCode;
pub use error::IpflagError;
pub use ip::classify_ip;
pub use ip::parse_ip;
pub use ip::IpScope;
pub use resolver::IpResolver;
pub use resolver::NoopResolver;
pub use tag::IpTag;
pub use tag::TagFormat;

Modules§

country
error
ip
resolver
tag

Functions§

tag_addr
Tag an already-parsed IpAddr into an IpTag.
tag_ip
Tag an IP string into an IpTag.