mod de_borrowed;
mod de_owned;
mod ser;
pub use self::de_borrowed::*;
pub use self::de_owned::*;
pub use self::ser::*;
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
AnyNotSupported,
IdentifierNotSupported,
IgnoredAnyNotSupported,
CannotBorrowOwnedData,
SkipFixedArrayLengthNotSupported,
#[cfg(not(feature = "alloc"))]
CannotAllocate,
#[cfg(not(feature = "alloc"))]
CustomError,
}
#[cfg(feature = "alloc")]
impl serde::de::Error for crate::error::DecodeError {
fn custom<T>(msg: T) -> Self
where
T: core::fmt::Display,
{
use alloc::string::ToString;
Self::OtherString(msg.to_string())
}
}
#[cfg(not(feature = "std"))]
impl serde::de::StdError for crate::error::DecodeError {}
#[cfg(not(feature = "alloc"))]
impl serde::de::Error for crate::error::DecodeError {
fn custom<T>(_: T) -> Self
where
T: core::fmt::Display,
{
DecodeError::CustomError.into()
}
}
#[allow(clippy::from_over_into)]
impl Into<crate::error::DecodeError> for DecodeError {
fn into(self) -> crate::error::DecodeError {
crate::error::DecodeError::Serde(self)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum EncodeError {
SequenceMustHaveLength,
SkipFixedArrayLengthNotSupported,
#[cfg(not(feature = "alloc"))]
CannotCollectStr,
#[cfg(not(feature = "alloc"))]
CustomError,
}
#[allow(clippy::from_over_into)]
impl Into<crate::error::EncodeError> for EncodeError {
fn into(self) -> crate::error::EncodeError {
crate::error::EncodeError::Serde(self)
}
}
#[cfg(feature = "alloc")]
impl serde::ser::Error for crate::error::EncodeError {
fn custom<T>(msg: T) -> Self
where
T: core::fmt::Display,
{
use alloc::string::ToString;
Self::OtherString(msg.to_string())
}
}
#[cfg(not(feature = "std"))]
impl serde::de::StdError for crate::error::EncodeError {}
#[cfg(not(feature = "alloc"))]
impl serde::ser::Error for crate::error::EncodeError {
fn custom<T>(_: T) -> Self
where
T: core::fmt::Display,
{
EncodeError::CustomError.into()
}
}
pub struct Compat<T>(pub T);
impl<T> crate::Decode for Compat<T>
where
T: serde::de::DeserializeOwned,
{
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_owned::SerdeDecoder { de: decoder };
T::deserialize(serde_decoder).map(Compat)
}
}
impl<'de, T> crate::BorrowDecode<'de> for Compat<T>
where
T: serde::de::DeserializeOwned,
{
fn borrow_decode<D: crate::de::BorrowDecoder<'de>>(
decoder: &mut D,
) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_owned::SerdeDecoder { de: decoder };
T::deserialize(serde_decoder).map(Compat)
}
}
impl<T> crate::Encode for Compat<T>
where
T: serde::Serialize,
{
fn encode<E: crate::enc::Encoder>(
&self,
encoder: &mut E,
) -> Result<(), crate::error::EncodeError> {
let serializer = ser::SerdeEncoder { enc: encoder };
self.0.serialize(serializer)?;
Ok(())
}
}
pub struct BorrowCompat<T>(pub T);
impl<'de, T> crate::de::BorrowDecode<'de> for BorrowCompat<T>
where
T: serde::de::Deserialize<'de>,
{
fn borrow_decode<D: crate::de::BorrowDecoder<'de>>(
decoder: &mut D,
) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_borrowed::SerdeDecoder {
de: decoder,
pd: core::marker::PhantomData,
};
T::deserialize(serde_decoder).map(BorrowCompat)
}
}
impl<T> crate::Encode for BorrowCompat<T>
where
T: serde::Serialize,
{
fn encode<E: crate::enc::Encoder>(
&self,
encoder: &mut E,
) -> Result<(), crate::error::EncodeError> {
let serializer = ser::SerdeEncoder { enc: encoder };
self.0.serialize(serializer)?;
Ok(())
}
}