Struct icu::pattern::Pattern

source ·
#[repr(transparent)]
pub struct Pattern<B>
where B: PatternBackend,
{ pub store: <B as PatternBackend>::Store, /* private fields */ }
Expand description

A string pattern with placeholders.

There are 2 generic parameters: Backend and Store.

§Backend

This determines the nature of placeholders and serialized encoding of the pattern.

The following backends are available:

§Format to Parts

Pattern supports interpolating with writeable::Parts, annotations for whether the substring was a placeholder or a literal.

By default, the substrings are annotated with PATTERN_LITERAL_PART and PATTERN_PLACEHOLDER_PART. This can be customized with PlaceholderValueProvider.

§Examples

Interpolating a SinglePlaceholder pattern with parts:

use core::str::FromStr;
use icu_pattern::Pattern;
use icu_pattern::SinglePlaceholder;
use writeable::assert_writeable_parts_eq;

let pattern =
    Pattern::<SinglePlaceholder>::try_from_str("Hello, {0}!", Default::default()).unwrap();

assert_writeable_parts_eq!(
    pattern.interpolate(["Alice"]),
    "Hello, Alice!",
    [
        (0, 7, icu_pattern::PATTERN_LITERAL_PART),
        (7, 12, icu_pattern::PATTERN_PLACEHOLDER_PART),
        (12, 13, icu_pattern::PATTERN_LITERAL_PART),
    ]
);

Fields§

§store: <B as PatternBackend>::Store

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> Bake for &Pattern<B>
where B: PatternBackend, &'b <B as PatternBackend>::Store: for<'b> Bake,

source§

fn bake(&self, ctx: &CrateEnv) -> TokenStream

Returns a TokenStream that would evaluate to self. Read more
source§

impl<B> BakeSize for &Pattern<B>
where B: PatternBackend, &'b <B as PatternBackend>::Store: for<'b> BakeSize,

source§

fn borrows_size(&self) -> usize

Returns the size
source§

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

source§

fn clone(&self) -> Box<Pattern<B>>

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<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> Default for &'static Pattern<B>
where B: PatternBackend,

source§

fn default() -> &'static Pattern<B>

Returns the “default value” for a type. Read more
source§

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

source§

fn default() -> Box<Pattern<B>>

Returns the “default value” for a type. Read more
source§

impl<'de, 'data, B> Deserialize<'de> for &'data Pattern<B>
where 'de: 'data, B: PatternBackend, &'data <B as PatternBackend>::Store: Deserialize<'de>,

source§

fn deserialize<D>( deserializer: D, ) -> Result<&'data Pattern<B>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

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

impl<'de, 'data, B> Deserialize<'de> for Box<Pattern<B>>
where 'de: 'data, B: PatternBackend<Store = str>, <B as PatternBackend>::PlaceholderKeyCow<'data>: Deserialize<'de>,

source§

fn deserialize<D>( deserializer: D, ) -> Result<Box<Pattern<B>>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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

Auto Trait Implementations§

§

impl<B> Freeze for Pattern<B>
where <B as PatternBackend>::Store: Freeze,

§

impl<B> RefUnwindSafe for Pattern<B>

§

impl<B> Send for Pattern<B>
where <B as PatternBackend>::Store: Send, B: Send,

§

impl<B> Sync for Pattern<B>
where <B as PatternBackend>::Store: Sync, B: Sync,

§

impl<B> Unpin for Pattern<B>
where <B as PatternBackend>::Store: Unpin, B: Unpin,

§

impl<B> UnwindSafe for Pattern<B>

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> EncodeAsVarULE<T> for T
where T: VarULE + ?Sized,

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more
source§

fn encode_var_ule_len(&self) -> usize

Return the length, in bytes, of the corresponding VarULE type
source§

fn encode_var_ule_write(&self, dst: &mut [u8])

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len()
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

source§

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

source§

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>,

source§

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<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,