[][src]Crate 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.

Examples

Retrieve jurisdiction from API boundary and validate supported jurisdictions

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")),
    }
}

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);

Construct Jurisdiction from string:

let jurisdiction = Jurisdiction::from_str("NO");
assert!(jurisdiction.is_ok());
assert_eq!(jurisdiction.unwrap(), Alpha2::NO);

Construct Jurisdiction from Alpha2/Alpha3:

let jurisdiction = Jurisdiction::from(Alpha2::NO);
assert_eq!(jurisdiction, Alpha2::NO);

let jurisdiction = Jurisdiction::from(Alpha3::NOR);
assert_eq!(jurisdiction, Alpha3::NOR);

Static jurisdiction information

All the static information about a jurisdiction is embedded into the application binary through a lazy_static hashmap declaration, populated on first use from const definitions. This way, the only copy of the definition should reside in the hashmap.

This map is not publicly exported from the crate, only accessible through Jurisdiction. A Jurisdiction object simply contains the reference to the definition within this hashmap, making all look-up operations a simple pointer dereference into the statically stored item in this global hashmap.

Features

This crate has the following features:

  • region: Include the region module with region definitions and Jurisdiction array methods returning the zoning jurisdictions within these regions (in_*_region).

Modules

region

UN M49 region definitions.

Structs

Jurisdiction

A pointer sized object encoding countries and areas of the world.

Enums

Alpha2

Two alpha character ISO 3166 country code classification.

Alpha3

Three alpha character ISO 3166 country code classification.