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,
impl<B> Pattern<B>where
B: PatternBackend,
Sourcepub fn try_from_items<'a, I>(items: I) -> Result<Box<Self>, Error>
pub fn try_from_items<'a, I>(items: I) -> Result<Box<Self>, Error>
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::PlaceholderKeyCow<'a>: FromStr,
<B::PlaceholderKeyCow<'a> as FromStr>::Err: Debug,
impl<'a, B> Pattern<B>where
B: PatternBackend,
B::PlaceholderKeyCow<'a>: FromStr,
<B::PlaceholderKeyCow<'a> as FromStr>::Err: Debug,
Sourcepub fn try_from_str(
pattern: &str,
options: ParserOptions,
) -> Result<Box<Self>, Error>
pub fn try_from_str( pattern: &str, options: ParserOptions, ) -> Result<Box<Self>, Error>
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::PlaceholderKey<'_>>> + '_
pub fn iter( &self, ) -> impl Iterator<Item = PatternItem<'_, B::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::Error<'a>> + Display + 'a
pub fn try_interpolate<'a, P>( &'a self, value_provider: P, ) -> impl TryWriteable<Error = B::Error<'a>> + Display + '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::Error<'a>, String)>
pub fn try_interpolate_to_string<'a, P>( &'a self, value_provider: P, ) -> Result<String, (B::Error<'a>, String)>
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
for<'b> B: PatternBackend<Error<'b> = Infallible>,
impl<B> Pattern<B>where
for<'b> B: PatternBackend<Error<'b> = Infallible>,
Sourcepub fn interpolate<'a, P>(
&'a self,
value_provider: P,
) -> impl Writeable + Display + 'a
pub fn interpolate<'a, P>( &'a self, value_provider: P, ) -> impl Writeable + Display + '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) -> String
pub fn interpolate_to_string<'a, P>(&'a self, value_provider: P) -> String
Interpolates the pattern directly to a string.
✨ Enabled with the alloc
Cargo feature.
Trait Implementations
Source§impl<B: PatternBackend> Debug for Pattern<B>
impl<B: PatternBackend> Debug for Pattern<B>
Source§impl<B: PatternBackend> PartialEq for Pattern<B>
impl<B: PatternBackend> PartialEq for Pattern<B>
Source§impl<B: PatternBackend> Serialize for Pattern<B>where
B::Store: Serialize,
for<'a> B::PlaceholderKeyCow<'a>: Serialize + From<B::PlaceholderKey<'a>>,
impl<B: PatternBackend> Serialize for Pattern<B>where
B::Store: Serialize,
for<'a> B::PlaceholderKeyCow<'a>: Serialize + From<B::PlaceholderKey<'a>>,
Source§impl<B: PatternBackend> ToOwned for Pattern<B>
impl<B: PatternBackend> ToOwned for Pattern<B>
Source§impl<B, S: ?Sized + VarULE> VarULE for Pattern<B>where
B: PatternBackend<Store = S>,
Implement VarULE
for Pattern<SinglePlaceholder, str>
.
impl<B, S: ?Sized + VarULE> VarULE for Pattern<B>where
B: PatternBackend<Store = S>,
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]) -> &Self
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
&[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§fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError>
fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError>
Source§impl<'a, B> Yokeable<'a> for Pattern<B>where
B: 'static + PatternBackend,
Self: Sized,
impl<'a, B> Yokeable<'a> for Pattern<B>where
B: 'static + PatternBackend,
Self: Sized,
Source§type Output = Pattern<B>
type Output = Pattern<B>
Self
with the 'static
replaced with 'a
, i.e. Self<'a>
Source§fn transform_owned(self) -> Self::Output
fn transform_owned(self) -> Self::Output
Source§unsafe fn make(this: Self::Output) -> Self
unsafe fn make(this: Self::Output) -> Self
Self<'a>
’s lifetime. Read moreSource§fn transform_mut<F>(&'a mut self, f: F)where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
fn transform_mut<F>(&'a mut self, f: F)where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
self
between &'a mut Self<'static>
and &'a mut Self<'a>
,
and pass it to f
. Read more