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 fromAfricaRegiontoCountrywhere possible, and fromCountrytoAfricaRegionfor African countries. Combined or non-mappable regions return informative errors. -
ISO Codes and Abbreviations: Each
AfricaRegionvariant can be converted toIso3166Alpha2,Iso3166Alpha3, andCountryCodewhen applicable. Additionally, theAbbreviationtrait provides short codes for each region (for example,Nigeria-> "NG"). -
Serialization/Deserialization: Out-of-the-box
serdesupport allows you to serialize and deserializeAfricaRegionvalues to JSON. By default, regions are stored as:You can deserialize these structures back into
AfricaRegionvariants. -
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 AfricaRegion;
use TryInto;
Testing
A comprehensive test suite is provided. Simply run:
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.