Struct rosetta_i18n::LanguageId
source · pub struct LanguageId<'a>(_);
Expand description
ISO 639-1 language identifier.
This type holds a string representing a language in the ISO 693-1 format (two-letter code).
The inner value is stored in a Cow
to avoid allocation when possible.
Validation
The type inner value is not validated unless the validate
method is used to initialize the instance.
Generally, you should use this method to initialize this type.
The performed validation only checks that the provided looks like an ISO 693-1language identifier (2 character alphanumeric ascii string).
Serde support
This type implements the Serialize
and Deserialize
traits if the serde
feature is enabled.
Deserialization will fail if the value is not an ISO 639-1 language identifier.
Example
use rosetta_i18n::LanguageId;
let language_id = LanguageId::new("fr");
assert_eq!(language_id.value(), "fr");
let language_id = LanguageId::validate("fr");
assert!(language_id.is_some());
Implementations§
source§impl<'a> LanguageId<'a>
impl<'a> LanguageId<'a>
sourcepub fn validate(value: &str) -> Option<Self>
pub fn validate(value: &str) -> Option<Self>
Initialize a new valid LanguageId
.
Unlike new
, this method ensures that the provided
value is a valid ISO 693-1 encoded language id.
assert!(LanguageId::validate("fr").is_some());
assert!(LanguageId::validate("invalid").is_none());
sourcepub fn new(value: impl Into<Cow<'a, str>>) -> Self
pub fn new(value: impl Into<Cow<'a, str>>) -> Self
Initialize a new LanguageId
from a string.
The provided value must be an ISO 693-1 encoded language id.
If you want to validate the value, use validate
instead.
let language_id = LanguageId::new("en");
assert_eq!(language_id.value(), "en");
sourcepub fn into_inner(self) -> String
pub fn into_inner(self) -> String
Convert the type into a String
.
Trait Implementations§
source§impl<'a> Clone for LanguageId<'a>
impl<'a> Clone for LanguageId<'a>
source§fn clone(&self) -> LanguageId<'a>
fn clone(&self) -> LanguageId<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for LanguageId<'a>
impl<'a> Debug for LanguageId<'a>
source§impl<'de> Deserialize<'de> for LanguageId<'de>
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for LanguageId<'de>
serde
only.source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<'a> Hash for LanguageId<'a>
impl<'a> Hash for LanguageId<'a>
source§impl<'a> PartialEq<LanguageId<'a>> for LanguageId<'a>
impl<'a> PartialEq<LanguageId<'a>> for LanguageId<'a>
source§fn eq(&self, other: &LanguageId<'a>) -> bool
fn eq(&self, other: &LanguageId<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for LanguageId<'_>
Available on crate feature serde
only.
impl Serialize for LanguageId<'_>
serde
only.