2 unstable releases
new 0.2.0 | Dec 19, 2024 |
---|---|
0.1.0 | Dec 19, 2024 |
#18 in #countries
209 downloads per month
Used in world-region
120KB
1.5K
SLoC
Africa Crate
The africa
crate provides an enumeration of African regions (primarily countries and a few combined or special territories) analogous to the asia
crate in the provided example. It supports serialization, deserialization, conversion to and from Country
enums, ISO codes, and also provides abbreviations for each region.
Features
-
Robust Enumeration of African Regions: All African countries are represented as variants of
AfricaRegion
, along with certain combined or special regions (e.g., Canary Islands, Senegal and Gambia, Saint Helena, Ascension, and Tristan da Cunha). -
Conversions to and from
Country
: The crate allows converting fromAfricaRegion
toCountry
where possible, and fromCountry
toAfricaRegion
for African countries. Combined or non-mappable regions return informative errors. -
ISO Codes and Abbreviations: Each
AfricaRegion
variant can be converted toIso3166Alpha2
,Iso3166Alpha3
, andCountryCode
when applicable. Additionally, theAbbreviation
trait provides short codes for each region (for example,Nigeria
-> "NG"). -
Serialization/Deserialization: Out-of-the-box
serde
support allows you to serialize and deserializeAfricaRegion
values to JSON. By default, regions are stored as:{ "country": "Nigeria" }
You can deserialize these structures back into
AfricaRegion
variants. -
Error Handling: Instead of panicking, all conversions that may fail return strongly typed error variants (
AfricaRegionConversionError
), enabling robust error handling in production systems.
Example
use africa::AfricaRegion;
use std::convert::TryInto;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let region = AfricaRegion::Nigeria;
let country = region.try_into()?; // Convert to Country
println!("Country: {:?}", country);
let iso_code: country::Iso3166Alpha2 = region.try_into()?;
println!("ISO Alpha-2: {:?}", iso_code);
let json = serde_json::to_string(®ion)?;
println!("Serialized: {}", json);
let deserialized: AfricaRegion = serde_json::from_str(&json)?;
println!("Deserialized: {:?}", deserialized);
Ok(())
}
Testing
A comprehensive test suite is provided. Simply run:
cargo test
This will run all tests ensuring correctness of default values, conversions, abbreviations, serialization/deserialization, and error handling.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Dependencies
~1.6–2.7MB
~52K SLoC