zenoh_protocol/zenoh/
reply.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 crate::{
17    common::ZExtUnknown,
18    zenoh::{query::ConsolidationMode, PushBody},
19};
20
21/// # Reply message
22///
23/// ```text
24/// Flags:
25/// - C: Consolidation  if C==1 then consolidation is present
26/// - X: Reserved
27/// - Z: Extension      If Z==1 then at least one extension is present
28///
29///   7 6 5 4 3 2 1 0
30///  +-+-+-+-+-+-+-+-+
31///  |Z|X|C|  REPLY  |
32///  +-+-+-+---------+
33///  % consolidation %  if C==1
34///  +---------------+
35///  ~  [repl_exts]  ~  if Z==1
36///  +---------------+
37///  ~   ReplyBody   ~  -- Payload
38///  +---------------+
39/// ```
40pub mod flag {
41    pub const C: u8 = 1 << 5; // 0x20 Consolidation if C==1 then consolidation is present
42                              // pub const X: u8 = 1 << 6; // 0x40 Reserved
43    pub const Z: u8 = 1 << 7; // 0x80 Extensions    if Z==1 then an extension will follow
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct Reply {
48    pub consolidation: ConsolidationMode,
49    pub ext_unknown: Vec<ZExtUnknown>,
50    pub payload: ReplyBody,
51}
52
53pub type ReplyBody = PushBody;
54
55impl Reply {
56    #[cfg(feature = "test")]
57    #[doc(hidden)]
58    pub fn rand() -> Self {
59        use rand::Rng;
60        let mut rng = rand::thread_rng();
61
62        let payload = ReplyBody::rand();
63        let consolidation = ConsolidationMode::rand();
64        let mut ext_unknown = Vec::new();
65        for _ in 0..rng.gen_range(0..4) {
66            ext_unknown.push(ZExtUnknown::rand2(1, false));
67        }
68
69        Self {
70            consolidation,
71            ext_unknown,
72            payload,
73        }
74    }
75}