| Crates.io | north-america |
| lib.rs | north-america |
| version | 0.7.0 |
| created_at | 2024-12-19 07:52:35.352183+00 |
| updated_at | 2025-02-06 22:58:05.827289+00 |
| description | A Rust crate providing enums and conversions for North American countries and their subregions, analogous to asia, europe, south-america, and central-america crates. |
| homepage | |
| repository | https://github.com/klebs6/klebs-general |
| max_upload_size | |
| id | 1488890 |
| size | 85,252 |
The north-america crate provides enums and conversions for North American countries and their subregions, analogous to the asia, europe, south-america, and central-america crates. It includes:
NorthAmericaRegion enum with:
Canada(CanadaRegion)GreenlandMexicoUnitedStates(USRegion), leveraging the existing usa crate for U.S. subdivisions.NorthAmericaRegion and Country.Iso3166Alpha2, Iso3166Alpha3, CountryCode).Convert NorthAmericaRegion to Country:
use north_america::{NorthAmericaRegion, CanadaRegion, Country};
use std::convert::TryInto;
let region = NorthAmericaRegion::Canada(CanadaRegion::Ontario);
let country: Country = region.try_into().expect("Should map to Canada");
assert_eq!(country.to_string(), "Canada");
Convert Country back to NorthAmericaRegion:
use north_america::{NorthAmericaRegion, Country};
use std::convert::TryInto;
let country = Country::Mexico;
let region: NorthAmericaRegion = country.try_into().expect("Should map to Mexico");
assert_eq!(region, NorthAmericaRegion::Mexico);
Serialize and deserialize:
use north_america::{NorthAmericaRegion, CanadaRegion};
use serde_json;
let region = NorthAmericaRegion::Canada(CanadaRegion::BritishColumbia);
let json = serde_json::to_string(®ion).expect("serialize");
assert!(json.contains("\"country\":\"Canada\""));
assert!(json.contains("\"region\":\"British Columbia\""));
let deser: NorthAmericaRegion = serde_json::from_str(&json).expect("deserialize");
assert_eq!(deser, region);
Non-North American countries fail conversion with a NorthAmericaRegionConversionError.
Issues and PRs are welcome.
MIT licensed. See LICENSE.