#country-code #country #codes #iso3166-1 #alpha2 #alpha3

celes

Rust crate for handling ISO 3166-1. Each country has a three digit code, two letter code, three letter code, full state name, and short english aliases

13 stable releases

new 2.5.0 Nov 7, 2024
2.4.0 Aug 30, 2022
2.2.0 Mar 29, 2022
2.1.0 Aug 2, 2021
1.0.5 Nov 26, 2019

#32 in Internationalization (i18n)

Download history 2705/week @ 2024-07-18 2197/week @ 2024-07-25 1930/week @ 2024-08-01 1982/week @ 2024-08-08 2311/week @ 2024-08-15 2943/week @ 2024-08-22 2876/week @ 2024-08-29 2470/week @ 2024-09-05 3074/week @ 2024-09-12 5045/week @ 2024-09-19 5022/week @ 2024-09-26 4283/week @ 2024-10-03 4220/week @ 2024-10-10 5617/week @ 2024-10-17 5103/week @ 2024-10-24 5554/week @ 2024-10-31

21,594 downloads per month
Used in 7 crates (6 directly)

Apache-2.0 OR MIT

205KB
4.5K SLoC

Celes

crate Docs Apache2/MIT licensed Downloads Maintenance Status: Passively-Maintained build MSRV

Convenience crate for handling ISO 3166-1. Also compatible with no-std environments.

If there are any countries missing then please let me know or submit a PR

The main struct is Country which provides the following properties

  • code - The three digit code for the country
  • value - The code as an integer
  • alpha2 - The alpha2 letter set for the country
  • alpha3 - The alpha3 letter set for the country
  • long_name - The official state name for the country
  • aliases - Other names by which the country is known. For example,

The Russian Federation is also called Russia or The United Kingdom of Great Britain and Northern Ireland is also called England, Great Britain, Northern Ireland, Scotland, and United Kingdom.

Each country can be instantiated by using a function with the country name in snake case

Usage

use celes::Country;

fn main() {
     let gb = Country::the_united_kingdom_of_great_britain_and_northern_ireland();
     println!("{}", gb);

     let usa = Country::the_united_states_of_america();
     println!("{}", usa);
}

Additionally, each country can be created from a string or its numeric code. Country provides multiple from methods to instantiate it from a string:

  • from_code - create Country from three digit code
  • from_alpha2 - create Country from two letter code
  • from_alpha3 - create Country from three letter code
  • from_alias - create Country from a common alias. This only works for some countries as not all countries have aliases
  • from_name - create Country from the full state name no space or underscores

Country implements the core::str::FromStr trait that accepts any valid argument to the previously mentioned functions such as:

  • The country aliases like UnitedKingdom, GreatBritain, Russia, America
  • The full country name
  • The alpha2 code
  • The alpha3 code

If you are uncertain which function to use, just use Country::from_str as it accepts any of the valid string values. Country::from_str is case-insensitive

From String Example

use celes::Country;
use core::str::FromStr;

fn main() {
     // All three of these are equivalent
     let usa_1 = Country::from_str("USA").unwrap();
     let usa_2 = Country::from_str("US").unwrap();
     let usa_3 = Country::from_str("America").unwrap();

     // All three of these are equivalent
     let gb_1 = Country::from_str("England").unwrap();
     let gb_2 = Country::from_str("gb").unwrap();
     let gb_3 = Country::from_str("Scotland").unwrap();
}

Documentation

License

Licensed under

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

Dependencies

~0.3–1MB
~22K SLoC