#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use musli_core02::de::DecodeUnsized;
use musli_core02::{Allocator, Decode, Decoder, Encode, Encoder};
use crate::RelativePath;
#[cfg(feature = "alloc")]
use crate::RelativePathBuf;
#[cfg_attr(relative_path_docsrs, doc(cfg(feature = "musli02")))]
impl<M> Encode<M> for RelativePath {
type Encode = Self;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder<Mode = M>,
{
encoder.encode_string(self.as_str())
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}
#[cfg_attr(relative_path_docsrs, doc(cfg(feature = "musli02")))]
impl<'de, M, A> Decode<'de, M, A> for &'de RelativePath
where
A: Allocator,
{
#[inline]
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M, Allocator = A>,
{
Ok(RelativePath::new(decoder.decode::<&'de str>()?))
}
}
#[cfg_attr(relative_path_docsrs, doc(cfg(feature = "musli02")))]
impl<'de, M> DecodeUnsized<'de, M> for RelativePath {
#[inline]
fn decode_unsized<D, F, O>(decoder: D, f: F) -> Result<O, D::Error>
where
D: Decoder<'de, Mode = M>,
F: FnOnce(&Self) -> Result<O, D::Error>,
{
decoder.decode_unsized(|string: &str| f(RelativePath::new(string)))
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(
relative_path_docsrs,
doc(cfg(all(feature = "alloc", feature = "musli02")))
)]
impl<M> Encode<M> for RelativePathBuf {
type Encode = RelativePath;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder<Mode = M>,
{
encoder.encode_string(self.as_str())
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(
relative_path_docsrs,
doc(cfg(all(feature = "alloc", feature = "musli02")))
)]
impl<'de, M, A> Decode<'de, M, A> for RelativePathBuf
where
A: Allocator,
{
#[inline]
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M, Allocator = A>,
{
decoder.decode_unsized(|path: &RelativePath| Ok(path.to_relative_path_buf()))
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(
relative_path_docsrs,
doc(cfg(all(feature = "alloc", feature = "musli02")))
)]
impl<'de, M, A> Decode<'de, M, A> for Box<RelativePath>
where
A: Allocator,
{
#[inline]
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Mode = M, Allocator = A>,
{
decoder.decode_unsized(|path: &RelativePath| Ok(Box::<RelativePath>::from(path)))
}
}