zenoh_protocol/core/
resolution.rs1use alloc::string::String;
15use core::{fmt, str::FromStr};
16
17use zenoh_result::{bail, ZError};
18
19use crate::{network::RequestId, transport::TransportSn};
20
21#[repr(u8)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
24pub enum Bits {
25 U8 = 0b00,
26 U16 = 0b01,
27 U32 = 0b10,
28 U64 = 0b11,
29}
30
31impl Bits {
32 const S8: &'static str = "8bit";
33 const S16: &'static str = "16bit";
34 const S32: &'static str = "32bit";
35 const S64: &'static str = "64bit";
36
37 pub const fn bits(&self) -> u32 {
38 match self {
39 Bits::U8 => u8::BITS,
40 Bits::U16 => u16::BITS,
41 Bits::U32 => u32::BITS,
42 Bits::U64 => u64::BITS,
43 }
44 }
45
46 pub const fn mask(&self) -> u64 {
47 match self {
48 Bits::U8 => u8::MAX as u64,
49 Bits::U16 => u16::MAX as u64,
50 Bits::U32 => u32::MAX as u64,
51 Bits::U64 => u64::MAX,
52 }
53 }
54
55 pub const fn to_str(self) -> &'static str {
56 match self {
57 Bits::U8 => Self::S8,
58 Bits::U16 => Self::S16,
59 Bits::U32 => Self::S32,
60 Bits::U64 => Self::S64,
61 }
62 }
63}
64
65impl From<u8> for Bits {
66 fn from(_: u8) -> Self {
67 Self::U8
68 }
69}
70
71impl From<u16> for Bits {
72 fn from(_: u16) -> Self {
73 Self::U16
74 }
75}
76
77impl From<u32> for Bits {
78 fn from(_: u32) -> Self {
79 Self::U32
80 }
81}
82
83impl From<u64> for Bits {
84 fn from(_: u64) -> Self {
85 Self::U64
86 }
87}
88
89impl FromStr for Bits {
90 type Err = ZError;
91
92 fn from_str(s: &str) -> Result<Self, Self::Err> {
93 match s {
94 Bits::S8 => Ok(Bits::U8),
95 Bits::S16 => Ok(Bits::U16),
96 Bits::S32 => Ok(Bits::U32),
97 Bits::S64 => Ok(Bits::U64),
98 _ => bail!(
99 "{s} is not a valid Bits value. Valid values are: '{}', '{}', '{}', '{}'.",
100 Bits::S8,
101 Bits::S16,
102 Bits::S32,
103 Bits::S64
104 ),
105 }
106 }
107}
108
109impl fmt::Display for Bits {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 f.write_str(self.to_str())
112 }
113}
114
115#[repr(u8)]
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118pub enum Field {
119 FrameSN = 0,
120 RequestID = 2,
121}
122
123#[repr(transparent)]
124#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
125pub struct Resolution(u8);
126
127impl Resolution {
128 pub const fn as_u8(&self) -> u8 {
129 self.0
130 }
131
132 pub const fn get(&self, field: Field) -> Bits {
133 let value = (self.0 >> (field as u8)) & 0b11;
134 unsafe { core::mem::transmute(value) }
135 }
136
137 pub fn set(&mut self, field: Field, bits: Bits) {
138 self.0 &= !(0b11 << field as u8); self.0 |= (bits as u8) << (field as u8); }
141
142 #[cfg(feature = "test")]
143 #[doc(hidden)]
144 pub fn rand() -> Self {
145 use rand::Rng;
146
147 let mut rng = rand::thread_rng();
148 let v: u8 = rng.gen();
149 Self(v & 0b00001111)
150 }
151}
152
153impl Default for Resolution {
154 fn default() -> Self {
155 let frame_sn = Bits::from(TransportSn::MAX) as u8;
156 let request_id = (Bits::from(RequestId::MAX) as u8) << 2;
157 Self(frame_sn | request_id)
158 }
159}
160
161impl From<u8> for Resolution {
162 fn from(v: u8) -> Self {
163 Self(v)
164 }
165}
166
167impl serde::Serialize for Bits {
169 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
170 where
171 S: serde::Serializer,
172 {
173 serializer.serialize_str(self.to_str())
174 }
175}
176
177pub struct BitsVisitor;
178impl<'de> serde::de::Visitor<'de> for BitsVisitor {
179 type Value = Bits;
180
181 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
182 write!(
183 formatter,
184 "either '{}', '{}', '{}', '{}'.",
185 Bits::S8,
186 Bits::S16,
187 Bits::S32,
188 Bits::S64,
189 )
190 }
191
192 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
193 where
194 E: serde::de::Error,
195 {
196 v.parse().map_err(|_| {
197 serde::de::Error::unknown_variant(v, &[Bits::S8, Bits::S16, Bits::S32, Bits::S64])
198 })
199 }
200
201 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
202 where
203 E: serde::de::Error,
204 {
205 self.visit_str(v)
206 }
207
208 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
209 where
210 E: serde::de::Error,
211 {
212 self.visit_str(&v)
213 }
214}
215
216impl<'de> serde::Deserialize<'de> for Bits {
217 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
218 where
219 D: serde::Deserializer<'de>,
220 {
221 deserializer.deserialize_str(BitsVisitor)
222 }
223}