pub mod array;
pub mod bin;
pub mod bool;
pub mod float;
pub mod int;
pub mod map;
pub mod nil;
pub mod str;
pub use bin::BinaryEncoder;
pub use map::{MapDataEncoder, MapEncoder, MapFormatEncoder, MapSliceEncoder};
pub use nil::NilEncoder;
use crate::{Format, io::IoWrite};
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub enum Error<T> {
Io(T),
InvalidFormat,
}
impl<T> From<T> for Error<T> {
fn from(value: T) -> Self {
Error::Io(value)
}
}
impl<T: core::fmt::Display> core::fmt::Display for Error<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Io(e) => write!(f, "{}", e),
Error::InvalidFormat => write!(f, "Cannot encode value"),
}
}
}
impl<T: core::error::Error> core::error::Error for Error<T> {}
type Result<T, E> = ::core::result::Result<T, Error<E>>;
pub trait Encode {
fn encode<W: IoWrite>(&self, writer: &mut W) -> Result<usize, W::Error>;
}
macro_rules! deref_impl {
(
$(#[$attr:meta])*
<$($desc:tt)+
) => {
$(#[$attr])*
impl<$($desc)+
{
fn encode<W: IoWrite>(&self, writer: &mut W) -> Result<usize, <W as IoWrite>::Error> {
(**self).encode(writer)
}
}
};
}
deref_impl! {
<V> Encode for &V
where
V: Encode,
}
deref_impl! {
<V> Encode for &mut V
where
V: Encode,
}
#[cfg(feature = "alloc")]
deref_impl! {
<V> Encode for alloc::boxed::Box<V>
where
V: Encode + ?Sized,
}
impl Encode for Format {
fn encode<W: IoWrite>(&self, writer: &mut W) -> Result<usize, <W as IoWrite>::Error> {
writer.write(&self.as_slice())?;
Ok(1)
}
}