Crates.io | central-america |
lib.rs | central-america |
version | |
source | src |
created_at | 2024-12-19 07:09:31.458226 |
updated_at | 2025-02-06 22:50:33.07446 |
description | A Rust crate providing enums and conversions for Central American countries and their subregions, analogous to the asia and europe crates. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1488855 |
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 central-america crate provides enums and conversions for Central American and nearby Caribbean countries and their combined region (Haiti and Dominican Republic), analogous to the asia
, europe
, and south-america
crates.
CentralAmericaRegion
includes countries like Belize, Costa Rica, Nicaragua, and a combined region for Haiti and the Dominican Republic.CentralAmericaRegion
to Country
(from a shared country
crate), Iso3166Alpha2
, Iso3166Alpha3
, and CountryCode
.CentralAmericaRegion
to/from JSON, including combined regions.unsafe
, no unwrap
, and no thiserror
. Errors are handled with custom error types.Convert CentralAmericaRegion
to Country
:
use central_america::{CentralAmericaRegion, Country};
use std::convert::TryInto;
let region = CentralAmericaRegion::CostaRica;
let country: Country = region.try_into().expect("Should map to Costa Rica");
assert_eq!(country.to_string(), "Costa Rica");
Convert Country
back to CentralAmericaRegion
:
use central_america::{CentralAmericaRegion, Country};
use std::convert::TryInto;
let country = Country::Haiti;
let region: CentralAmericaRegion = country.try_into().expect("Should map to Haiti and Dominican Republic combined region");
assert_eq!(region, CentralAmericaRegion::HaitiAndDominicanRepublic);
Serialize and deserialize:
use central_america::CentralAmericaRegion;
use serde_json;
let region = CentralAmericaRegion::ElSalvador;
let json = serde_json::to_string(®ion).expect("serialize");
assert!(json.contains("\"country\":\"El Salvador\""));
let deser: CentralAmericaRegion = serde_json::from_str(&json).expect("deserialize");
assert_eq!(deser, region);
If you attempt to convert a Country
not represented in Central America to a CentralAmericaRegion
, you get a CentralAmericaRegionConversionError
. Similarly, no unwrap
or expect
are used internally.
Contributions, issues, and feature requests are welcome! Check the issues page.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
Licensed under the MIT license. See LICENSE for details.