Crates.io | africa |
lib.rs | africa |
version | |
source | src |
created_at | 2024-12-19 08:23:03.447973 |
updated_at | 2025-02-06 22:34:53.93357 |
description | A Rust crate providing enums and conversions for African countries and their subregions, analogous to the Europe crate. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1488906 |
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 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.
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 from AfricaRegion
to Country
where possible, and from Country
to AfricaRegion
for African countries. Combined or non-mappable regions return informative errors.
ISO Codes and Abbreviations:
Each AfricaRegion
variant can be converted to Iso3166Alpha2
, Iso3166Alpha3
, and CountryCode
when applicable. Additionally, the Abbreviation
trait provides short codes for each region (for example, Nigeria
-> "NG").
Serialization/Deserialization:
Out-of-the-box serde
support allows you to serialize and deserialize AfricaRegion
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.
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(())
}
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.
This project is licensed under the MIT License - see the LICENSE file for details.