aboutsummaryrefslogtreecommitdiff
path: root/src/data_type.rs
blob: 43a7f1a068be11c96aa08cbb2b38fb9fd9473024 (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
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

pub trait DataType {
    const TYPE: u32;
    const LEN: u32;

    fn data(&self) -> Vec<u8>;
}

impl DataType for Ipv4Addr {
    const TYPE: u32 = 7;
    const LEN: u32 = 4;

    fn data(&self) -> Vec<u8> {
        self.octets().to_vec()
    }
}

impl DataType for Ipv6Addr {
    const TYPE: u32 = 8;
    const LEN: u32 = 16;

    fn data(&self) -> Vec<u8> {
        self.octets().to_vec()
    }
}

impl<const N: usize> DataType for [u8; N] {
    const TYPE: u32 = 5;
    const LEN: u32 = N as u32;

    fn data(&self) -> Vec<u8> {
        self.to_vec()
    }
}

pub fn ip_to_vec(ip: IpAddr) -> Vec<u8> {
    match ip {
        IpAddr::V4(x) => x.octets().to_vec(),
        IpAddr::V6(x) => x.octets().to_vec(),
    }
}