Crates.io | asia |
lib.rs | asia |
version | |
source | src |
created_at | 2024-12-19 06:42:45.331392 |
updated_at | 2025-02-06 22:57:45.798883 |
description | A Rust crate providing enums and conversions for Asian countries and their subregions, analogous to the Europe crate. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1488838 |
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 asia crate provides a rich enumeration of Asian countries and their subregions, with functionality similar to the europe
crate. It includes:
AsiaRegion
enum representing both individual countries and subdivided countries.AsiaRegion
and standard ISO country codes.AsiaRegion
to Country
(from a shared country
crate) and back.AsiaRegion
and its subregions, including nested subregions like ChinaRegion
, IndiaRegion
, JapanRegion
, and IndonesiaRegion
.unwrap
or expect
: All error handling is done gracefully with Result
and custom error types.thiserror
: Error handling is implemented manually for full control and consistency.Convert a AsiaRegion
to a Country
:
use asia::{AsiaRegion, Country};
use std::convert::TryInto;
let region = AsiaRegion::Japan(asia::JapanRegion::Hokkaido);
let country: Country = region.try_into().expect("Should map to Japan");
assert_eq!(country.to_string(), "Japan");
Convert a Country
back to AsiaRegion
:
use asia::{AsiaRegion, Country};
use std::convert::TryInto;
let country = Country::India;
let region: AsiaRegion = country.try_into().expect("Should map to India");
assert!(matches!(region, AsiaRegion::India(_)));
Serialize and deserialize AsiaRegion
with JSON:
use asia::AsiaRegion;
use asia::ChinaRegion;
use serde_json;
let region = AsiaRegion::China(ChinaRegion::Beijing);
let json = serde_json::to_string(®ion).expect("serialize");
assert!(json.contains("\"country\":\"China\""));
assert!(json.contains("\"region\":\"Beijing\""));
let deser: AsiaRegion = serde_json::from_str(&json).expect("deserialize");
assert_eq!(deser, region);
When attempting conversions that are not possible (e.g., mapping a combined region like GCC States to a single Country
), a custom error type AsiaRegionConversionError
is returned, allowing you to handle these cases gracefully.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
This project is licensed under the MIT license. See LICENSE for details.