Crates.io | country |
lib.rs | country |
version | |
source | src |
created_at | 2024-12-11 18:59:44.269345 |
updated_at | 2024-12-11 23:07:57.923564 |
description | A Rust library providing a single source of truth for country enumeration and their associated ISO 3166-1 alpha-2 and alpha-3 codes |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1480456 |
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 |
country
is a Rust library providing a single source of truth for country enumeration and their associated ISO 3166-1 alpha-2 and alpha-3 codes. By using a macro-based approach, all country variants, along with their standardized ISO codes, are defined and linked together in one place, eliminating redundancy and reducing maintenance overhead.
Single Source of Truth:
All Country
variants and their corresponding ISO codes (Iso3166Alpha2
, Iso3166Alpha3
) are declared once via a macro. This generates:
Country
enum enumerating countries.Iso3166Alpha2
and Iso3166Alpha3
enums representing standardized country codes.Country
and ISO codes.Automatic Code Generation:
The macro expands into:
From<Country>
conversions to Iso3166Alpha2
and Iso3166Alpha3
.FromStr
implementations for each enum to parse from strings.Display
implementations for easy printing.Country::alpha2()
and Country::alpha3()
for quick code lookups.Serde Integration:
All enums derive Serialize
and Deserialize
, making it easy to persist and exchange country data in JSON or other formats.
Extensible & Maintainable:
Adding or modifying a country's mapping is as simple as editing a single macro invocation. No need to update multiple code sections.
use country::{Country, Iso3166Alpha2, Iso3166Alpha3};
use std::str::FromStr;
fn main() {
let c = Country::USA;
println!("Country: {}", c); // "USA"
println!("Alpha-2: {:?}", c.alpha2()); // Some like US
println!("Alpha-3: {:?}", c.alpha3()); // Some like USA
let from_alpha2 = Iso3166Alpha2::from_str("GB").unwrap();
println!("From Alpha-2: {:?}", from_alpha2); // GBR for Great Britain
let from_country_str = Country::from_str("France").unwrap();
println!("From Country str: {}", from_country_str); // "France"
}
Localization & Internationalization:
Quickly convert between human-friendly country names and standardized codes for UIs, APIs, and data storage.
Data Validation & Parsing:
Validate country inputs from users, forms, or external services by parsing into Country
, ensuring correctness and consistency.
Geopolitical & Mapping Apps:
Serve as a robust backbone for applications that rely heavily on country data, enabling swift lookups, code conversions, and serialization.
Update the macro invocation in the source code. All conversions and parsing logic will be regenerated automatically during compilation, ensuring no inconsistencies.
This project is licensed under the MIT license. See the LICENSE file for details.