Crate celes

Source
Expand description

Rust implementation of Countries as specified by Country Codes and ISO-3166-1

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;


 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 stripped of any spaces or underscores. 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;

// All of these are equivalent
assert_eq!("US", Country::from_str("USA").unwrap().alpha2);
assert_eq!("US", Country::from_str("US").unwrap().alpha2);
assert_eq!("US", Country::from_str("America").unwrap().alpha2);
assert_eq!("US", Country::from_str("UnitedStates").unwrap().alpha2);
assert_eq!("US", Country::from_str("TheUnitedStatesOfAmerica").unwrap().alpha2);

// All of these are equivalent
assert_eq!("GB", Country::from_str("England").unwrap().alpha2);
assert_eq!("GB", Country::from_str("gb").unwrap().alpha2);
assert_eq!("GB", Country::from_str("Scotland").unwrap().alpha2);
assert_eq!("GB", Country::from_str("TheUnitedKingdomOfGreatBritainAndNorthernIreland").unwrap().alpha2);

Structs§

Enums§

  • Wrapper struct for alias tables to avoid using Box

Statics§

Traits§

  • A lookup table where all elements are statically known