zenoh_codec/core/
wire_expr.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14use 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}