North America Crate
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:
- A
NorthAmericaRegionenum with:Canada(CanadaRegion)GreenlandMexicoUnitedStates(USRegion), leveraging the existingusacrate for U.S. subdivisions.
- Conversions between
NorthAmericaRegionandCountry. - ISO code conversions (
Iso3166Alpha2,Iso3166Alpha3,CountryCode). - Serde support for serialization/deserialization, including subdivided regions.
- No unsafe code, no unwrap, and no thiserror. All error handling is manual and explicit.
Examples
Convert NorthAmericaRegion to Country:
use ;
use TryInto;
let region = Canada;
let country: Country = region.try_into.expect;
assert_eq!;
Convert Country back to NorthAmericaRegion:
use ;
use TryInto;
let country = Mexico;
let region: NorthAmericaRegion = country.try_into.expect;
assert_eq!;
Serialize and deserialize:
use ;
use serde_json;
let region = Canada;
let json = to_string.expect;
assert!;
assert!;
let deser: NorthAmericaRegion = from_str.expect;
assert_eq!;
Error Handling
Non-North American countries fail conversion with a NorthAmericaRegionConversionError.
Contributing
Issues and PRs are welcome.
License
MIT licensed. See LICENSE.