pub struct MyLanguageTag(/* private fields */);
Expand description
A wrapper around LanguageTag to use it when Option<T>
, serialization
and deserialization are needed.
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§
Methods from Deref<Target = LanguageTag>§
Sourcepub fn as_str(&self) -> &str
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.
Sourcepub fn primary_language(&self) -> &str
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");
Sourcepub fn extended_language(&self) -> Option<&str>
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"));
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"]);
Sourcepub fn full_language(&self) -> &str
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");
Sourcepub fn script(&self) -> Option<&str>
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"));
Sourcepub fn region(&self) -> Option<&str>
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"));
Sourcepub fn variant(&self) -> Option<&str>
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"));
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"]);
Sourcepub fn extension(&self) -> Option<&str>
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"));
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")]);
Sourcepub fn private_use(&self) -> Option<&str>
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"));
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"]);
Sourcepub fn validate(&self) -> Result<(), ValidationError>
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.
Sourcepub fn canonicalize(&self) -> Result<LanguageTag, ValidationError>
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.
Sourcepub fn matches(&self, other: &LanguageTag) -> bool
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));
Sourcepub fn is_language_range(&self) -> bool
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 MyLanguageTag
impl Clone for MyLanguageTag
Source§fn clone(&self) -> MyLanguageTag
fn clone(&self) -> MyLanguageTag
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for MyLanguageTag
impl Debug for MyLanguageTag
Source§impl Deref for MyLanguageTag
impl Deref for MyLanguageTag
Source§impl DerefMut for MyLanguageTag
impl DerefMut for MyLanguageTag
Source§impl<'de> Deserialize<'de> for MyLanguageTag
impl<'de> Deserialize<'de> for MyLanguageTag
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 Display for MyLanguageTag
impl Display for MyLanguageTag
Source§impl From<LanguageTag> for MyLanguageTag
impl From<LanguageTag> for MyLanguageTag
Source§fn from(value: LanguageTag) -> Self
fn from(value: LanguageTag) -> Self
Source§impl FromStr for MyLanguageTag
impl FromStr for MyLanguageTag
Source§impl Ord for MyLanguageTag
impl Ord for MyLanguageTag
Source§impl PartialEq<MyLanguageTag> for &MyLanguageTag
impl PartialEq<MyLanguageTag> for &MyLanguageTag
Source§impl PartialEq<String> for MyLanguageTag
impl PartialEq<String> for MyLanguageTag
Source§impl PartialEq<str> for MyLanguageTag
impl PartialEq<str> for MyLanguageTag
Source§impl PartialEq for MyLanguageTag
impl PartialEq for MyLanguageTag
Source§impl PartialOrd for MyLanguageTag
impl PartialOrd for MyLanguageTag
Source§impl Serialize for MyLanguageTag
impl Serialize for MyLanguageTag
impl Eq for MyLanguageTag
impl StructuralPartialEq for MyLanguageTag
Auto Trait Implementations§
impl Freeze for MyLanguageTag
impl RefUnwindSafe for MyLanguageTag
impl Send for MyLanguageTag
impl Sync for MyLanguageTag
impl Unpin for MyLanguageTag
impl UnwindSafe for MyLanguageTag
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.