use codes_agency::{Agency, Standard, standardized_type}; use codes_common::{code_impl, error, fixed_length_code}; use crate::LanguageCodeError; use crate::part_1::LanguageCode as Part1; use std::str::FromStr; // ------------------------------------------------------------------------------------------------ // 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-3 specification. /// pub const ISO_639_3: Standard = Standard::new_with_long_ref( Agency::ISO, "639-3", "ISO 639-3:2007", "Codes for the representation of names of languages — Part 3: Alpha-3 code for comprehensive coverage of languages", "https://www.iso.org/standard/", ); /// /// A Language Code enumeration representing the three-letter /// 639-3 identifier. /// #[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].ref_name }} {{ 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 %} ]; // ------------------------------------------------------------------------------------------------ // Public Functions // ------------------------------------------------------------------------------------------------ /// /// The code `mis` (uncoded languages, originally an abbreviation for /// 'miscellaneous') is intended for languages which have not (yet) been /// included in the ISO standard. /// pub fn special_for_uncoded_languages() -> {{ type_name }} {{ "{" }} {{ type_name }}::Mis {{ "}" }} /// /// The code `mul` (multiple languages) is intended for cases where the /// data includes more than one language, and (for example) the database /// requires a single ISO code. /// pub fn special_for_multiple_languages() -> {{ type_name }} {{ "{" }} {{ type_name }}::Mul {{ "}" }} /// /// The code `und` (undetermined) is intended for cases where the language /// in the data has not been identified, such as when it is mislabeled /// or never had been labeled. It is not intended for cases such as /// Trojan where an unattested language has been given a name. /// pub fn special_for_undetermined_language() -> {{ type_name }} {{ "{" }} {{ type_name }}::Und {{ "}" }} /// /// The code `zxx` (no linguistic content / not applicable) is intended /// for data which is not a language at all, such as animal calls. /// pub fn special_for_no_linguistic_content() -> {{ type_name }} {{ "{" }} {{ type_name }}::Zxx {{ "}" }} // ------------------------------------------------------------------------------------------------ // Implementations // ------------------------------------------------------------------------------------------------ impl FromStr for {{ type_name }} {{ "{" }} type Err = {{ type_name }}Error; fn from_str(s: &str) -> Result::Err> {{ "{" }} 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_3); impl {{ type_name }} {{ "{" }} /// /// Returns the ISO 639-3 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 corresponding ISO 639-1 two-letter code, if one exists. /// pub fn part_1_code(&self) -> Option {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].part_1_code %} Self::{{ id | capitalize }} => Some(Part1::{{ codes[id].part_1_code | capitalize }}),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// /// Returns the name (called "ref name" in the standard) /// of this language. /// pub fn language_name(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id | capitalize }} => "{{ codes[id].ref_name }}",{% endfor %} {{ "}" }} {{ "}" }} /// /// Return the scope defined for this language. /// #[cfg(feature = "scope")] pub fn scope(&self) -> LanguageScope {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id | capitalize }} => LanguageScope::{{ codes[id].scope }},{% endfor %} {{ "}" }} {{ "}" }} /// /// Return the language type defined for this language. /// #[cfg(feature = "language_type")] pub fn language_type(&self) -> LanguageType {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self::{{ id | capitalize }} => LanguageType::{{ codes[id].language_type }},{% endfor %} {{ "}" }} {{ "}" }} /// /// Return any comment included in the registration for this language. /// #[cfg(feature = "comment")] pub fn comment(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].comment %} Self::{{ id | capitalize }} => Some("{{ codes[id].comment }}"),{% endif %}{% endfor %} _ => None {{ "}" }} {{ "}" }} /// /// If this language is a macro language, return a list of individuals. /// #[cfg(feature = "macro_individuals")] pub fn macro_individuals(&self) -> Option<&[LanguageCode]> {{ "{" }} match self {{ "{" }}{% for code, individuals in macro_langs %} Self::{{ code | capitalize }} => Some(&[{% for code in individuals %}{{ type_name }}::{{ code | capitalize }}, {% endfor %} ]),{% endfor %} _ => None {{ "}" }} {{ "}" }} {{ "}" }}