3 releases (breaking)
new 0.3.0 | Dec 19, 2024 |
---|---|
0.2.0 | Dec 19, 2024 |
0.1.0 | Dec 19, 2024 |
#15 in #countries
38 downloads per month
255KB
4K
SLoC
Asia Crate
The asia crate provides a rich enumeration of Asian countries and their subregions, with functionality similar to the europe
crate. It includes:
- A comprehensive
AsiaRegion
enum representing both individual countries and subdivided countries. - Conversion traits to convert between
AsiaRegion
and standard ISO country codes. - Round-trip conversions from
AsiaRegion
toCountry
(from a sharedcountry
crate) and back. - Serialization/Deserialization (Serde) support for
AsiaRegion
and its subregions, including nested subregions likeChinaRegion
,IndiaRegion
,JapanRegion
, andIndonesiaRegion
. - Exhaustive test suites ensuring correctness and stability.
Features
- No unsafe code: The codebase is entirely safe Rust.
- No
unwrap
orexpect
: All error handling is done gracefully withResult
and custom error types. - No
thiserror
: Error handling is implemented manually for full control and consistency.
Examples
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);
Error Handling
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.
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
License
This project is licensed under the MIT license. See LICENSE for details.
Dependencies
~6.5–8.5MB
~156K SLoC