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
use crate::io::Source;

use super::Token;

const BYTE_LEN_FOR_8: usize = 1;
const BYTE_LEN_FOR_16: usize = 2;
const BYTE_LEN_FOR_32: usize = 4;
const BYTE_LEN_FOR_64: usize = 8;

pub trait Encode {
    fn to_le_vec(&self) -> Vec<u8>;
}

impl Encode for u16 {
    fn to_le_vec(&self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl Encode for u32 {
    fn to_le_vec(&self) -> Vec<u8> {
        self.to_le_bytes().to_vec()
    }
}

impl Encode for Vec<Token> {
    fn to_le_vec(&self) -> Vec<u8> {
        self.iter().flat_map(|token| token.to_le_vec()).collect()
    }
}

pub trait Decode {
    fn from_le_vec(source: &mut Source) -> Result<Self, String>
    where
        Self: Sized;
}

impl Decode for u8 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_8] = [0; BYTE_LEN_FOR_8];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_8)?[..]);
        Ok(u8::from_le_bytes(temp))
    }
}

impl Decode for u16 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_16] = [0; BYTE_LEN_FOR_16];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_16)?[..]);
        Ok(u16::from_le_bytes(temp))
    }
}

impl Decode for u32 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_32] = [0; BYTE_LEN_FOR_32];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_32)?[..]);
        Ok(u32::from_le_bytes(temp))
    }
}

impl Decode for i8 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_8] = [0; BYTE_LEN_FOR_8];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_8)?[..]);
        Ok(i8::from_le_bytes(temp))
    }
}

impl Decode for i16 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_16] = [0; BYTE_LEN_FOR_16];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_16)?[..]);
        Ok(i16::from_le_bytes(temp))
    }
}

impl Decode for i32 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_32] = [0; BYTE_LEN_FOR_32];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_32)?[..]);
        Ok(i32::from_le_bytes(temp))
    }
}

impl Decode for f32 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_32] = [0; BYTE_LEN_FOR_32];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_32)?[..]);
        Ok(f32::from_le_bytes(temp))
    }
}

impl Decode for f64 {
    fn from_le_vec(source: &mut Source) -> Result<Self, String> {
        let mut temp: [u8; BYTE_LEN_FOR_64] = [0; BYTE_LEN_FOR_64];
        temp.copy_from_slice(&source.get_vec(BYTE_LEN_FOR_64)?[..]);
        Ok(f64::from_le_bytes(temp))
    }
}