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 K, pub V::Value); #[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(u.arbitrary()?, u.arbitrary()?)) } } impl Sealed for Update {} impl UpdateTrait for Update { fn key(&self) -> &K { &self.0 } fn value(&self) -> &V::Value { &self.1 } fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[key:{} value:{}]", hex(&self.0), hex(&self.1.encode())) } } // --- Fixed-value impls (require K: Array for FixedSize) --- impl FixedSize for Update> { const SIZE: usize = K::SIZE + V::SIZE; } impl Write for Update> { fn write(&self, buf: &mut impl BufMut) { self.0.write(buf); self.1.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)?; Ok(Self(key, value)) } } // --- Variable-value impls (relaxed to K: Key) --- impl EncodeSize for Update> where K: Key + EncodeSize, V: VariableValue, { fn encode_size(&self) -> usize { self.0.encode_size() + self.1.encode_size() } } impl Write for Update> where K: Key + Write, V: VariableValue, { fn write(&self, buf: &mut impl BufMut) { self.0.write(buf); self.1.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)?; Ok(Self(key, value)) } }