| Crates.io | central-america |
| lib.rs | central-america |
| version | 0.5.0 |
| created_at | 2024-12-19 07:09:31.458226+00 |
| updated_at | 2025-02-06 22:50:33.07446+00 |
| 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 |
| size | 77,114 |
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-featuregit commit -am 'Add some feature'git push origin my-new-featureLicensed under the MIT license. See LICENSE for details.