use crate::qmdb::{ any::{ operation::{update::sealed::Sealed, Update as UpdateTrait}, value::{FixedEncoding, ValueEncoding, VariableEncoding}, FixedValue, VariableValue, }, operation::Key, }; use commonware_codec::{ Encode as _, EncodeSize, Error as CodecError, FixedSize, Read, ReadExt as _, Write, }; use commonware_runtime::{Buf, BufMut}; 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 value(&self) -> &V::Value { &self.value } fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "[key:{} next_key:{} value:{}]", hex(&self.key), hex(&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> where K: Key + EncodeSize, V: VariableValue, { fn encode_size(&self) -> usize { self.key.encode_size() + self.value.encode_size() + self.next_key.encode_size() } } impl Write for Update> where K: Key + Write, V: VariableValue, { fn write(&self, buf: &mut impl BufMut) { self.key.write(buf); self.value.write(buf); self.next_key.write(buf); } } impl Read for Update> where K: Key + Read, V: VariableValue, { type Cfg = (::Cfg, ::Cfg); fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let key = K::read_cfg(buf, &cfg.0)?; let value = V::read_cfg(buf, &cfg.1)?; let next_key = K::read_cfg(buf, &cfg.0)?; Ok(Self { key, value, next_key, }) } }