vlan 0.1.0

Clean VLAN ID representation with transparent u16 support, an invalid VLAN Error type, and a value that represents a native VLAN.
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::{
    fmt::{Debug, Display},
    hash::Hash,
    num::NonZero,
};

pub type RawVlanId = u16;

/// Types that can be converted to a raw VLAN ID (u16).
pub trait AsRawVlanId {
    /// Get the u16 value.
    fn as_raw_vlan_id(&self) -> RawVlanId;
}

/// The error value that represents an invalid VLAN ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InvalidVlanId;

impl Display for InvalidVlanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("INVALID_VLAN_ID")
    }
}

impl std::error::Error for InvalidVlanId {}

/// The value that represents the native VLAN.
///
/// It is semantivally zero.
/// Internally, it consumes no memory.
#[derive(Debug, Clone, Copy)]
pub struct NativeVlanId;

impl NativeVlanId {
    pub const VALUE: u16 = 0;
}

impl Display for NativeVlanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("0")
    }
}

impl AsRawVlanId for NativeVlanId {
    fn as_raw_vlan_id(&self) -> RawVlanId {
        Self::VALUE
    }
}

impl From<NativeVlanId> for u16 {
    fn from(_val: NativeVlanId) -> Self {
        NativeVlanId::VALUE
    }
}

impl From<&NativeVlanId> for u16 {
    fn from(val: &NativeVlanId) -> Self {
        (*val).into()
    }
}

impl TryFrom<u16> for NativeVlanId {
    type Error = InvalidVlanId;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        if value == Self::VALUE {
            Ok(NativeVlanId)
        } else {
            Err(InvalidVlanId)
        }
    }
}

impl<Rhs: AsRawVlanId> PartialEq<Rhs> for NativeVlanId {
    fn eq(&self, other: &Rhs) -> bool {
        self.as_raw_vlan_id() == other.as_raw_vlan_id()
    }
}

impl<Rhs: AsRawVlanId> PartialOrd<Rhs> for NativeVlanId {
    fn partial_cmp(&self, other: &Rhs) -> Option<std::cmp::Ordering> {
        Some(self.as_raw_vlan_id().cmp(&other.as_raw_vlan_id()))
    }
}

impl Eq for NativeVlanId {}

impl Hash for NativeVlanId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u16(self.into());
    }
}

/// Non-zero VLAN ID valid per the IEEE 802.1Q standard.
///
/// Range: 1-4094
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct VlanId {
    inner: NonZero<u16>,
}

impl VlanId {
    pub const MIN_VALUE: u16 = 1;
    pub const MAX_VALUE: u16 = 4094;

    /// Minimal tagged VLAN ID (1)
    pub const MIN: Self = unsafe { Self::new_unchecked(Self::MIN_VALUE) };

    /// Maximum tagged VLAN ID (4094)
    pub const MAX: Self = unsafe { Self::new_unchecked(Self::MAX_VALUE) };

    /// Same as MIN (1)
    pub const ONE: Self = Self::MIN;

    /// Try to construct a new VlanId instance.
    pub const fn try_new(vlan: u16) -> Result<Self, InvalidVlanId> {
        if vlan < Self::MIN_VALUE || vlan > Self::MAX_VALUE {
            Err(InvalidVlanId)
        } else {
            Ok(Self {
                inner: unsafe { NonZero::new_unchecked(vlan) },
            })
        }
    }

    pub(crate) const unsafe fn new_unchecked(value: u16) -> Self {
        Self {
            inner: unsafe { NonZero::new_unchecked(value) },
        }
    }

    /// Extract the inner type.
    pub const fn inner(&self) -> NonZero<u16> {
        self.inner
    }

    /// Convert to u16.
    pub const fn as_u16(&self) -> u16 {
        self.inner.get()
    }
}

impl Display for VlanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("{}", self.as_u16()))
    }
}

impl TryFrom<u16> for VlanId {
    type Error = InvalidVlanId;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_new(value)
    }
}

impl From<VlanId> for u16 {
    fn from(val: VlanId) -> Self {
        val.inner.get()
    }
}

impl From<&VlanId> for u16 {
    fn from(val: &VlanId) -> Self {
        (*val).into()
    }
}

impl Debug for VlanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("VlanId({})", self.inner.get()))
    }
}

impl AsRawVlanId for VlanId {
    fn as_raw_vlan_id(&self) -> RawVlanId {
        self.inner.get()
    }
}

impl<Rhs: AsRawVlanId> PartialEq<Rhs> for VlanId {
    fn eq(&self, other: &Rhs) -> bool {
        self.as_raw_vlan_id() == other.as_raw_vlan_id()
    }
}

impl<Rhs: AsRawVlanId> PartialOrd<Rhs> for VlanId {
    fn partial_cmp(&self, other: &Rhs) -> Option<std::cmp::Ordering> {
        Some(self.as_raw_vlan_id().cmp(&other.as_raw_vlan_id()))
    }
}

impl Ord for VlanId {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_raw_vlan_id().cmp(&other.as_raw_vlan_id())
    }
}

impl Eq for VlanId {}

impl Hash for VlanId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u16(self.into());
    }
}

/// VLAN ID that may be a native VLAN.
///
/// Effectively, this is an `Option<T>` for the VLAN world.
///
/// It has the same memory layout as `u16`.
#[derive(Clone, Copy, Debug)]
pub enum MaybeVlanId {
    /// Native VLAN value. It is effectively the same as `Option::None`.
    Native(NativeVlanId),

    /// Tagged valid VLAN value.
    Tagged(VlanId),
}

impl MaybeVlanId {
    /// The size of this type in octets
    pub const OCTET_SIZE: usize = 2;

    /// Number of bits this type contains
    pub const BITS: u32 = 16;

    /// Number of bits actually used
    pub const EFFECTIVE_BITS: u32 = 12;

    /// Native VLAN value
    pub const NATIVE: MaybeVlanId = MaybeVlanId::Native(NativeVlanId);

    pub const MIN_TAGGED_VLAN: MaybeVlanId = MaybeVlanId::Tagged(VlanId::MIN);
    pub const MAX_TAGGED_VLAN: MaybeVlanId = MaybeVlanId::Tagged(VlanId::MAX);

    /// Tagged VLAN value
    pub const fn tagged(vlan: VlanId) -> Self {
        Self::Tagged(vlan)
    }

    /// Convert to u16.
    pub const fn as_u16(&self) -> u16 {
        match *self {
            Self::Tagged(vlan) => vlan.as_u16(),
            _ => 0,
        }
    }

    /// Try to construct a MaybeVlanId instance, with `InvalidVlanId` on errors.
    pub const fn try_new(vlan: u16) -> Result<Self, InvalidVlanId> {
        match vlan {
            0 => Ok(Self::NATIVE),
            VlanId::MIN_VALUE..=VlanId::MAX_VALUE => unsafe {
                Ok(Self::Tagged(VlanId::new_unchecked(vlan)))
            },
            _ => Err(InvalidVlanId),
        }
    }

    /// Convert to big-endian octets.
    ///
    /// You are most likely to use this for network applications.
    pub const fn as_be_bytes(&self) -> [u8; 2] {
        u16::to_be_bytes(self.as_u16())
    }

    /// Convert to native-endian octets.
    pub const fn as_ne_bytes(&self) -> [u8; 2] {
        u16::to_ne_bytes(self.as_u16())
    }

    /// Convert to little-endian octets.
    pub const fn as_le_bytes(&self) -> [u8; 2] {
        u16::to_le_bytes(self.as_u16())
    }
}

impl From<VlanId> for MaybeVlanId {
    fn from(value: VlanId) -> Self {
        MaybeVlanId::Tagged(value)
    }
}

impl From<NativeVlanId> for MaybeVlanId {
    fn from(value: NativeVlanId) -> Self {
        MaybeVlanId::Native(value)
    }
}

impl Display for MaybeVlanId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("{}", self.as_u16()))
    }
}

impl TryFrom<u16> for MaybeVlanId {
    type Error = InvalidVlanId;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_new(value)
    }
}

impl From<MaybeVlanId> for u16 {
    fn from(val: MaybeVlanId) -> Self {
        val.as_u16()
    }
}

impl From<&MaybeVlanId> for u16 {
    fn from(val: &MaybeVlanId) -> Self {
        val.as_u16()
    }
}

impl AsRawVlanId for MaybeVlanId {
    fn as_raw_vlan_id(&self) -> RawVlanId {
        match *self {
            Self::Native(_) => 0,
            Self::Tagged(vlan) => vlan.as_raw_vlan_id(),
        }
    }
}

impl<Rhs: AsRawVlanId> PartialEq<Rhs> for MaybeVlanId {
    fn eq(&self, other: &Rhs) -> bool {
        self.as_raw_vlan_id() == other.as_raw_vlan_id()
    }
}

impl<Rhs: AsRawVlanId> PartialOrd<Rhs> for MaybeVlanId {
    fn partial_cmp(&self, other: &Rhs) -> Option<std::cmp::Ordering> {
        Some(self.as_raw_vlan_id().cmp(&other.as_raw_vlan_id()))
    }
}

impl Eq for MaybeVlanId {}

impl Ord for MaybeVlanId {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.as_raw_vlan_id().cmp(&other.as_raw_vlan_id())
    }
}

impl Hash for MaybeVlanId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u16(self.into());
    }
}

#[cfg(feature = "serde")]
mod serialization {
    use super::*;
    use serde::de::{self, Deserialize, Deserializer, Visitor};
    use serde::ser::{Serialize, Serializer};
    use std::fmt;

    struct VlanIdVisitor;

    impl<'de> Visitor<'de> for VlanIdVisitor {
        type Value = VlanId;
        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("Expected a valid tagged VLAN ID")
        }

        fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            v.try_into().map_err(|e| de::Error::custom(e))
        }
    }

    impl<'de> Deserialize<'de> for VlanId {
        fn deserialize<D>(deserializer: D) -> Result<VlanId, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_str(VlanIdVisitor)
        }
    }

    impl Serialize for VlanId {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_u16(self.as_u16())
        }
    }

    struct MaybeVlanIdVisitor;

    impl<'de> Visitor<'de> for MaybeVlanIdVisitor {
        type Value = MaybeVlanId;
        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("Expected a valid tagged VLAN ID or zero (native VLAN)")
        }

        fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            v.try_into().map_err(|e| de::Error::custom(e))
        }
    }

    impl<'de> Deserialize<'de> for MaybeVlanId {
        fn deserialize<D>(deserializer: D) -> Result<MaybeVlanId, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_str(MaybeVlanIdVisitor)
        }
    }

    impl Serialize for MaybeVlanId {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_u16(self.as_u16())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bound_check() {
        assert!(TryInto::<VlanId>::try_into(0).is_err());
        assert!(TryInto::<VlanId>::try_into(1).is_ok());
        assert!(TryInto::<VlanId>::try_into(4094).is_ok());
        assert!(TryInto::<VlanId>::try_into(4095).is_err());
        assert!(TryInto::<VlanId>::try_into(u16::MAX).is_err());

        assert_eq!(TryInto::<MaybeVlanId>::try_into(0), Ok(MaybeVlanId::NATIVE));
        assert_eq!(
            TryInto::<MaybeVlanId>::try_into(1),
            Ok(MaybeVlanId::MIN_TAGGED_VLAN)
        );
        assert_eq!(
            TryInto::<MaybeVlanId>::try_into(4094),
            Ok(MaybeVlanId::MAX_TAGGED_VLAN)
        );
        assert!(TryInto::<MaybeVlanId>::try_into(4095).is_err());
        assert!(TryInto::<MaybeVlanId>::try_into(u16::MAX).is_err());

        assert_eq!(MaybeVlanId::NATIVE, NativeVlanId);
    }

    #[test]
    fn mem_compat() {
        let zero: u16 = 0u16;
        let should_be_zero: u16 = unsafe { std::mem::transmute(MaybeVlanId::NATIVE) };
        assert_eq!(zero, should_be_zero);

        let a: u16 = 3125u16;
        let b = MaybeVlanId::Tagged(VlanId::try_new(3125u16).unwrap());
        assert_eq!(a, unsafe { std::mem::transmute(b) });
    }
}