use crate::qmdb::any::{ operation::{update::sealed::Sealed, Update as UpdateTrait}, value::{FixedEncoding, ValueEncoding, VariableEncoding}, FixedValue, VariableValue, }; use bytes::{Buf, BufMut}; use commonware_codec::{ Encode as _, EncodeSize, Error as CodecError, FixedSize, Read, ReadExt as _, Write, }; use commonware_utils::{hex, Array}; use std::fmt; #[derive(Clone, PartialEq, Debug, Eq)] pub struct Update { pub key: K, pub value: V::Value, pub next_key: K, } #[cfg(feature = "arbitrary")] impl arbitrary::Arbitrary<'_> for Update where K: for<'a> arbitrary::Arbitrary<'a>, V::Value: for<'a> arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { Ok(Self { key: u.arbitrary()?, value: u.arbitrary()?, next_key: u.arbitrary()?, }) } } impl Sealed for Update {} impl UpdateTrait for Update { fn key(&self) -> &K { &self.key } fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "[key:{} next_key:{} value:{}]", self.key, self.next_key, hex(&self.value.encode()) ) } } impl FixedSize for Update> { const SIZE: usize = K::SIZE + V::SIZE + K::SIZE; } impl Write for Update> { fn write(&self, buf: &mut impl BufMut) { self.key.write(buf); self.value.write(buf); self.next_key.write(buf); } } impl Read for Update> { type Cfg = (); fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let key = K::read(buf)?; let value = V::read_cfg(buf, cfg)?; let next_key = K::read(buf)?; Ok(Self { key, value, next_key, }) } } impl EncodeSize for Update> { fn encode_size(&self) -> usize { K::SIZE + self.value.encode_size() + K::SIZE } } impl Write for Update> { fn write(&self, buf: &mut impl BufMut) { self.key.write(buf); self.value.write(buf); self.next_key.write(buf); } } impl Read for Update> { type Cfg = ::Cfg; fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let key = K::read(buf)?; let value = V::read_cfg(buf, cfg)?; let next_key = K::read(buf)?; Ok(Self { key, value, next_key, }) } }