icu::pattern

Type Alias MultiNamedPlaceholderPattern

Source
pub type MultiNamedPlaceholderPattern = Pattern<MultiNamedPlaceholder>;
Expand description

§Examples

use core::str::FromStr;
use icu_pattern::MultiNamedPlaceholderPattern;
use std::collections::BTreeMap;
use writeable::assert_try_writeable_eq;

// Create a pattern from the string syntax:
let pattern = MultiNamedPlaceholderPattern::try_from_str(
    "Hello, {person0} and {person1}!",
    Default::default(),
)
.unwrap();

// Interpolate some values into the pattern:
assert_try_writeable_eq!(
    pattern.try_interpolate(
        [("person0", "Alice"), ("person1", "Bob")]
            .into_iter()
            .collect::<BTreeMap<&str, &str>>()
    ),
    "Hello, Alice and Bob!"
);

Aliased Type§

struct MultiNamedPlaceholderPattern {
    pub store: str,
    /* private fields */
}

Fields§

§store: str

The encoded storage

Implementations

Source§

impl<B> Pattern<B>
where B: PatternBackend,

Source

pub fn try_from_items<'a, I>(items: I) -> Result<Box<Pattern<B>>, PatternError>
where I: Iterator<Item = PatternItemCow<'a, <B as PatternBackend>::PlaceholderKeyCow<'a>>>,

Creates a pattern from an iterator of pattern items.

Enabled with the alloc Cargo feature.

§Examples
use icu_pattern::Pattern;
use icu_pattern::PatternItemCow;
use icu_pattern::SinglePlaceholder;
use icu_pattern::SinglePlaceholderKey;
use std::borrow::Cow;

Pattern::<SinglePlaceholder>::try_from_items(
    [
        PatternItemCow::Placeholder(SinglePlaceholderKey::Singleton),
        PatternItemCow::Literal(Cow::Borrowed(" days")),
    ]
    .into_iter(),
)
.expect("valid pattern items");
Source§

impl<'a, B> Pattern<B>

Source

pub fn try_from_str( pattern: &str, options: ParserOptions, ) -> Result<Box<Pattern<B>>, PatternError>

Creates a pattern by parsing a syntax string.

Enabled with the alloc Cargo feature.

§Examples
use icu_pattern::Pattern;
use icu_pattern::SinglePlaceholder;

// Create a pattern from a valid string:
Pattern::<SinglePlaceholder>::try_from_str("{0} days", Default::default())
    .expect("valid pattern");

// Error on an invalid pattern:
Pattern::<SinglePlaceholder>::try_from_str("{0 days", Default::default())
    .expect_err("mismatched braces");
Source§

impl<B> Pattern<B>
where B: PatternBackend,

Source

pub fn iter( &self, ) -> impl Iterator<Item = PatternItem<'_, <B as PatternBackend>::PlaceholderKey<'_>>>

Returns an iterator over the PatternItems in this pattern.

Source

pub fn try_interpolate<'a, P>( &'a self, value_provider: P, ) -> impl TryWriteable<Error = <B as PatternBackend>::Error<'a>> + Display + 'a
where P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,

Returns a TryWriteable that interpolates items from the given replacement provider into this pattern string.

Source

pub fn try_interpolate_to_string<'a, P>( &'a self, value_provider: P, ) -> Result<String, (<B as PatternBackend>::Error<'a>, String)>
where P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,

Interpolates the pattern directly to a string, returning the string or an error.

In addition to the error, the lossy fallback string is returned in the failure case.

Enabled with the alloc Cargo feature.

Source§

impl<B> Pattern<B>
where B: for<'b> PatternBackend<Error<'b> = Infallible>,

Source

pub fn interpolate<'a, P>( &'a self, value_provider: P, ) -> impl Writeable + Display + 'a
where P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,

Returns a Writeable that interpolates items from the given replacement provider into this pattern string.

Source

pub fn interpolate_to_string<'a, P>(&'a self, value_provider: P) -> String
where P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,

Interpolates the pattern directly to a string.

Enabled with the alloc Cargo feature.

Trait Implementations

Source§

impl<B> Debug for Pattern<B>
where B: PatternBackend,

Source§

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

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

impl<B> PartialEq for Pattern<B>
where B: PatternBackend,

Source§

fn eq(&self, other: &Pattern<B>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<B> Serialize for Pattern<B>

Source§

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

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

impl<B> ToOwned for Pattern<B>
where B: PatternBackend, Box<<B as PatternBackend>::Store>: for<'a> From<&'a <B as PatternBackend>::Store>,

Source§

type Owned = Box<Pattern<B>>

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> <Pattern<B> as ToOwned>::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

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

impl<B, S> VarULE for Pattern<B>
where S: VarULE + ?Sized, B: PatternBackend<Store = S>,

Implement VarULE for Pattern<SinglePlaceholder, str>.

§Safety

Safety checklist for ULE:

  1. Pattern<B> does not include any uninitialized or padding bytes.
  2. Pattern<B> is aligned to 1 byte.
  3. The implementation of validate_bytes() returns an error if any byte is not valid.
  4. The implementation of validate_bytes() returns an error if the slice cannot be used to build a Pattern<B> in its entirety.
  5. The implementation of from_bytes_unchecked() returns a reference to the same data.
  6. parse_bytes() is equivalent to validate_bytes() followed by from_bytes_unchecked().
  7. Pattern<B> byte equality is semantic equality.
Source§

fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>

Validates a byte slice, &[u8]. Read more
Source§

unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Pattern<B>

Takes a byte slice, &[u8], and return it as &Self with the same lifetime, assuming that this byte slice has previously been run through Self::parse_bytes() with success. Read more
Source§

fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError>

Parses a byte slice, &[u8], and return it as &Self with the same lifetime. Read more
Source§

fn as_bytes(&self) -> &[u8]

Given &Self, returns a &[u8] with the same lifetime. Read more
Source§

fn to_boxed(&self) -> Box<Self>

Allocate on the heap as a Box<T>
Source§

impl<'a, B> Yokeable<'a> for Pattern<B>
where B: PatternBackend + 'static, Pattern<B>: Sized,

Source§

type Output = Pattern<B>

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>
Source§

fn transform(&self) -> &<Pattern<B> as Yokeable<'a>>::Output

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more
Source§

fn transform_owned(self) -> <Pattern<B> as Yokeable<'a>>::Output

This method must cast self between Self<'static> and Self<'a>. Read more
Source§

unsafe fn make(this: <Pattern<B> as Yokeable<'a>>::Output) -> Pattern<B>

This method can be used to cast away Self<'a>’s lifetime. Read more
Source§

fn transform_mut<F>(&'a mut self, f: F)
where F: 'static + for<'b> FnOnce(&'b mut <Pattern<B> as Yokeable<'a>>::Output),

This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. Read more
Source§

impl<'a, B> ZeroMapKV<'a> for Pattern<B>

Source§

type Container = VarZeroVec<'a, Pattern<B>>

The container that can be used with this type: ZeroVec or VarZeroVec.
Source§

type Slice = VarZeroSlice<Pattern<B>>

Source§

type GetType = Pattern<B>

The type produced by Container::get() Read more
Source§

type OwnedType = Box<Pattern<B>>

The type produced by Container::replace() and Container::remove(), also used during deserialization. If Self is human readable serialized, deserializing to Self::OwnedType should produce the same value once passed through Self::owned_as_self() Read more