use crate::{common::extension, RCodec, WCodec, Zenoh080, Zenoh080Header};
use alloc::vec::Vec;
use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
};
use zenoh_protocol::{
common::imsg,
zenoh::{
id,
pull::{flag, Pull},
},
};
impl<W> WCodec<&Pull, &mut W> for Zenoh080
where
W: Writer,
{
type Output = Result<(), DidntWrite>;
fn write(self, writer: &mut W, x: &Pull) -> Self::Output {
let Pull { ext_unknown } = x;
let mut header = id::PULL;
let mut n_exts = ext_unknown.len() as u8;
if n_exts != 0 {
header |= flag::Z;
}
self.write(&mut *writer, header)?;
for u in ext_unknown.iter() {
n_exts -= 1;
self.write(&mut *writer, (u, n_exts != 0))?;
}
Ok(())
}
}
impl<R> RCodec<Pull, &mut R> for Zenoh080
where
R: Reader,
{
type Error = DidntRead;
fn read(self, reader: &mut R) -> Result<Pull, Self::Error> {
let header: u8 = self.read(&mut *reader)?;
let codec = Zenoh080Header::new(header);
codec.read(reader)
}
}
impl<R> RCodec<Pull, &mut R> for Zenoh080Header
where
R: Reader,
{
type Error = DidntRead;
fn read(self, reader: &mut R) -> Result<Pull, Self::Error> {
if imsg::mid(self.header) != id::PULL {
return Err(DidntRead);
}
let mut ext_unknown = Vec::new();
let mut has_ext = imsg::has_flag(self.header, flag::Z);
while has_ext {
let ext: u8 = self.codec.read(&mut *reader)?;
let (u, ext) = extension::read(reader, "Pull", ext)?;
ext_unknown.push(u);
has_ext = ext;
}
Ok(Pull { ext_unknown })
}
}