1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]
extern crate alloc;
extern crate fallible_collections;
extern crate hash32;
extern crate hash32_derive;
extern crate heapless;
extern crate log;
pub extern crate nb;
mod error;
pub mod session;
pub mod subscription;
pub mod time;
pub mod transfer;
pub mod transport;
use core::convert::TryFrom;
use core::ops::RangeInclusive;
use core::str::FromStr;
use hash32_derive::Hash32;
#[derive(Debug)]
pub struct InvalidValue;
pub use crate::error::{OutOfMemoryError, ServiceSubscribeError};
const VALID_SUBJECT_IDS: RangeInclusive<u16> = 0..=8191;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Hash32)]
pub struct SubjectId(u16);
impl SubjectId {
pub const fn from_truncating(value: u16) -> Self {
SubjectId(value & *VALID_SUBJECT_IDS.end())
}
}
impl TryFrom<u16> for SubjectId {
type Error = InvalidValue;
fn try_from(value: u16) -> core::result::Result<Self, Self::Error> {
if VALID_SUBJECT_IDS.contains(&value) {
Ok(SubjectId(value))
} else {
Err(InvalidValue)
}
}
}
impl From<SubjectId> for u16 {
#[inline]
fn from(id: SubjectId) -> Self {
id.0
}
}
impl From<SubjectId> for u32 {
#[inline]
fn from(id: SubjectId) -> Self {
id.0 as u32
}
}
impl From<SubjectId> for usize {
#[inline]
fn from(id: SubjectId) -> Self {
id.0 as usize
}
}
impl FromStr for SubjectId {
type Err = InvalidValue;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value: u16 = s.parse().map_err(|_| InvalidValue)?;
SubjectId::try_from(value)
}
}
const VALID_SERVICE_IDS: RangeInclusive<u16> = 0..=511;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Hash32)]
pub struct ServiceId(u16);
impl ServiceId {
pub const fn from_truncating(value: u16) -> Self {
ServiceId(value & *VALID_SERVICE_IDS.end())
}
}
impl TryFrom<u16> for ServiceId {
type Error = InvalidValue;
fn try_from(value: u16) -> core::result::Result<Self, Self::Error> {
if VALID_SERVICE_IDS.contains(&value) {
Ok(ServiceId(value))
} else {
Err(InvalidValue)
}
}
}
impl From<ServiceId> for u16 {
#[inline]
fn from(id: ServiceId) -> Self {
id.0
}
}
impl From<ServiceId> for u32 {
#[inline]
fn from(id: ServiceId) -> Self {
id.0 as u32
}
}
impl From<ServiceId> for usize {
#[inline]
fn from(id: ServiceId) -> Self {
id.0 as usize
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Hash32)]
pub struct PortId(u16);
impl From<SubjectId> for PortId {
fn from(subject: SubjectId) -> Self {
PortId(subject.0)
}
}
impl TryFrom<PortId> for SubjectId {
type Error = InvalidValue;
fn try_from(port: PortId) -> Result<Self, Self::Error> {
SubjectId::try_from(port.0)
}
}
impl From<ServiceId> for PortId {
fn from(service: ServiceId) -> Self {
PortId(service.0)
}
}
impl TryFrom<PortId> for ServiceId {
type Error = InvalidValue;
fn try_from(port: PortId) -> Result<Self, Self::Error> {
ServiceId::try_from(port.0)
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Priority {
Exceptional = 0,
Immediate = 1,
Fast = 2,
High = 3,
Nominal = 4,
Low = 5,
Slow = 6,
Optional = 7,
}
impl Default for Priority {
fn default() -> Self {
Priority::Nominal
}
}
impl From<Priority> for u8 {
fn from(priority: Priority) -> Self {
priority as u8
}
}
impl TryFrom<u8> for Priority {
type Error = InvalidValue;
fn try_from(value: u8) -> core::result::Result<Self, Self::Error> {
match value {
0 => Ok(Priority::Exceptional),
1 => Ok(Priority::Immediate),
2 => Ok(Priority::Fast),
3 => Ok(Priority::High),
4 => Ok(Priority::Nominal),
5 => Ok(Priority::Low),
6 => Ok(Priority::Slow),
7 => Ok(Priority::Optional),
_ => Err(InvalidValue),
}
}
}
mod fmt_impl {
use super::SubjectId;
use core::fmt::{Display, Formatter, Result};
impl Display for SubjectId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Display::fmt(&self.0, f)
}
}
}