use std::str::FromStr; use codes_agency::{Agency, Standard, standardized_type}; use codes_common::{code_impl, error}; #[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-10383 specification. /// pub const IANA_CHARSET: Standard = Standard::new( Agency::IANA, "Charset", "Character Sets", "https://www.iana.org/assignments/character-sets/character-sets.xhtml", ); /// /// This type is used to encapsulate the numeric **MIB enum** for /// IANA-defined Character Sets. /// /// This integer code is used as the main identifier rather than the /// textual names as they have no common formatting. /// #[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 %} /// {{ codes[id].name }} pub const CHARSET_{{ 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 %} CHARSET_{{ id }},{% endfor %} ]; pub use codes_common::CodeParseError as {{ type_name }}Error; // ------------------------------------------------------------------------------------------------ // Implementations // ------------------------------------------------------------------------------------------------ impl FromStr for {{ type_name }} {{ "{" }} type Err = {{ type_name }}Error; fn from_str(s: &str) -> Result {{ "{" }} match s {{ "{" }}{% for id in all_ids %} "{{ codes[id].name }}" => Ok(Self({{ id }})),{% if codes[id].aliases %} {% for alias in codes[id].aliases %}"{{ alias }}" => Ok(Self({{ id }})),{% endfor %} {% endif %}{% endfor %} _ => Err(error::unknown_value("{{ type_name }}", s)), {{ "}" }} {{ "}" }} {{ "}" }} impl TryFrom<{{ inner_type_name }}> for {{ type_name }} {{ "{" }} type Error = {{ type_name }}Error; 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())), {{ "}" }} {{ "}" }} {{ "}" }} code_impl!({{ type_name }}, id, {{ inner_type_name }}); standardized_type!({{ type_name }}, IANA_CHARSET); impl {{ type_name }} {{ "{" }} /// /// Returns this character set identifier (MIB Enumeration Value). /// pub const fn id(&self) -> {{ inner_type_name }} {{ "{" }} self.0 {{ "}" }} /// /// Returns this character set's name as a string. /// pub const fn name(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self({{ id }}) => "{{ codes[id].name }}",{% endfor %} _ => unreachable!() {{ "}" }} {{ "}" }} /// /// Returns this character set's source as a string. /// pub const fn source(&self) -> &'static str {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self({{ id }}) => r##"{{ codes[id].source }}"##,{% endfor %} _ => unreachable!() {{ "}" }} {{ "}" }} /// /// Returns this character set's preferred MIME alias as a string. /// pub const fn preferred_alias(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].preferred_alias %} Self({{ id }}) => Some("{{ codes[id].preferred_alias }}"),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} /// /// Returns this character set's set of aliases as a list of strings. /// pub const fn aliases(&self) -> &[&'static str] {{ "{" }} match self {{ "{" }}{% for id in all_ids %} Self({{ id }}) => &[{% for alias in codes[id].aliases %}"{{ alias }}",{% endfor %}],{% endfor %} _ => unreachable!() {{ "}" }} {{ "}" }} /// /// Returns this character set's reference as a string. /// pub const fn reference(&self) -> Option<&'static str> {{ "{" }} match self {{ "{" }}{% for id in all_ids %}{% if codes[id].reference %} Self({{ id }}) => Some(r##"{{ codes[id].reference }}"##),{% endif %}{% endfor %} _ => None, {{ "}" }} {{ "}" }} {{ "}" }}