use crate::kv_store::Value;
use core::ops::Deref;
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;
pub mod manual;
pub mod postcard;
pub mod primitive;
pub mod raw;
pub trait Encoder {
fn as_bytes(&self) -> Cow<'_, [u8]>;
}
pub trait Encode<T: ?Sized> {
type Encoder<'a>: Encoder
where
T: 'a;
fn encode(t: &T) -> Self::Encoder<'_>;
fn encode_as_value(t: &T) -> Value {
Value::from(Self::encode(t).as_bytes())
}
}
pub trait Decode<T> {
fn decode(bytes: &[u8]) -> anyhow::Result<T>;
fn decode_from_value(value: Value) -> anyhow::Result<T> {
Self::decode(value.deref())
}
}
impl Encoder for Cow<'_, [u8]> {
fn as_bytes(&self) -> Cow<'_, [u8]> {
match self {
Cow::Borrowed(borrowed) => Cow::Borrowed(borrowed),
Cow::Owned(owned) => Cow::Borrowed(owned.as_ref()),
}
}
}
impl<const SIZE: usize> Encoder for [u8; SIZE] {
fn as_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_slice())
}
}