WorldRegion Crate
The WorldRegion crate provides a robust, structured representation of countries, continents, and their subregions. It allows you to:
- Convert between
Countryenums and aWorldRegionenum that supports continents and subdivided regions (like states/provinces or regional groupings). - Retrieve ISO3166 Alpha2, Alpha3, and CountryCode representations from world regions.
- Serialize and deserialize
WorldRegionvalues to and from JSON, including continent, country, and optional subregion fields. - Handle errors gracefully through structured error types (e.g.,
WorldRegionConversionError,WorldRegionParseError). - Support abbreviation lookups and conversions to and from
Country, making it easy to integrate geography into your application.
Features
- continent-to-country conversions: Convert a
Countryinto the correspondingWorldRegionvariant or vice versa. - ISO codes: Extract ISO3166 Alpha2, Alpha3, and
CountryCodevariants directly from aWorldRegion. - Serialization & Deserialization: Serialize
WorldRegioninto a structured JSON object withcontinent,country, and optionalregionkeys. Deserialize back toWorldRegioneasily. - Error Handling: Fine-grained error enums to understand why a conversion or parse failed.
Example
use ;
use TryFrom;
// Convert a known country to its world region
let c = France;
let wr = try_from.expect;
println!; // e.g., WorldRegion::Europe(EuropeRegion::France(FranceRegion::IleDeFrance))
// Convert back to a country
let back: Country = wr.try_into.expect;
assert_eq!;
// Obtain ISO codes
use TryInto;
let alpha2: Iso3166Alpha2 = wr.clone.try_into.expect;
assert_eq!;
Serialization / Deserialization
use serde_json;
use ;
let wr = Europe;
let json = to_string.expect;
println!;
// Outputs: {"continent":"Europe","country":"France","region":"Ile-de-France"}
let deserialized: WorldRegion = from_str.expect;
assert_eq!;
Error Handling
Conversions can fail if a country or region isn't represented. Errors are returned via Result<T, WorldRegionConversionError> or WorldRegionParseError, allowing you to handle them gracefully.
License
This crate is distributed under the MIT license. See LICENSE for details.