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>

source

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());
source

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");
source

pub fn value(&self) -> &str

Return a reference of the inner value.

source

pub fn into_inner(self) -> String

Convert the type into a String.

Trait Implementations§

source§

impl<'a> Clone for LanguageId<'a>

source§

fn clone(&self) -> LanguageId<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for LanguageId<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for LanguageId<'de>

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a> Hash for LanguageId<'a>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> PartialEq<LanguageId<'a>> for LanguageId<'a>

source§

fn eq(&self, other: &LanguageId<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for LanguageId<'_>

Available on crate feature serde only.
source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a> Eq for LanguageId<'a>

source§

impl<'a> StructuralEq for LanguageId<'a>

source§

impl<'a> StructuralPartialEq for LanguageId<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for LanguageId<'a>

§

impl<'a> Send for LanguageId<'a>

§

impl<'a> Sync for LanguageId<'a>

§

impl<'a> Unpin for LanguageId<'a>

§

impl<'a> UnwindSafe for LanguageId<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.