use codes_agency::{Agency, Standard, standardized_type}; use codes_common::{code_impl, error, fixed_length_code}; use crate::CountryCodeError; use std::str::FromStr; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "languages")] use codes_iso_639::part_3::LanguageCode; // ------------------------------------------------------------------------------------------------ // Public Types // ------------------------------------------------------------------------------------------------ /// /// An instance of the `Standard` struct defined in the /// [`codes_agency`](https://docs.rs/codes-agency/latest/codes_agency/) /// package that describes the ISO 3166-1 specification. /// pub const ISO_3166_1: Standard = Standard::new_with_long_ref( Agency::ISO, "3166-1", "ISO 3166-1:2020", "Codes for the representation of names of countries and their subdivisions - Part 1: Country code", "https://www.iso.org/standard/72482.html", ); /// /// This is a full enumeration of all the countries defined in ISO 3166-1. /// /// The standard's two-letter code is used as a variant name, in upper /// case. /// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum {{ type_name }} {{ "{" }}{% for id in all_ids %} /// {{ codes[id].short_name | replace(from="[", to="\[") | replace(from="]", to="\]") }} {{ id }},{% endfor %} {{ "}" }} /// Provides an array of all defined [{{ type_name }}] codes, useful for queries. pub const ALL_CODES: [{{ type_name }};{{ all_ids | length }}] = [{% for id in all_ids %} {{ type_name }}::{{ id }},{% endfor %} ]; // ------------------------------------------------------------------------------------------------ // Implementations // ------------------------------------------------------------------------------------------------ impl FromStr for {{ type_name }} {{ "{" }} type Err = {{ type_name }}Error; fn from_str(s: &str) -> Result {{ "{" }} match s {{ "{" }}{% for id in all_ids %} "{{ id }}" => Ok(Self::{{ id }}),{% endfor %} _ => Err(error::unknown_value("{{ type_name }}", s)), {{ "}" }} {{ "}" }} {{ "}" }} code_impl!({{ type_name }}, alpha_2_code); fixed_length_code!({{ type_name }}, 2); standardized_type!({{ type_name }}, ISO_3166_1); impl {{ type_name }} {{ "{" }} /// Returns the two-letter country codes which are the most widely /// used of the three, and used most prominently for the Internet's /// country code top-level domains (with a few exceptions). pub const fn alpha_2_code(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => "{{ id }}",{% endfor %} {{ "}" }} {{ "}" }} /// Returns a name for this country in English; this name is considered usable /// in most cases, although a more formal or complete name may be /// present in [{{ type_name }}::full_name]. pub const fn short_name(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => "{{ codes[id].short_name }}",{% endfor %} {{ "}" }} {{ "}" }} /// Returns the three-letter country codes which allow a better /// visual association between the codes and the country names /// than the alpha-2 codes. #[cfg(feature = "alpha_3_code")] pub const fn alpha_3_code(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].alpha_3_code %} Self::{{ id }} => Some("{{ codes[id].alpha_3_code }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns the three-digit country codes which are identical to /// those developed and maintained by the United Nations /// Statistics Division, with the advantage of script (writing /// system) independence, and hence useful for people or systems /// using non-Latin scripts. #[cfg(feature = "numeric_code")] pub const fn numeric_code(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].numeric_code %} Self::{{ id }} => Some({{ codes[id].numeric_code }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns a boolean value which denotes the country as a legally /// independent state. The standard has some records without values /// which are represented as `None` rather than choose a default /// boolean value. #[cfg(feature = "independent")] pub const fn independent(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].independent %} Self::{{ id }} => Some({{ codes[id].independent }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns this country record's [Status]. #[cfg(feature = "status")] pub const fn status(&self) -> Status {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => {{ codes[id].status }},{% endfor %} {{ "}" }} {{ "}" }} /// Returns a full, formal, or complete, name for this country, if one exists. #[cfg(feature = "full_name")] pub const fn full_name(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].full_name %} Self::{{ id }} => Some("{{ codes[id].full_name }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns the short name of this country in it's own administrative language. #[cfg(feature = "local_names")] #[allow(clippy::match_single_binding)] pub const fn local_short_name(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].local_short_name %} Self::{{ id }} => Some("{{ codes[id].local_short_name }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns the full name of this country in it's own administrative language. #[cfg(all(feature = "local_names", feature = "full_name"))] #[allow(clippy::match_single_binding)] pub const fn local_full_name(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].local_full_name %} Self::{{ id }} => Some("{{ codes[id].local_full_name }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns a list of recognized [LanguageCode]s for this country. #[cfg(feature = "languages")] pub const fn languages(&self) -> Option<&[LanguageCode]> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].languages %} Self::{{ id }} => Some(&[{% for language in codes[id].languages %}LanguageCode::{{ language | capitalize }},{% endfor %}]),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns the LanguageCode for the country's administrative language. #[cfg(feature = "languages")] pub const fn administrative_language(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].administrative_language %} Self::{{ id }} => Some(LanguageCode::{{ codes[id].administrative_language | capitalize }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// If this code were formerly assigned to another country, this returns /// the short name of the former country. #[cfg(feature = "formerly")] pub const fn former_short_name(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].former_short_name %} Self::{{ id }} => Some("{{ codes[id].former_short_name }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// If this code were formerly assigned to another country, this returns /// the three-letter country code of the former country. #[cfg(feature = "formerly")] pub const fn former_alpha_3_code(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].former_alpha_3_code %} Self::{{ id }} => Some("{{ codes[id].former_alpha_3_code }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} {{ "}" }}