aboutsummaryrefslogtreecommitdiff
path: root/src/types.rs
blob: c4661c70678349fcc36f4ea4dd9a91121eeeaf74 (plain) (blame)
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
use crate::{Deserialize, Result, Serialize};

use std::io::{Read, Write};

use bitfield::bitfield;

bitfield! {
    /// Version and type of a PPPoE header combined in a single octet.
    #[derive(Clone, Eq, PartialEq)]
    pub struct VerType(u8);
    impl Debug;

    u8;

    pub ver, set_ver: 7, 4;
    pub ty, set_ty: 3, 0;
}

impl Default for VerType {
    fn default() -> Self {
        Self(0x11)
    }
}

impl Serialize for VerType {
    fn serialize<W: Write>(&self, w: &mut W) -> Result<()> {
        self.0.serialize(w)
    }
}

impl Deserialize for VerType {
    fn deserialize<R: Read>(&mut self, r: &mut R) -> Result<()> {
        self.0.deserialize(r)
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Ipv4Addr(pub std::net::Ipv4Addr);

impl Default for Ipv4Addr {
    fn default() -> Self {
        Self(std::net::Ipv4Addr::UNSPECIFIED)
    }
}

impl From<std::net::Ipv4Addr> for Ipv4Addr {
    fn from(addr: std::net::Ipv4Addr) -> Self {
        Self(addr)
    }
}

impl Serialize for Ipv4Addr {
    fn serialize<W: Write>(&self, w: &mut W) -> Result<()> {
        u32::from(self.0).serialize(w)
    }
}

impl Deserialize for Ipv4Addr {
    fn deserialize<R: Read>(&mut self, r: &mut R) -> Result<()> {
        let mut tmp = u32::default();
        tmp.deserialize(r)?;

        self.0 = tmp.into();
        Ok(())
    }
}