| Crates.io | world-region |
| lib.rs | world-region |
| version | 0.6.0 |
| created_at | 2024-12-19 22:19:45.664357+00 |
| updated_at | 2025-02-06 23:02:10.994629+00 |
| description | A Rust crate providing enums and conversions for World regions and their subregions, building on the africa, europe, north-america, etc crates. |
| homepage | |
| repository | https://github.com/klebs6/klebs-general |
| max_upload_size | |
| id | 1489637 |
| size | 97,911 |
The WorldRegion crate provides a robust, structured representation of countries, continents, and their subregions. It allows you to:
Country enums and a WorldRegion enum that supports continents and subdivided regions (like states/provinces or regional groupings).WorldRegion values to and from JSON, including continent, country, and optional subregion fields.WorldRegionConversionError, WorldRegionParseError).Country, making it easy to integrate geography into your application.Country into the corresponding WorldRegion variant or vice versa.CountryCode variants directly from a WorldRegion.WorldRegion into a structured JSON object with continent, country, and optional region keys. Deserialize back to WorldRegion easily.use world_region::{WorldRegion, Country};
use std::convert::TryFrom;
// Convert a known country to its world region
let c = Country::France;
let wr = WorldRegion::try_from(c).expect("France should be in Europe");
println!("{:?}", wr); // e.g., WorldRegion::Europe(EuropeRegion::France(FranceRegion::IleDeFrance))
// Convert back to a country
let back: Country = wr.try_into().expect("Should convert back to France");
assert_eq!(back, Country::France);
// Obtain ISO codes
use std::convert::TryInto;
let alpha2: Iso3166Alpha2 = wr.clone().try_into().expect("Alpha2 conversion");
assert_eq!(alpha2, Iso3166Alpha2::FR);
use serde_json;
use world_region::{WorldRegion, EuropeRegion, FranceRegion};
let wr = WorldRegion::Europe(EuropeRegion::France(FranceRegion::IleDeFrance));
let json = serde_json::to_string(&wr).expect("serialize");
println!("{}", json);
// Outputs: {"continent":"Europe","country":"France","region":"Ile-de-France"}
let deserialized: WorldRegion = serde_json::from_str(&json).expect("deserialize");
assert_eq!(deserialized, wr);
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.
This crate is distributed under the MIT license. See LICENSE for details.