Struct Keywords

struct Keywords(_)

A list of Key-Value pairs representing functional information about locale's internationalization preferences.

Here are examples of fields used in Unicode:

You can find the full list in Unicode BCP 47 U Extension section of LDML.

Examples

Manually build up a Keywords object:

use icu::locale::extensions::unicode::{key, value, Keywords};

let keywords = [(key!("hc"), value!("h23"))]
    .into_iter()
    .collect::<Keywords>();

assert_eq!(&keywords.to_string(), "hc-h23");

Access a Keywords object from a Locale:

use icu::locale::{
    extensions::unicode::{key, value},
    Locale,
};

let loc: Locale = "und-u-hc-h23-kc-true".parse().expect("Valid BCP-47");

assert_eq!(loc.extensions.unicode.keywords.get(&key!("ca")), None);
assert_eq!(
    loc.extensions.unicode.keywords.get(&key!("hc")),
    Some(&value!("h23"))
);
assert_eq!(
    loc.extensions.unicode.keywords.get(&key!("kc")),
    Some(&value!("true"))
);

assert_eq!(loc.extensions.unicode.keywords.to_string(), "hc-h23-kc");

Implementations

impl Keywords

fn to_string(self: &Self) -> String

Converts the given value to a String.

Under the hood, this uses an efficient Writeable implementation. However, in order to avoid allocating a string, it is more efficient to use Writeable directly.

impl Keywords

const fn new() -> Self

Returns a new empty list of key-value pairs. Same as default(), but is const.

Examples

use icu::locale::extensions::unicode::Keywords;

assert_eq!(Keywords::new(), Keywords::default());
const fn new_single(key: Key, value: Value) -> Self

Create a new list of key-value pairs having exactly one pair, callable in a const context.

fn is_empty(self: &Self) -> bool

Returns true if there are no keywords.

Examples

use icu::locale::locale;
use icu::locale::Locale;

let loc1 = Locale::try_from_str("und-t-h0-hybrid").unwrap();
let loc2 = locale!("und-u-ca-buddhist");

assert!(loc1.extensions.unicode.keywords.is_empty());
assert!(!loc2.extensions.unicode.keywords.is_empty());
fn contains_key<Q>(self: &Self, key: &Q) -> bool
where
    Key: Borrow<Q>,
    Q: Ord

Returns true if the list contains a Value for the specified Key.

Examples

use icu::locale::extensions::unicode::{key, value, Keywords};

let keywords = [(key!("ca"), value!("gregory"))]
    .into_iter()
    .collect::<Keywords>();

assert!(&keywords.contains_key(&key!("ca")));
fn get<Q>(self: &Self, key: &Q) -> Option<&Value>
where
    Key: Borrow<Q>,
    Q: Ord

Returns a reference to the Value corresponding to the Key.

Examples

use icu::locale::extensions::unicode::{key, value, Keywords};

let keywords = [(key!("ca"), value!("buddhist"))]
    .into_iter()
    .collect::<Keywords>();

assert_eq!(keywords.get(&key!("ca")), Some(&value!("buddhist")));
fn clear(self: &mut Self) -> Self

Clears all Unicode extension keywords, leaving Unicode attributes.

Returns the old Unicode extension keywords.

Examples

use icu::locale::Locale;

let mut loc: Locale = "und-u-hello-ca-buddhist-hc-h12".parse().unwrap();
loc.extensions.unicode.keywords.clear();
assert_eq!(loc, "und-u-hello".parse().unwrap());
fn strict_cmp(self: &Self, other: &[u8]) -> Ordering

Compare this Keywords with BCP-47 bytes.

The return value is equivalent to what would happen if you first converted this Keywords to a BCP-47 string and then performed a byte comparison.

This function is case-sensitive and results in a total order, so it is appropriate for binary search. The only argument producing Ordering::Equal is self.to_string().

Examples

use icu::locale::Locale;
use std::cmp::Ordering;

let bcp47_strings: &[&str] =
    &["ca-hebrew", "ca-japanese", "ca-japanese-nu-latn", "nu-latn"];

for ab in bcp47_strings.windows(2) {
    let a = ab[0];
    let b = ab[1];
    assert!(a.cmp(b) == Ordering::Less);
    let a_kwds = format!("und-u-{}", a)
        .parse::<Locale>()
        .unwrap()
        .extensions
        .unicode
        .keywords;
    assert!(a_kwds.strict_cmp(a.as_bytes()) == Ordering::Equal);
    assert!(a_kwds.strict_cmp(b.as_bytes()) == Ordering::Less);
}
fn iter(self: &Self) -> impl Iterator<Item = (&Key, &Value)>

Produce an ordered iterator over key-value pairs

impl Clone for Keywords

fn clone(self: &Self) -> Keywords

impl Debug for Keywords

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

impl Default for Keywords

fn default() -> Keywords

impl Display for Keywords

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

impl Eq for Keywords

impl Freeze for Keywords

impl From for Keywords

fn from(map: LiteMap<Key, Value, ShortBoxSlice<(Key, Value)>>) -> Self

impl Hash for Keywords

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

impl Ord for Keywords

fn cmp(self: &Self, other: &Keywords) -> Ordering

impl PartialEq for Keywords

fn eq(self: &Self, other: &Keywords) -> bool

impl PartialOrd for Keywords

fn partial_cmp(self: &Self, other: &Keywords) -> Option<Ordering>

impl RefUnwindSafe for Keywords

impl Send for Keywords

impl StructuralPartialEq for Keywords

impl Sync for Keywords

impl Unpin for Keywords

impl UnsafeUnpin for Keywords

impl UnwindSafe for Keywords

impl Writeable for Keywords

fn write_to<W: core::fmt::Write + ?Sized>(self: &Self, sink: &mut W) -> Result
fn writeable_length_hint(self: &Self) -> LengthHint

impl<T> Any for Keywords

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Keywords

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Keywords

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

impl<T> CloneToUninit for Keywords

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> ErasedDestructor for Keywords

impl<T> From for Keywords

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Keywords

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for Keywords

fn to_string(self: &Self) -> String

impl<T, U> Into for Keywords

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Keywords

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

impl<T, U> TryInto for Keywords

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