Skip to main content

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    #[inline(always)]
34    fn write(self, writer: &mut W, x: &WireExpr<'_>) -> Self::Output {
35        let WireExpr {
36            scope,
37            suffix,
38            mapping: _,
39        } = x;
40
41        let zodec = Zenoh080Bounded::<ExprId>::new();
42        zodec.write(&mut *writer, *scope)?;
43
44        if x.has_suffix() {
45            #[cold]
46            fn write_suffix<W: Writer>(writer: &mut W, suffix: &str) -> Result<(), DidntWrite> {
47                let zodec = Zenoh080Bounded::<ExprLen>::new();
48                zodec.write(&mut *writer, suffix)
49            }
50            write_suffix(writer, suffix)?;
51        }
52        Ok(())
53    }
54}
55
56impl<R> RCodec<WireExpr<'static>, &mut R> for Zenoh080Condition
57where
58    R: Reader,
59{
60    type Error = DidntRead;
61
62    #[inline(always)]
63    fn read(self, reader: &mut R) -> Result<WireExpr<'static>, Self::Error> {
64        let zodec = Zenoh080Bounded::<ExprId>::new();
65        let scope: ExprId = zodec.read(&mut *reader)?;
66
67        let suffix: String = if self.condition {
68            #[cold]
69            fn read_suffix<R: Reader>(reader: &mut R) -> Result<String, DidntRead> {
70                let zodec = Zenoh080Bounded::<ExprLen>::new();
71                zodec.read(&mut *reader)
72            }
73            read_suffix(reader)?
74        } else {
75            String::new()
76        };
77        Ok(WireExpr {
78            scope,
79            suffix: suffix.into(),
80            mapping: Mapping::DEFAULT,
81        })
82    }
83}