north-america

Crates.ionorth-america
lib.rsnorth-america
version
sourcesrc
created_at2024-12-19 07:52:35.352183
updated_at2025-02-06 22:58:05.827289
descriptionA Rust crate providing enums and conversions for North American countries and their subregions, analogous to asia, europe, south-america, and central-america crates.
homepage
repositoryhttps://github.com/klebs6/klebs-general
max_upload_size
id1488890
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`
size0
(klebs6)

documentation

https://docs.rs/north-america

README

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 NorthAmericaRegion enum with:
    • Canada(CanadaRegion)
    • Greenland
    • Mexico
    • UnitedStates(USRegion), leveraging the existing usa crate for U.S. subdivisions.
  • Conversions between NorthAmericaRegion and Country.
  • 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 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(&region).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);

Error Handling

Non-North American countries fail conversion with a NorthAmericaRegionConversionError.

Contributing

Issues and PRs are welcome.

License

MIT licensed. See LICENSE.

Commit count: 357

cargo fmt