use codes_agency::{Agency, Standard, standardized_type}; use codes_common::{code_impl, error, fixed_length_code}; use crate::LanguageCodeError; use std::str::FromStr; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; // ------------------------------------------------------------------------------------------------ // 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-639-5 specification. /// pub const ISO_639_5: Standard = Standard::new_with_long_ref( Agency::ISO, "639-5", "ISO 639-5:2008", "Codes for the representation of names of languages — Part 5: Alpha-3 code for language families and groups", "https://www.iso.org/standard/39536.html", ); /// /// A Language Code enumeration representing the three-letter /// 639-5 identifier. /// /// ISO 639-5 defines alpha-3 (3-letter) codes, called "collective /// codes", that identify language families and groups. As of the /// February 11, 2013 update to ISO 639-5, the standard defines /// 115 collective codes. The United States Library of Congress /// maintains the list of Alpha-3 codes that comprise ISO 639-5. /// /// The standard does not cover all language families used by /// linguists. The languages covered by a group code need not be /// linguistically related, but may have a geographic relation, /// or category relation (such as Creoles). /// #[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].label }} {{ id | capitalize }},{% 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 | capitalize }},{% 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 | capitalize }}),{% endfor %} _ => Err(error::unknown_value("{{ type_name }}", s)), {{ "}" }} {{ "}" }} {{ "}" }} code_impl!({{ type_name }}); fixed_length_code!({{ type_name }}, 3); standardized_type!({{ type_name }}, ISO_639_5); impl {{ type_name }} {{ "{" }} /// /// Returns the ISO 639-5 three-letter code a string. /// pub fn code(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id | capitalize }} => "{{ id }}",{% endfor %} {{ "}" }} {{ "}" }} /// /// Returns the name of this language family or group. /// pub fn family_or_group_name(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id | capitalize }} => "{{ codes[id].label }}",{% endfor %} {{ "}" }} {{ "}" }} {{ "}" }}