use crate::Error;
use writeable::{Part, TryWriteable};
#[cfg(feature = "alloc")]
use alloc::{borrow::Cow, borrow::ToOwned};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::exhaustive_enums)] pub enum PatternItem<'a, T> {
Placeholder(T),
Literal(&'a str),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::exhaustive_enums)] #[cfg(feature = "alloc")]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum PatternItemCow<'a, T> {
Placeholder(T),
#[cfg_attr(feature = "serde", serde(borrow))]
Literal(Cow<'a, str>),
}
#[cfg(feature = "alloc")]
impl<'a, T, U> From<PatternItem<'a, U>> for PatternItemCow<'a, T>
where
T: From<U>,
{
fn from(value: PatternItem<'a, U>) -> Self {
match value {
PatternItem::Placeholder(t) => Self::Placeholder(t.into()),
PatternItem::Literal(s) => Self::Literal(Cow::Borrowed(s)),
}
}
}
pub trait PatternBackend: crate::private::Sealed + 'static {
type PlaceholderKey<'a>;
#[cfg(feature = "alloc")]
type PlaceholderKeyCow<'a>;
type Error<'a>;
type Store: ?Sized;
#[doc(hidden)] type Iter<'a>: Iterator<Item = PatternItem<'a, Self::PlaceholderKey<'a>>>;
#[doc(hidden)] fn validate_store(store: &Self::Store) -> Result<(), Error>;
#[doc(hidden)]
#[cfg(feature = "alloc")]
fn try_from_items<
'cow,
'ph,
I: Iterator<Item = Result<PatternItemCow<'cow, Self::PlaceholderKeyCow<'ph>>, Error>>,
>(
items: I,
) -> Result<<Self::Store as ToOwned>::Owned, Error>
where
Self::Store: ToOwned;
#[doc(hidden)] fn iter_items(store: &Self::Store) -> Self::Iter<'_>;
}
pub const PATTERN_LITERAL_PART: Part = Part {
category: "pattern",
value: "literal",
};
pub const PATTERN_PLACEHOLDER_PART: Part = Part {
category: "pattern",
value: "placeholder",
};
pub trait PlaceholderValueProvider<K> {
type Error;
type W<'a>: TryWriteable<Error = Self::Error>
where
Self: 'a;
const LITERAL_PART: Part;
fn value_for(&self, key: K) -> (Self::W<'_>, Part);
}
impl<'b, K, T> PlaceholderValueProvider<K> for &'b T
where
T: PlaceholderValueProvider<K> + ?Sized,
{
type Error = T::Error;
type W<'a> = T::W<'a> where T: 'a, 'b: 'a;
const LITERAL_PART: Part = T::LITERAL_PART;
fn value_for(&self, key: K) -> (Self::W<'_>, Part) {
(*self).value_for(key)
}
}