pub trait PlaceholderValueProvider<K> {
type Error;
type W<'a>: TryWriteable<Error = Self::Error>
where Self: 'a;
type L<'a, 'l>: Writeable
where Self: 'a;
// Required methods
fn value_for(&self, key: K) -> Self::W<'_>;
fn map_literal<'a, 'l>(&'a self, literal: &'l str) -> Self::L<'a, 'l>;
}
Expand description
Trait implemented on collections that can produce TryWriteable
s for interpolation.
This trait can add Part
s for individual literals or placeholders. The implementations
of this trait on standard types do not add any Part
s.
This trait has a blanket implementation and is therefore not implementable by user code.
§Examples
A custom implementation that adds parts:
use core::str::FromStr;
use icu_pattern::Pattern;
use icu_pattern::DoublePlaceholder;
use icu_pattern::DoublePlaceholderKey;
use icu_pattern::PlaceholderValueProvider;
use writeable::adapters::WithPart;
use writeable::adapters::WriteableAsTryWriteableInfallible;
use writeable::assert_writeable_parts_eq;
use writeable::Part;
use writeable::Writeable;
let pattern = Pattern::<DoublePlaceholder>::try_from_str(
"Hello, {0} and {1}!",
Default::default(),
)
.unwrap();
struct ValuesWithParts<'a>(&'a str, &'a str);
const PART_PLACEHOLDER_0: Part = Part {
category: "custom",
value: "placeholder0",
};
const PART_PLACEHOLDER_1: Part = Part {
category: "custom",
value: "placeholder1",
};
const PART_LITERAL: Part = Part {
category: "custom",
value: "literal",
};
impl PlaceholderValueProvider<DoublePlaceholderKey> for ValuesWithParts<'_> {
type Error = core::convert::Infallible;
type W<'a> = WriteableAsTryWriteableInfallible<WithPart<&'a str>>
where
Self: 'a;
type L<'a, 'l> = WithPart<&'l str>
where
Self: 'a;
#[inline]
fn value_for(&self, key: DoublePlaceholderKey) -> Self::W<'_> {
let writeable = match key {
DoublePlaceholderKey::Place0 => WithPart {
writeable: self.0,
part: PART_PLACEHOLDER_0,
},
DoublePlaceholderKey::Place1 => WithPart {
writeable: self.1,
part: PART_PLACEHOLDER_1,
},
};
WriteableAsTryWriteableInfallible(writeable)
}
#[inline]
fn map_literal<'a, 'l>(&'a self, literal: &'l str) -> Self::L<'a, 'l> {
WithPart {
writeable: literal,
part: PART_LITERAL,
}
}
}
assert_writeable_parts_eq!(
pattern.interpolate(ValuesWithParts("Alice", "Bob")),
"Hello, Alice and Bob!",
[
(0, 7, PART_LITERAL),
(7, 12, PART_PLACEHOLDER_0),
(12, 17, PART_LITERAL),
(17, 20, PART_PLACEHOLDER_1),
(20, 21, PART_LITERAL),
]
);
Required Associated Types§
type Error
Sourcetype W<'a>: TryWriteable<Error = Self::Error>
where
Self: 'a
type W<'a>: TryWriteable<Error = Self::Error> where Self: 'a
The type of TryWriteable
returned by Self::value_for
.
To return a Writeable
, wrap it with WriteableAsTryWriteableInfallible
.
Sourcetype L<'a, 'l>: Writeable
where
Self: 'a
type L<'a, 'l>: Writeable where Self: 'a
The type of Writeable
returned by Self::map_literal
.
If you are not adding parts, this can be &'l str
.
Required Methods§
Sourcefn value_for(&self, key: K) -> Self::W<'_>
fn value_for(&self, key: K) -> Self::W<'_>
Returns the TryWriteable
to substitute for the given placeholder.
Sourcefn map_literal<'a, 'l>(&'a self, literal: &'l str) -> Self::L<'a, 'l>
fn map_literal<'a, 'l>(&'a self, literal: &'l str) -> Self::L<'a, 'l>
Maps a literal string to a Writeable
that could contain parts.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.