zenoh_codec/core/
wire_expr.rs1use alloc::string::String;
15
16use zenoh_buffers::{
17 reader::{DidntRead, Reader},
18 writer::{DidntWrite, Writer},
19};
20use zenoh_protocol::{
21 core::{ExprId, ExprLen, WireExpr},
22 network::Mapping,
23};
24
25use crate::{core::Zenoh080Bounded, RCodec, WCodec, Zenoh080, Zenoh080Condition};
26
27impl<W> WCodec<&WireExpr<'_>, &mut W> for Zenoh080
28where
29 W: Writer,
30{
31 type Output = Result<(), DidntWrite>;
32
33 fn write(self, writer: &mut W, x: &WireExpr<'_>) -> Self::Output {
34 let WireExpr {
35 scope,
36 suffix,
37 mapping: _,
38 } = x;
39
40 let zodec = Zenoh080Bounded::<ExprId>::new();
41 zodec.write(&mut *writer, *scope)?;
42
43 if x.has_suffix() {
44 let zodec = Zenoh080Bounded::<ExprLen>::new();
45 zodec.write(&mut *writer, suffix.as_ref())?;
46 }
47 Ok(())
48 }
49}
50
51impl<R> RCodec<WireExpr<'static>, &mut R> for Zenoh080Condition
52where
53 R: Reader,
54{
55 type Error = DidntRead;
56
57 fn read(self, reader: &mut R) -> Result<WireExpr<'static>, Self::Error> {
58 let zodec = Zenoh080Bounded::<ExprId>::new();
59 let scope: ExprId = zodec.read(&mut *reader)?;
60
61 let suffix: String = if self.condition {
62 let zodec = Zenoh080Bounded::<ExprLen>::new();
63 zodec.read(&mut *reader)?
64 } else {
65 String::new()
66 };
67 Ok(WireExpr {
68 scope,
69 suffix: suffix.into(),
70 mapping: Mapping::DEFAULT,
71 })
72 }
73}