#country-code #country #iso-3166 #localization

jurisdiction

A lightweight API-friendly abstraction for the jurisdiction in the world, and their accompanying static information pertaining to that jurisdiction. Information available includes: * ISO 3166 country codes * UN M49 Region classifications

2 releases

0.1.1 Apr 27, 2020
0.1.0 Apr 6, 2020

#211 in Internationalization (i18n)

Download history 3987/week @ 2024-08-15 2534/week @ 2024-08-22 3062/week @ 2024-08-29 3790/week @ 2024-09-05 2663/week @ 2024-09-12 2693/week @ 2024-09-19 2187/week @ 2024-09-26 2601/week @ 2024-10-03 2988/week @ 2024-10-10 2842/week @ 2024-10-17 2446/week @ 2024-10-24 2141/week @ 2024-10-31 2768/week @ 2024-11-07 3032/week @ 2024-11-14 3400/week @ 2024-11-21 2691/week @ 2024-11-28

12,485 downloads per month

MIT license

34KB
603 lines

Jurisdiction

Lightweight static Jurisdiction information.

This crate provides interfaces to work with jurisdictions for areas around the world. Information about a jurisdiction includes

  • ISO 3166 Alpha2 and Alpha3 character codes.
  • ISO 3166 numeric country code.
  • UN M49 region classifications.

The Jurisdiction object is a lightweight object, the size of a pointer, suitable for transfer in API surfaces throughout an ecosystem. Serialization on API boundaries may choose to employ any of the standardized classification formats.

Example

use anyhow::{Result, anyhow, format_err};
use jurisdiction::{Jurisdiction, Alpha2, Alpha3};
use jurisdiction::region::{Region, SubRegion};
use std::str::FromStr;

fn supported_jurisdiction(alpha: &str) -> Result<Jurisdiction> {
    let jurisdiction = Jurisdiction::from_str(alpha)?;
    match jurisdiction.alpha2() {
        Alpha2::NO | Alpha2::SE | Alpha2::DK => Ok(jurisdiction),
        _ => Err(format_err!("only scandinavian countries are supported")),
    }
}

fn main() {
    let jurisdiction = supported_jurisdiction("NO").expect("unsupported");

    assert_eq!(jurisdiction, Alpha2::NO);
    assert_eq!(jurisdiction.alpha2(), Alpha2::NO);
    assert_eq!(jurisdiction.alpha2().to_string(), "NO");

    assert_eq!(jurisdiction, Alpha3::NOR);
    assert_eq!(jurisdiction.alpha3(), Alpha3::NOR);
    assert_eq!(jurisdiction.alpha3().to_string(), "NOR");

    assert_eq!(jurisdiction.country_code(), 578);

    assert_eq!(jurisdiction.region(), Region::Europe);
    assert_eq!(jurisdiction.sub_region(), SubRegion::NorthernEurope);
}

See docs.rs for more extensive API documentation and examples.

Dependencies

~0.5–1.2MB
~25K SLoC