use std::error::Error;
use crate::{KeyDeserializeError, KeySerde, KeySerializeError};
pub trait Encoding {
type EncodeError: Error;
type DecodeError: Error;
}
pub trait Encodable<E: Encoding> {
fn encode(&self) -> Result<Vec<u8>, E::EncodeError>;
}
pub trait Decodable<E: Encoding>: Sized {
fn decode(bytes: &[u8]) -> Result<Self, E::DecodeError>;
}
pub trait Codec<E: Encoding>: Encodable<E> + Decodable<E> {}
impl<T: Encodable<E> + Decodable<E>, E: Encoding> Codec<E> for T {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyEncoding;
impl Encoding for KeyEncoding {
type EncodeError = KeySerializeError;
type DecodeError = KeyDeserializeError;
}
impl<T: KeySerde> Encodable<KeyEncoding> for T {
fn encode(&self) -> Result<Vec<u8>, KeySerializeError> {
self.encode()
}
}
impl<T: KeySerde> Decodable<KeyEncoding> for T {
fn decode(mut bytes: &[u8]) -> Result<Self, KeyDeserializeError> {
T::decode(&mut bytes)
}
}
#[cfg(feature = "borsh")]
pub(crate) mod borsh {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BorshEncoding;
impl Encoding for BorshEncoding {
type EncodeError = borsh::io::Error;
type DecodeError = borsh::io::Error;
}
impl<T: borsh::ser::BorshSerialize> Encodable<BorshEncoding> for T {
fn encode(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
impl<T: borsh::de::BorshDeserialize> Decodable<BorshEncoding> for T {
fn decode(bytes: &[u8]) -> Result<Self, borsh::io::Error> {
borsh::from_slice(bytes)
}
}
}