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 pub fn rand() -> Self {
144 use rand::Rng;
145
146 let mut rng = rand::thread_rng();
147 let v: u8 = rng.gen();
148 Self(v & 0b00001111)
149 }
150}
151
152impl Default for Resolution {
153 fn default() -> Self {
154 let frame_sn = Bits::from(TransportSn::MAX) as u8;
155 let request_id = (Bits::from(RequestId::MAX) as u8) << 2;
156 Self(frame_sn | request_id)
157 }
158}
159
160impl From<u8> for Resolution {
161 fn from(v: u8) -> Self {
162 Self(v)
163 }
164}
165
166impl serde::Serialize for Bits {
168 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
169 where
170 S: serde::Serializer,
171 {
172 serializer.serialize_str(self.to_str())
173 }
174}
175
176pub struct BitsVisitor;
177impl<'de> serde::de::Visitor<'de> for BitsVisitor {
178 type Value = Bits;
179
180 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
181 write!(
182 formatter,
183 "either '{}', '{}', '{}', '{}'.",
184 Bits::S8,
185 Bits::S16,
186 Bits::S32,
187 Bits::S64,
188 )
189 }
190
191 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
192 where
193 E: serde::de::Error,
194 {
195 v.parse().map_err(|_| {
196 serde::de::Error::unknown_variant(v, &[Bits::S8, Bits::S16, Bits::S32, Bits::S64])
197 })
198 }
199
200 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
201 where
202 E: serde::de::Error,
203 {
204 self.visit_str(v)
205 }
206
207 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
208 where
209 E: serde::de::Error,
210 {
211 self.visit_str(&v)
212 }
213}
214
215impl<'de> serde::Deserialize<'de> for Bits {
216 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
217 where
218 D: serde::Deserializer<'de>,
219 {
220 deserializer.deserialize_str(BitsVisitor)
221 }
222}