zenoh_protocol/zenoh/
err.rs

1//
2// Copyright (c) 2022 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::vec::Vec;
15
16use zenoh_buffers::ZBuf;
17
18use crate::{common::ZExtUnknown, core::Encoding};
19
20/// # Err message
21///
22/// ```text
23/// Flags:
24/// - X: Reserved
25/// - E: Encoding       If E==1 then the encoding is present
26/// - Z: Extension      If Z==1 then at least one extension is present
27///
28///   7 6 5 4 3 2 1 0
29///  +-+-+-+-+-+-+-+-+
30///  |Z|E|X|   ERR   |
31///  +-+-+-+---------+
32///  ~   encoding    ~  if E==1
33///  +---------------+
34///  ~  [err_exts]   ~  if Z==1
35///  +---------------+
36///  ~ pl: <u8;z32>  ~  -- Payload
37///  +---------------+
38/// ```
39pub mod flag {
40    // pub const X: u8 = 1 << 5; // 0x20 Reserved
41    pub const E: u8 = 1 << 6; // 0x40 Encoding      if E==1 then the encoding is present
42    pub const Z: u8 = 1 << 7; // 0x80 Extensions        if Z==1 then an extension will follow
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct Err {
47    pub encoding: Encoding,
48    pub ext_sinfo: Option<ext::SourceInfoType>,
49    #[cfg(feature = "shared-memory")]
50    pub ext_shm: Option<ext::ShmType>,
51    pub ext_unknown: Vec<ZExtUnknown>,
52    pub payload: ZBuf,
53}
54
55pub mod ext {
56    #[cfg(feature = "shared-memory")]
57    use crate::{common::ZExtUnit, zextunit};
58    use crate::{common::ZExtZBuf, zextzbuf};
59
60    /// # SourceInfo extension
61    /// Used to carry additional information about the source of data
62    pub type SourceInfo = zextzbuf!(0x1, false);
63    pub type SourceInfoType = crate::zenoh::ext::SourceInfoType<{ SourceInfo::ID }>;
64
65    /// # Shared Memory extension
66    /// Used to carry additional information about the shared-memory layout of data
67    #[cfg(feature = "shared-memory")]
68    pub type Shm = zextunit!(0x2, true);
69    #[cfg(feature = "shared-memory")]
70    pub type ShmType = crate::zenoh::ext::ShmType<{ Shm::ID }>;
71}
72
73impl Err {
74    #[cfg(feature = "test")]
75    pub fn rand() -> Self {
76        use rand::Rng;
77
78        use crate::common::iext;
79        let mut rng = rand::thread_rng();
80
81        let encoding = Encoding::rand();
82        let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
83        #[cfg(feature = "shared-memory")]
84        let ext_shm = rng.gen_bool(0.5).then_some(ext::ShmType::rand());
85        let mut ext_unknown = Vec::new();
86        for _ in 0..rng.gen_range(0..4) {
87            ext_unknown.push(ZExtUnknown::rand2(
88                iext::mid(ext::SourceInfo::ID) + 1,
89                false,
90            ));
91        }
92        let payload = ZBuf::rand(rng.gen_range(0..=64));
93
94        Self {
95            encoding,
96            ext_sinfo,
97            #[cfg(feature = "shared-memory")]
98            ext_shm,
99            ext_unknown,
100            payload,
101        }
102    }
103}