Crates.io | north-america |
lib.rs | north-america |
version | |
source | src |
created_at | 2024-12-19 07:52:35.352183 |
updated_at | 2025-02-06 22:58:05.827289 |
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 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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)
Greenland
Mexico
UnitedStates(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.