use codes_agency::{Agency, Standard, standardized_type}; use codes_common::{code_impl, error}; use crate::CountryCodeError; use crate::part_1::CountryCode; 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-2 specification. /// pub const ISO_3166_2: Standard = Standard::new_with_long_ref( Agency::ISO, "3166-2", "ISO 3166-2:2020", "Codes for the representation of names of countries and their subdivisions - Part 2: Country subdivision code", "https://www.iso.org/standard/72483.html", ); /// /// This is a full enumeration of all the country subdivisions defined in /// ISO 3166-1. /// /// The standard's code is used as a variant name, in upper case, with the /// hyphen separator replaced with an underscore. /// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[allow(non_camel_case_types)] pub enum {{ type_name }} {{ "{" }}{% for id in all_ids %} /// {{ codes[id].code }}: {{ codes[id].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 = CountryCodeError; 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 }}); standardized_type!({{ type_name }}, ISO_3166_2); impl {{ type_name }} {{ "{" }} /// Returns the code for this national subdivision. pub const fn code(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => "{{ codes[id].code }}",{% endfor %} {{ "}" }} {{ "}" }} /// Returns the country code for the country in which this subdivision exists. pub const fn country_code(&self) -> CountryCode {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => CountryCode::{{ codes[id].country_alpha_2_code }},{% endfor %} {{ "}" }} {{ "}" }} /// Returns a country code if this subdivision also has a separate code /// in ISO 3166-1. pub const fn separate_country_code(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].separate_country_code %} Self::{{ id }} => Some(CountryCode::{{ codes[id].separate_country_code }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Return the category code for this subdivision. #[cfg(feature = "categories")] pub const fn category_code(&self) -> categories::SubdivisionCategoryCode {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => categories::ISO_3166_CATEGORY_{{ codes[id].category_code }},{% endfor %} {{ "}" }} {{ "}" }} /// Returns the parent subdivision if nested subdivisions are described. pub const fn parent_subdivision_code(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].parent_subdivision_code %} Self::{{ id }} => Some(Self::{{ codes[id].parent_subdivision_code }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// Returns the name of this subdivision pub const fn name(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id }} => "{{ codes[id].name }}",{% endfor %} {{ "}" }} {{ "}" }} /// Returns the language code in which the name of this subdivision is represented. #[cfg(feature = "languages")] pub const fn name_language(&self) -> LanguageCode {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].name_language %} Self::{{ id }} => LanguageCode::{{ codes[id].name_language | capitalize }},{% endif %}{% endfor %} {{ "}" }} {{ "}" }} /// Returns any local variation of this subdivision's name. pub const fn name_local_variation(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].name_local_variation %} Self::{{ id }} => Some("{{ codes[id].name_local_variation }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} {{ "}" }}