Skip to main content

zenoh_config/
wrappers.rs

1//
2// Copyright (c) 2024 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//
14
15//! Wrappers around types reexported by `zenoh` from subcrates.
16//! These wrappers are used to avoid exposing the the API necessary only for zenoh internals into the public API.
17
18use core::fmt;
19use std::str::FromStr;
20
21use serde::{Deserialize, Serialize};
22use zenoh_protocol::{
23    core::{key_expr::OwnedKeyExpr, EntityGlobalIdProto, Locator, WhatAmI, ZenohIdProto},
24    scouting::HelloProto,
25};
26
27/// The global unique id of a Zenoh runtime
28#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
29#[repr(transparent)]
30pub struct ZenohId(ZenohIdProto);
31
32impl ZenohId {
33    /// Used by plugins for crating adminspace path
34    #[doc(hidden)]
35    pub fn into_keyexpr(self) -> OwnedKeyExpr {
36        self.into()
37    }
38
39    pub fn to_le_bytes(self) -> [u8; uhlc::ID::MAX_SIZE] {
40        self.0.to_le_bytes()
41    }
42}
43
44impl fmt::Debug for ZenohId {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        self.0.fmt(f)
47    }
48}
49impl fmt::Display for ZenohId {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        self.0.fmt(f)
52    }
53}
54
55impl From<ZenohIdProto> for ZenohId {
56    fn from(id: ZenohIdProto) -> Self {
57        Self(id)
58    }
59}
60
61impl TryFrom<&[u8]> for ZenohId {
62    type Error = zenoh_result::Error;
63
64    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
65        let proto: ZenohIdProto = value.try_into()?;
66        Ok(ZenohId::from(proto))
67    }
68}
69
70impl From<ZenohId> for ZenohIdProto {
71    fn from(id: ZenohId) -> Self {
72        id.0
73    }
74}
75
76impl From<ZenohId> for uhlc::ID {
77    fn from(zid: ZenohId) -> Self {
78        zid.0.into()
79    }
80}
81
82impl From<ZenohId> for OwnedKeyExpr {
83    fn from(zid: ZenohId) -> Self {
84        zid.0.into()
85    }
86}
87
88impl From<&ZenohId> for OwnedKeyExpr {
89    fn from(zid: &ZenohId) -> Self {
90        (*zid).into()
91    }
92}
93
94impl FromStr for ZenohId {
95    type Err = zenoh_result::Error;
96
97    fn from_str(s: &str) -> Result<Self, Self::Err> {
98        ZenohIdProto::from_str(s).map(|zid| zid.into())
99    }
100}
101
102/// A zenoh Hello message.
103#[derive(Clone)]
104#[repr(transparent)]
105pub struct Hello(HelloProto);
106
107impl Hello {
108    /// Get the locators of this Hello message.
109    pub fn locators(&self) -> &[Locator] {
110        &self.0.locators
111    }
112
113    /// Get the zenoh id of this Hello message.
114    pub fn zid(&self) -> ZenohId {
115        self.0.zid.into()
116    }
117
118    /// Get the whatami of this Hello message.
119    pub fn whatami(&self) -> WhatAmI {
120        self.0.whatami
121    }
122
123    /// Constructs an empty Hello message.
124    #[zenoh_macros::internal]
125    pub fn empty() -> Self {
126        Hello(HelloProto {
127            version: zenoh_protocol::VERSION,
128            whatami: WhatAmI::default(),
129            zid: ZenohIdProto::default(),
130            locators: Vec::default(),
131        })
132    }
133}
134
135impl From<HelloProto> for Hello {
136    fn from(inner: HelloProto) -> Self {
137        Hello(inner)
138    }
139}
140
141impl fmt::Debug for Hello {
142    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143        fmt::Debug::fmt(&self.0, f)
144    }
145}
146
147impl fmt::Display for Hello {
148    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149        f.debug_struct("Hello")
150            .field("zid", &self.zid())
151            .field("whatami", &self.whatami())
152            .field("locators", &self.locators())
153            .finish()
154    }
155}
156
157/// The ID globally identifying an entity in a zenoh system.
158#[derive(Default, Copy, Clone, Eq, Hash, PartialEq)]
159#[repr(transparent)]
160pub struct EntityGlobalId(EntityGlobalIdProto);
161
162/// The ID to locally identify an entity in a Zenoh session.
163pub type EntityId = u32;
164
165impl EntityGlobalId {
166    /// Creates a new EntityGlobalId.
167    #[zenoh_macros::internal]
168    pub fn new(zid: ZenohId, eid: EntityId) -> Self {
169        EntityGlobalIdProto {
170            zid: zid.into(),
171            eid,
172        }
173        .into()
174    }
175
176    /// Returns the [`ZenohId`], i.e. the Zenoh session, this ID is associated to.
177    pub fn zid(&self) -> ZenohId {
178        self.0.zid.into()
179    }
180
181    /// Returns the [`EntityId`] used to identify the entity in a Zenoh session.
182    pub fn eid(&self) -> EntityId {
183        self.0.eid
184    }
185}
186
187impl fmt::Debug for EntityGlobalId {
188    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189        f.debug_struct("EntityGlobalId")
190            .field("zid", &self.zid())
191            .field("eid", &self.eid())
192            .finish()
193    }
194}
195
196impl From<EntityGlobalIdProto> for EntityGlobalId {
197    fn from(id: EntityGlobalIdProto) -> Self {
198        Self(id)
199    }
200}
201
202impl From<EntityGlobalId> for EntityGlobalIdProto {
203    fn from(value: EntityGlobalId) -> Self {
204        value.0
205    }
206}