use std::sync::Arc;
use spacetimedb_sats::{
bsatn,
buffer::{BufReader, BufWriter, DecodeError},
ser::Serialize,
ProductValue,
};
use thiserror::Error;
pub mod txdata;
pub use txdata::Txdata;
pub trait Encode {
fn encode_record<W: BufWriter>(&self, writer: &mut W);
}
impl<T: Encode> Encode for Arc<T> {
fn encode_record<W: BufWriter>(&self, writer: &mut W) {
(**self).encode_record(writer)
}
}
impl Encode for ProductValue {
fn encode_record<W: BufWriter>(&self, writer: &mut W) {
self.serialize(bsatn::Serializer::new(writer))
.expect("bsatn serialize should never fail");
}
}
impl Encode for () {
fn encode_record<W: BufWriter>(&self, _writer: &mut W) {}
}
pub trait Decoder {
type Record: Encode;
type Error: From<DecodeError>;
fn decode_record<'a, R: BufReader<'a>>(
&self,
version: u8,
tx_offset: u64,
reader: &mut R,
) -> Result<Self::Record, Self::Error>;
fn consume_record<'a, R: BufReader<'a>>(
&self,
version: u8,
tx_offset: u64,
reader: &mut R,
) -> Result<(), Self::Error> {
self.decode_record(version, tx_offset, reader).map(drop)
}
fn skip_record<'a, R: BufReader<'a>>(&self, version: u8, tx_offset: u64, reader: &mut R)
-> Result<(), Self::Error>;
}
impl<const N: usize> Encode for [u8; N] {
fn encode_record<W: BufWriter>(&self, writer: &mut W) {
writer.put_slice(&self[..])
}
}
#[derive(Debug, Error)]
pub enum ArrayDecodeError {
#[error(transparent)]
Decode(#[from] DecodeError),
#[error(transparent)]
Traversal(#[from] crate::error::Traversal),
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub struct ArrayDecoder<const N: usize>;
impl<const N: usize> Decoder for ArrayDecoder<N> {
type Record = [u8; N];
type Error = ArrayDecodeError;
fn decode_record<'a, R: BufReader<'a>>(
&self,
_version: u8,
_tx_offset: u64,
reader: &mut R,
) -> Result<Self::Record, Self::Error> {
Ok(*reader.get_array()?)
}
fn skip_record<'a, R: BufReader<'a>>(
&self,
version: u8,
tx_offset: u64,
reader: &mut R,
) -> Result<(), Self::Error> {
self.decode_record(version, tx_offset, reader).map(drop)
}
}