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
use crate::CompactInt;
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use paste::paste;
use std::error::Error;
use std::net::SocketAddr;
use std::{fmt, io};

#[derive(Debug)]
pub enum DeserializationError {
    Io(io::Error),
    Parse(String),
}
impl DeserializationError {
    pub fn parse(source: &[u8], into: &str) -> DeserializationError {
        DeserializationError::Parse(format!("Could not construct {} from {:?}", into, source))
    }
}
impl Error for DeserializationError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            DeserializationError::Io(ref err) => Some(err),
            DeserializationError::Parse(_) => None,
        }
    }
}
impl fmt::Display for DeserializationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DeserializationError::Io(ref err) => err.fmt(f),
            DeserializationError::Parse(ref err) => err.fmt(f),
        }
    }
}
impl From<io::Error> for DeserializationError {
    fn from(err: io::Error) -> DeserializationError {
        DeserializationError::Io(err)
    }
}
type Result<R> = std::result::Result<R, DeserializationError>;

pub trait Deserializable {
    fn deserialize<R>(reader: &mut R) -> Result<Self>
    where
        Self: Sized,
        R: std::io::Read;
}

impl Deserializable for bool {
    fn deserialize<R>(target: &mut R) -> Result<bool>
    where
        R: std::io::Read,
    {
        let value = target.read_u8()?;
        match value {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(DeserializationError::Parse(format!(
                "Could not parse {:?} as bool",
                value
            ))),
        }
    }
}
impl Deserializable for u8 {
    fn deserialize<R>(target: &mut R) -> Result<u8>
    where
        R: std::io::Read,
    {
        Ok(target.read_u8()?)
    }
}

macro_rules! impl_deser_primitive {
    ($($t:ty),+) => {
        $(impl Deserializable for $t {
            fn deserialize<R>(target: &mut R) -> Result<$t>
            where
                R: std::io::Read,
            {
                paste! {Ok(target.[<read_ $t>]::<LittleEndian>()?)}
            }
        })+
    };
}

impl_deser_primitive!(u16, u32, u64, i32, i64);

impl<T> Deserializable for Vec<T>
where
    T: Deserializable,
{
    fn deserialize<R>(target: &mut R) -> Result<Vec<T>>
    where
        R: std::io::Read,
        T: Deserializable,
    {
        let len = CompactInt::deserialize(target)?.value() as usize;
        let mut result: Vec<T> = Vec::with_capacity(len);
        for _ in 0..len {
            result.push(T::deserialize(target)?);
        }
        Ok(result)
    }
}

impl Deserializable for String {
    fn deserialize<R>(target: &mut R) -> Result<String>
    where
        R: std::io::Read,
    {
        let len = CompactInt::deserialize(target)?.value() as usize;
        let mut vec = Vec::with_capacity(len);
        vec.resize(len, 0);
        target.read_exact(&mut vec)?;
        if let Ok(string) = String::from_utf8(vec) {
            return Ok(string);
        }
        return Ok(String::from("?"));
        // let mut result = String::with_capacity(len);
        // for _ in 0..len {
        //     result.push(target.read_u8()? as char);
        // }
        // Ok(result)
    }
}

// TODO: test
impl Deserializable for SocketAddr {
    fn deserialize<R>(target: &mut R) -> Result<SocketAddr>
    where
        R: std::io::Read,
    {
        Ok(SocketAddr::from((
            <[u8; 16]>::deserialize(target)?,
            target.read_u16::<BigEndian>()?,
        )))
    }
}

// TODO: Replace when const generics stabilize
macro_rules! impl_deserializable_byte_array {
    ($size:expr) => {
        impl Deserializable for [u8; $size] {
            fn deserialize<R>(target: &mut R) -> Result<[u8; $size]>
            where
                R: std::io::Read,
            {
                let mut result = [0u8; $size];
                target.read_exact(&mut result)?;
                Ok(result)
            }
        }
    };
}

impl_deserializable_byte_array!(4);
impl_deserializable_byte_array!(16);
impl_deserializable_byte_array!(32);