Trait icu::pattern::PlaceholderValueProvider

source ·
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 TryWriteables for interpolation.

This trait can add Parts for individual literals or placeholders. The implementations of this trait on standard types do not add any Parts.

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

source

type Error

source

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.

source

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§

source

fn value_for(&self, key: K) -> Self::W<'_>

Returns the TryWriteable to substitute for the given placeholder.

See PatternItem::Placeholder

source

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.

See PatternItem::Literal

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'b, K, T> PlaceholderValueProvider<K> for &'b T

source§

type Error = <T as PlaceholderValueProvider<K>>::Error

source§

type W<'a> = <T as PlaceholderValueProvider<K>>::W<'a> where &'b T: 'a

source§

type L<'a, 'l> = <T as PlaceholderValueProvider<K>>::L<'a, 'l> where &'b T: 'a

source§

fn value_for(&self, key: K) -> <&'b T as PlaceholderValueProvider<K>>::W<'_>

source§

fn map_literal<'a, 'l>( &'a self, literal: &'l str, ) -> <&'b T as PlaceholderValueProvider<K>>::L<'a, 'l>

source§

impl<'k, K, W> PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>> for BTreeMap<K, W>
where K: Ord + Borrow<str>, W: Writeable,

source§

type Error = MissingNamedPlaceholderError<'k>

source§

type W<'a> = Result<&'a W, <BTreeMap<K, W> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::Error> where BTreeMap<K, W>: 'a

source§

type L<'a, 'l> = &'l str where BTreeMap<K, W>: 'a

source§

fn value_for<'a>( &'a self, key: MultiNamedPlaceholderKey<'k>, ) -> <BTreeMap<K, W> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::W<'a>

source§

fn map_literal<'a, 'l>( &'a self, literal: &'l str, ) -> <BTreeMap<K, W> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::L<'a, 'l>

source§

impl<'k, K, W, S> PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>> for LiteMap<K, W, S>
where K: Ord + Borrow<str>, W: Writeable, S: Store<K, W>,

source§

type Error = MissingNamedPlaceholderError<'k>

source§

type W<'a> = Result<&'a W, <LiteMap<K, W, S> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::Error> where LiteMap<K, W, S>: 'a

source§

type L<'a, 'l> = &'l str where LiteMap<K, W, S>: 'a

source§

fn value_for<'a>( &'a self, key: MultiNamedPlaceholderKey<'k>, ) -> <LiteMap<K, W, S> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::W<'a>

source§

fn map_literal<'a, 'l>( &'a self, literal: &'l str, ) -> <LiteMap<K, W, S> as PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>>>::L<'a, 'l>

source§

impl<W0, W1> PlaceholderValueProvider<DoublePlaceholderKey> for (W0, W1)
where W0: Writeable, W1: Writeable,

source§

impl<W> PlaceholderValueProvider<DoublePlaceholderKey> for [W; 2]
where W: Writeable,

source§

impl<W> PlaceholderValueProvider<SinglePlaceholderKey> for [W; 1]
where W: Writeable,

source§

impl<W> PlaceholderValueProvider<SinglePlaceholderKey> for (W,)
where W: Writeable,

Implementors§