use codes_agency::standardized_type; use codes_common::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 // ------------------------------------------------------------------------------------------------ /// /// This type is used to encapsulate the numeric code for country /// subdivision categories. /// /// The standard identifies categories by country, so although a /// number of countries use the category *autonomous region* it is /// defined once per-country. /// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct {{ type_name }}({{ inner_type_name }}); {% for id in all_ids %} /// Category "{{ codes[id].name }}"{% if codes[id].country_alpha_2_code %}, defined by country `{{ codes[id].country_alpha_2_code }}`{% endif %}. pub const ISO_3166_CATEGORY_{{ id }}: {{ type_name }} = {{ type_name }}({{ 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 %} ISO_3166_CATEGORY_{{ 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)), {{ "}" }} {{ "}" }} {{ "}" }} impl From<{{ type_name }}> for {{ inner_type_name }} {{ "{" }} fn from(v: {{ type_name }}) -> Self {{ "{" }} v.0 {{ "}" }} {{ "}" }} impl TryFrom<{{ inner_type_name }}> for {{ type_name }} {{ "{" }} type Error = CountryCodeError; fn try_from(value: {{ inner_type_name }}) -> Result {{ "{" }} match value {{ "{" }}{% for id in all_ids %} {{ id }} => Ok(Self({{ id }})),{% endfor %} _ => Err(error::unknown_value("{{ type_name }}", value.to_string())), {{ "}" }} {{ "}" }} {{ "}" }} standardized_type!({{ type_name }}, crate::part_2::ISO_3166_2); impl {{ type_name }} {{ "{" }} /// Returns the numeric code for this category. pub const fn code(&self) -> {{ inner_type_name }} {{ "{" }} self.0 {{ "}" }} /// Returns the country code for the country defining this category. pub const fn country_code(&self) -> CountryCode {{ "{" }} match self.0 {{ "{" }}{% for id in all_ids %} {{ id }} => CountryCode::{{ codes[id].country_alpha_2_code }},{% endfor %} _ => unreachable!(), {{ "}" }} {{ "}" }} /// Return the language code corresponding to this category's name. pub const fn language_code(&self) -> LanguageCode {{ "{" }} match self.0 {{ "{" }}{% for id in all_ids %} {{ id }} => LanguageCode::{{ codes[id].language_alpha_3_code | capitalize }},{% endfor %} _ => unreachable!(), {{ "}" }} {{ "}" }} /// Return the name of this category. pub const fn name(&self) -> &'static str {{ "{" }} match self.0 {{ "{" }}{% for id in all_ids %} {{ id }} => "{{ codes[id].name }}",{% endfor %} _ => unreachable!(), {{ "}" }} {{ "}" }} /// Return the plural form, if different, of this category name. pub const fn name_plural(&self) -> Option<&'static str> {{ "{" }} match self.0 {{ "{" }}{% for id in all_ids %}{% if codes[id].name_plural %} {{ id }} => Some("{{ codes[id].name_plural }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} {{ "}" }}