#countries #region #geography #country-code #rust

asia

A Rust crate providing enums and conversions for Asian countries and their subregions, analogous to the Europe crate

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

MIT license

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 to Country (from a shared country crate) and back.
  • Serialization/Deserialization (Serde) support for AsiaRegion and its subregions, including nested subregions like ChinaRegion, IndiaRegion, JapanRegion, and IndonesiaRegion.
  • Exhaustive test suites ensuring correctness and stability.

Features

  • No unsafe code: The codebase is entirely safe Rust.
  • No unwrap or expect: All error handling is done gracefully with Result 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(&region).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.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

License

This project is licensed under the MIT license. See LICENSE for details.

Dependencies

~6.5–8.5MB
~156K SLoC