#[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:
SinglePlaceholder
for patterns with up to one placeholder:"{0} days ago"
DoublePlaceholder
for patterns with up to two placeholders:"{0} days, {1} hours ago"
MultiNamedPlaceholder
for patterns with named placeholders:"{name} sent you a message"
§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,
impl<B> Pattern<B>where
B: PatternBackend,
sourcepub fn try_from_items<'a, I>(items: I) -> Result<Box<Pattern<B>>, PatternError>
pub fn try_from_items<'a, I>(items: I) -> Result<Box<Pattern<B>>, PatternError>
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>where
B: PatternBackend,
<B as PatternBackend>::PlaceholderKeyCow<'a>: FromStr,
<<B as PatternBackend>::PlaceholderKeyCow<'a> as FromStr>::Err: Debug,
impl<'a, B> Pattern<B>where
B: PatternBackend,
<B as PatternBackend>::PlaceholderKeyCow<'a>: FromStr,
<<B as PatternBackend>::PlaceholderKeyCow<'a> as FromStr>::Err: Debug,
sourcepub fn try_from_str(
pattern: &str,
options: ParserOptions,
) -> Result<Box<Pattern<B>>, PatternError>
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,
impl<B> Pattern<B>where
B: PatternBackend,
sourcepub fn iter(
&self,
) -> impl Iterator<Item = PatternItem<'_, <B as PatternBackend>::PlaceholderKey<'_>>>
pub fn iter( &self, ) -> impl Iterator<Item = PatternItem<'_, <B as PatternBackend>::PlaceholderKey<'_>>>
Returns an iterator over the PatternItem
s in this pattern.
sourcepub fn try_interpolate<'a, P>(
&'a self,
value_provider: P,
) -> impl TryWriteable<Error = <B as PatternBackend>::Error<'a>> + Display + 'awhere
P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,
pub fn try_interpolate<'a, P>(
&'a self,
value_provider: P,
) -> impl TryWriteable<Error = <B as PatternBackend>::Error<'a>> + Display + 'awhere
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.
sourcepub 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,
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>,
impl<B> Pattern<B>where
B: for<'b> PatternBackend<Error<'b> = Infallible>,
sourcepub fn interpolate<'a, P>(
&'a self,
value_provider: P,
) -> impl Writeable + Display + 'awhere
P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,
pub fn interpolate<'a, P>(
&'a self,
value_provider: P,
) -> impl Writeable + Display + 'awhere
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.
sourcepub fn interpolate_to_string<'a, P>(&'a self, value_provider: P) -> Stringwhere
P: PlaceholderValueProvider<<B as PatternBackend>::PlaceholderKey<'a>, Error = <B as PatternBackend>::Error<'a>> + 'a,
pub fn interpolate_to_string<'a, P>(&'a self, value_provider: P) -> Stringwhere
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> Clone for Box<Pattern<B>>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: for<'a> From<&'a <B as PatternBackend>::Store>,
impl<B> Clone for Box<Pattern<B>>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: for<'a> From<&'a <B as PatternBackend>::Store>,
source§impl<B> Debug for Pattern<B>where
B: PatternBackend,
impl<B> Debug for Pattern<B>where
B: PatternBackend,
source§impl<B> Default for &'static Pattern<B>where
B: PatternBackend,
impl<B> Default for &'static Pattern<B>where
B: PatternBackend,
source§impl<B> Default for Box<Pattern<B>>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: From<&'static <B as PatternBackend>::Store>,
impl<B> Default for Box<Pattern<B>>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: From<&'static <B as PatternBackend>::Store>,
source§impl<'de, 'data, B> Deserialize<'de> for &'data Pattern<B>
impl<'de, 'data, B> Deserialize<'de> for &'data Pattern<B>
source§fn deserialize<D>(
deserializer: D,
) -> Result<&'data Pattern<B>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<&'data Pattern<B>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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>,
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>,
fn deserialize<D>(
deserializer: D,
) -> Result<Box<Pattern<B>>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl<B> PartialEq for Pattern<B>where
B: PatternBackend,
impl<B> PartialEq for Pattern<B>where
B: PatternBackend,
source§impl<B> Serialize for Pattern<B>where
B: PatternBackend,
<B as PatternBackend>::Store: Serialize,
<B as PatternBackend>::PlaceholderKeyCow<'a>: for<'a> Serialize + for<'a> From<<B as PatternBackend>::PlaceholderKey<'a>>,
impl<B> Serialize for Pattern<B>where
B: PatternBackend,
<B as PatternBackend>::Store: Serialize,
<B as PatternBackend>::PlaceholderKeyCow<'a>: for<'a> Serialize + for<'a> From<<B as PatternBackend>::PlaceholderKey<'a>>,
source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
source§impl<B> ToOwned for Pattern<B>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: for<'a> From<&'a <B as PatternBackend>::Store>,
impl<B> ToOwned for Pattern<B>where
B: PatternBackend,
Box<<B as PatternBackend>::Store>: for<'a> From<&'a <B as PatternBackend>::Store>,
source§impl<B, S> VarULE for Pattern<B>
impl<B, S> VarULE for Pattern<B>
Implement VarULE
for Pattern<SinglePlaceholder, str>
.
§Safety
Safety checklist for ULE
:
Pattern<B>
does not include any uninitialized or padding bytes.Pattern<B>
is aligned to 1 byte.- The implementation of
validate_bytes()
returns an error if any byte is not valid. - The implementation of
validate_bytes()
returns an error if the slice cannot be used to build aPattern<B>
in its entirety. - The implementation of
from_bytes_unchecked()
returns a reference to the same data. parse_bytes()
is equivalent tovalidate_bytes()
followed byfrom_bytes_unchecked()
.Pattern<B>
byte equality is semantic equality.
source§fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>
&[u8]
. Read moresource§unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Pattern<B>
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Pattern<B>
&[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 moresource§impl<'a, B> Yokeable<'a> for Pattern<B>
impl<'a, B> Yokeable<'a> for Pattern<B>
source§type Output = Pattern<B>
type Output = Pattern<B>
Self
with the 'static
replaced with 'a
, i.e. Self<'a>
Auto Trait Implementations§
impl<B> Freeze for Pattern<B>
impl<B> RefUnwindSafe for Pattern<B>
impl<B> Send for Pattern<B>
impl<B> Sync for Pattern<B>
impl<B> Unpin for Pattern<B>
impl<B> UnwindSafe for Pattern<B>
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> EncodeAsVarULE<T> for T
impl<T> EncodeAsVarULE<T> for T
source§fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
cb
with a piecewise list of byte slices that when concatenated
produce the memory pattern of the corresponding instance of T
. Read moresource§fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_len(&self) -> usize
VarULE
typesource§fn encode_var_ule_write(&self, dst: &mut [u8])
fn encode_var_ule_write(&self, dst: &mut [u8])
VarULE
type to the dst
buffer. dst
should
be the size of Self::encode_var_ule_len()
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 more