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
use std::io::{Error, ErrorKind, Read, Result, Write};
#[cfg(feature = "signed")]
use crate::zigzag::Zigzag;

#[cfg(feature = "async")]
pub extern crate async_trait;

#[cfg(feature = "bools")]
mod bools;
#[cfg(feature = "raw")]
mod raw;
#[cfg(any(feature = "varint", feature = "async_varint"))]
mod varint;
#[cfg(any(feature = "signed", feature = "async_signed"))]
pub mod zigzag;
#[cfg(any(feature = "signed", feature = "async_signed"))]
mod signed;
#[cfg(feature = "async")]
pub mod asynchronous;

pub trait VariableReadable {
    fn read(&mut self) -> Result<u8>;

    #[inline]
    fn read_bool(&mut self) -> Result<bool> {
        match self.read()? {
            0 => Ok(false),
            1 => Ok(true),
            i => Err(Error::new(ErrorKind::InvalidData, format!("Invalid boolean value: {}", i))),
        }
    }

    #[cfg(feature = "bools")]
    bools::define_bools_read!();

    fn read_more(&mut self, buf: &mut [u8]) -> Result<()> {
        for i in 0..buf.len() {
            buf[i] = self.read()?;
        }
        Ok(())
    }

    #[cfg(feature = "raw")]
    raw::define_raw_read!();

    #[cfg(feature = "varint")]
    varint::define_varint_read!();

    #[cfg(feature = "signed")]
    signed::define_signed_read!();

    #[cfg(feature = "vec_u8")]
    #[inline]
    fn read_u8_vec(&mut self) -> Result<Vec<u8>> {
        let length = self.read_u128_varint()? as usize;
        let mut bytes = vec![0; length];
        self.read_more(&mut bytes)?;
        Ok(bytes)
    }

    #[cfg(feature = "string")]
    #[inline]
    fn read_string(&mut self) -> Result<String> {
        match String::from_utf8(self.read_u8_vec()?) {
            Ok(s) => Ok(s),
            Err(e) => Err(Error::new(ErrorKind::InvalidData, e.to_string())),
        }
    }
}

pub trait VariableWritable {
    fn write(&mut self, byte: u8) -> Result<usize>;

    #[inline]
    fn write_bool(&mut self, b: bool) -> Result<usize> {
        self.write(if b { 1 } else { 0 })
    }

    #[cfg(feature = "bools")]
    bools::define_bools_write!();

    fn write_more(&mut self, bytes: &[u8]) -> Result<usize> {
        for i in 0..bytes.len() {
            self.write(bytes[i])?;
        }
        Ok(bytes.len())
    }

    #[cfg(feature = "raw")]
    raw::define_raw_write!();

    #[cfg(feature = "varint")]
    varint::define_varint_write!();

    #[cfg(feature = "signed")]
    signed::define_signed_write!();

    #[cfg(feature = "vec_u8")]
    #[inline]
    fn write_u8_vec(&mut self, message: &[u8]) -> Result<usize> {
        self.write_u128_varint(message.len() as u128)?;
        self.write_more(message)
    }

    #[cfg(feature = "string")]
    #[inline]
    fn write_string(&mut self, message: &str) -> Result<usize> {
        self.write_u8_vec(message.as_bytes())
    }
}

impl<R: Read> VariableReadable for R {
    #[inline]
    fn read(&mut self) -> Result<u8> {
        let mut buf = [0];
        self.read_exact(&mut buf)?;
        Ok(buf[0])
    }

    #[inline]
    fn read_more(&mut self, buf: &mut [u8]) -> Result<()> {
        self.read_exact(buf)
    }
}

impl<W: Write> VariableWritable for W {
    #[inline]
    fn write(&mut self, byte: u8) -> Result<usize> {
        self.write_all(&[byte])?;
        Ok(1)
    }

    #[inline]
    fn write_more(&mut self, bytes: &[u8]) -> Result<usize> {
        self.write_all(bytes)?;
        Ok(bytes.len())
    }
}