use core::fmt;
use crate::prelude::*;
use ufotofu::{
codec::Blame,
codec_prelude::{RelativeDecodable, RelativeEncodable},
};
use willow_data_model::paths::private::{ComponentsNotRelatedError, PrivatePathContext as PPC};
wrapper! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
PrivatePathContext; willow_data_model::paths::private::PrivatePathContext<MCL, MCC, MPL>
}
impl fmt::Debug for PrivatePathContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PrivatePathContext {
pub fn new(private: Path, rel: Path) -> Result<Self, ComponentsNotRelatedError> {
let inner = PPC::new(private.into(), rel.into())?;
Ok(Self(inner))
}
pub unsafe fn new_unchecked(private: Path, rel: Path) -> Self {
let inner = unsafe { PPC::new_unchecked(private.into(), rel.into()) };
Self(inner)
}
pub fn private(&self) -> &Path {
<&willow_data_model::paths::Path<MCL, MCC, MPL> as Into<&Path>>::into(self.0.private())
}
pub fn rel(&self) -> &Path {
<&willow_data_model::paths::Path<MCL, MCC, MPL> as Into<&Path>>::into(self.0.rel())
}
}
impl RelativeEncodable<PrivatePathContext> for Path {
async fn relative_encode<C>(
&self,
rel: &PrivatePathContext,
consumer: &mut C,
) -> Result<(), C::Error>
where
C: ufotofu::BulkConsumer<Item = u8> + ?Sized,
{
self.0
.relative_encode(<&PPC<MCL, MCC, MPL>>::from(rel), consumer)
.await
}
fn can_be_encoded_relative_to(&self, rel: &PrivatePathContext) -> bool {
self.0
.can_be_encoded_relative_to(<&PPC<MCL, MCC, MPL>>::from(rel))
}
}
impl RelativeDecodable<PrivatePathContext> for Path {
type ErrorReason = Blame;
async fn relative_decode<P>(
rel: &PrivatePathContext,
producer: &mut P,
) -> Result<Self, ufotofu::codec::DecodeError<P::Final, P::Error, Self::ErrorReason>>
where
P: ufotofu::BulkProducer<Item = u8> + ?Sized,
Self: Sized,
{
let inner = willow_data_model::paths::Path::<MCL, MCC, MPL>::relative_decode(
<&PPC<MCL, MCC, MPL>>::from(rel),
producer,
)
.await?;
Ok(Self(inner))
}
}