Struct actix_web::http::header::LanguageTag

source ·
pub struct LanguageTag { /* private fields */ }
Expand description

A language tag as described in RFC 5646.

Language tags are used to help identify languages, whether spoken, written, signed, or otherwise signaled, for the purpose of communication. This includes constructed and artificial languages but excludes languages not intended primarily for human communication, such as programming languages.

Implementations§

source§

impl LanguageTag

source

pub fn as_str(&self) -> &str

Return the serialization of this language tag.

This is fast since that serialization is already stored in the LanguageTag struct.

source

pub fn into_string(self) -> String

Return the serialization of this language tag.

This consumes the LanguageTag and takes ownership of the String stored in it.

source

pub fn primary_language(&self) -> &str

Return the primary language subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.primary_language(), "zh");
source

pub fn extended_language(&self) -> Option<&str>

Return the extended language subtags.

Valid language tags have at most one extended language.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.extended_language(), Some("cmn"));
source

pub fn extended_language_subtags(&self) -> impl Iterator<Item = &str>

Iterate on the extended language subtags.

Valid language tags have at most one extended language.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.extended_language_subtags().collect::<Vec<_>>(), vec!["cmn"]);
source

pub fn full_language(&self) -> &str

Return the primary language subtag and its extended language subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.full_language(), "zh-cmn");
source

pub fn script(&self) -> Option<&str>

Return the script subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.script(), Some("Hans"));
source

pub fn region(&self) -> Option<&str>

Return the region subtag.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-cmn-Hans-CN").unwrap();
assert_eq!(language_tag.region(), Some("CN"));
source

pub fn variant(&self) -> Option<&str>

Return the variant subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-Latn-TW-pinyin").unwrap();
assert_eq!(language_tag.variant(), Some("pinyin"));
source

pub fn variant_subtags(&self) -> impl Iterator<Item = &str>

Iterate on the variant subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("zh-Latn-TW-pinyin").unwrap();
assert_eq!(language_tag.variant_subtags().collect::<Vec<_>>(), vec!["pinyin"]);
source

pub fn extension(&self) -> Option<&str>

Return the extension subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-DE-u-co-phonebk").unwrap();
assert_eq!(language_tag.extension(), Some("u-co-phonebk"));
source

pub fn extension_subtags(&self) -> impl Iterator<Item = (char, &str)>

Iterate on the extension subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-DE-u-co-phonebk").unwrap();
assert_eq!(language_tag.extension_subtags().collect::<Vec<_>>(), vec![('u', "co-phonebk")]);
source

pub fn private_use(&self) -> Option<&str>

Return the private use subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-x-foo-bar").unwrap();
assert_eq!(language_tag.private_use(), Some("x-foo-bar"));
source

pub fn private_use_subtags(&self) -> impl Iterator<Item = &str>

Iterate on the private use subtags.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("de-x-foo-bar").unwrap();
assert_eq!(language_tag.private_use_subtags().collect::<Vec<_>>(), vec!["foo", "bar"]);
source

pub fn parse(input: &str) -> Result<LanguageTag, ParseError>

Create a LanguageTag from its serialization.

This parser accepts the language tags that are “well-formed” according to RFC 5646. Full validation could be done with the validate method.

use language_tags::LanguageTag;

let language_tag = LanguageTag::parse("en-us").unwrap();
assert_eq!(language_tag.into_string(), "en-US")
§Errors

If the language tag is not “well-formed” a ParseError variant will be returned.

source

pub fn validate(&self) -> Result<(), ValidationError>

Check if the language tag is “valid” according to RFC 5646.

It applies the following steps:

  • grandfathereds and private use tags are valid
  • There should be no more than one extended language subtag (c.f. errata 5457).
  • Primary language, extended language, script, region and variants should appear in the IANA Language Subtag Registry.
  • Extended language and variants should have a correct prefix as set in the IANA Language Subtag Registry.
  • There should be no duplicate variant and singleton (extension) subtags.
§Errors

If the language tag is not “valid” a ValidationError variant will be returned.

source

pub fn is_valid(&self) -> bool

Check if the language tag is valid according to RFC 5646.

source

pub fn canonicalize(&self) -> Result<LanguageTag, ValidationError>

Returns the canonical version of the language tag following RFC 5646 4.5.

It currently applies the following steps:

  • Grandfathered tags are replaced with the canonical version if possible.
  • Redundant tags are replaced with the canonical version if possible.
  • Extension languages are promoted to primary language.
  • Deprecated languages, scripts, regions and variants are replaced with modern equivalents.
  • Suppress-Script is applied to remove default script for a language (e.g. “en-Latn” is canonicalized as “en”).
  • Variants are deduplicated
§Errors

If there is not a unique way to canonicalize the language tag a ValidationError variant will be returned.

source

pub fn matches(&self, other: &LanguageTag) -> bool

Matches language tags. The first language acts as a language range, the second one is used as a normal language tag. None fields in the language range are ignored. If the language tag has more extlangs than the range these extlangs are ignored. Matches are case-insensitive.

For example the range en-GB matches only en-GB and en-Arab-GB but not en. The range en matches all language tags starting with en including en, en-GB, en-Arab and en-Arab-GB.

§Panics

If the language range has extensions or private use tags.

§Examples
use language_tags::LanguageTag;

let range_italian = LanguageTag::parse("it").unwrap();
let tag_german = LanguageTag::parse("de").unwrap();
let tag_italian_switzerland = LanguageTag::parse("it-CH").unwrap();
assert!(!range_italian.matches(&tag_german));
assert!(range_italian.matches(&tag_italian_switzerland));

let range_spanish_brazil = LanguageTag::parse("es-BR").unwrap();
let tag_spanish = LanguageTag::parse("es").unwrap();
assert!(!range_spanish_brazil.matches(&tag_spanish));
source

pub fn is_language_range(&self) -> bool

Checks if it is a language range, meaning that there are no extension and privateuse tags.

Trait Implementations§

source§

impl Clone for LanguageTag

source§

fn clone(&self) -> LanguageTag

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 Debug for LanguageTag

source§

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

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

impl Display for LanguageTag

source§

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

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

impl FromStr for LanguageTag

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(input: &str) -> Result<LanguageTag, ParseError>

Parses a string s to return a value of this type. Read more
source§

impl Hash for LanguageTag

source§

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

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 PartialEq for LanguageTag

source§

fn eq(&self, other: &LanguageTag) -> 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 Eq for LanguageTag

source§

impl StructuralPartialEq for LanguageTag

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more