use crate::builders::Operation;
use crate::builders::change::{encode_changeset_op, encode_patchset_op, patchset_pk_mapping};
use crate::encoding::markers;
use crate::encoding::{MaybeValue, Value};
use crate::schema::SchemaWithPK;
use alloc::vec::Vec;
use core::fmt::Debug;
pub(crate) trait Format<S, B>: Default + Clone + Copy + PartialEq + Eq + 'static {
type Old: Clone + Debug + Default;
type DeleteData: Clone + Debug + Default;
const TABLE_MARKER: u8;
type BuildState;
fn build_state<T: SchemaWithPK>(table: &T) -> Self::BuildState;
fn encode_op(
out: &mut Vec<u8>,
op: &Operation<Self, S, B>,
pk: &[Value<S, B>],
state: &Self::BuildState,
) where
S: AsRef<str>,
B: AsRef<[u8]>;
}
pub trait DiffFormat<S, B>: Format<S, B> {}
impl<S, B, F: Format<S, B>> DiffFormat<S, B> for F {}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ChangesetFormat;
impl<S: Clone + Debug + AsRef<str>, B: Clone + Debug + AsRef<[u8]>> Format<S, B>
for ChangesetFormat
{
type Old = MaybeValue<S, B>;
type DeleteData = Vec<Value<S, B>>;
const TABLE_MARKER: u8 = markers::CHANGESET;
type BuildState = ();
fn build_state<T: SchemaWithPK>(_table: &T) -> Self::BuildState {}
fn encode_op(
out: &mut Vec<u8>,
op: &Operation<Self, S, B>,
_pk: &[Value<S, B>],
_state: &Self::BuildState,
) where
S: AsRef<str>,
B: AsRef<[u8]>,
{
encode_changeset_op(out, op);
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct PatchsetFormat;
impl<S, B> Format<S, B> for PatchsetFormat {
type Old = ();
type DeleteData = ();
const TABLE_MARKER: u8 = markers::PATCHSET;
type BuildState = (Vec<u8>, Vec<Option<usize>>);
fn build_state<T: SchemaWithPK>(table: &T) -> Self::BuildState {
patchset_pk_mapping(table)
}
fn encode_op(
out: &mut Vec<u8>,
op: &Operation<Self, S, B>,
pk: &[Value<S, B>],
state: &Self::BuildState,
) where
S: AsRef<str>,
B: AsRef<[u8]>,
{
encode_patchset_op(out, op, pk, &state.0, &state.1);
}
}