msgpack/
unpacker.rs

1use crate::{unpack, value::RefValue, BufferedRead, UnpackError, Value};
2
3use std::iter::Iterator;
4use std::{cmp, io};
5pub struct RefUnpacker;
6pub struct RefUnpackFeeder<'a, R>(&'a mut R);
7use bytes::{Buf, BytesMut};
8
9impl<'a, R> Iterator for RefUnpackFeeder<'a, R>
10where
11    R: BufferedRead<'a>,
12{
13    type Item = RefValue<'a>;
14
15    fn next(&mut self) -> Option<Self::Item> {
16        RefUnpacker::unpack_ref_value(self.0).ok()
17    }
18}
19
20impl RefUnpacker {
21    pub fn feed<'a, R>(rd: &'a mut R) -> RefUnpackFeeder<'a, R>
22    where
23        R: BufferedRead<'a>,
24    {
25        RefUnpackFeeder(rd)
26    }
27
28    pub fn unpack_ref_value<'a, R>(rd: &mut R) -> Result<RefValue<'a>, UnpackError>
29    where
30        R: BufferedRead<'a>,
31    {
32        unpack::ref_value::unpack_value_ref(rd)
33    }
34}
35
36#[derive(Debug)]
37pub struct Unpacker<R> {
38    inner: R,
39}
40
41#[derive(Debug)]
42pub struct InnerBuf(io::Cursor<bytes::BytesMut>);
43
44impl io::Write for InnerBuf {
45    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
46        self.0.get_mut().extend_from_slice(buf);
47        Ok(buf.len())
48    }
49
50    fn flush(&mut self) -> io::Result<()> {
51        Ok(())
52    }
53}
54
55impl io::Read for InnerBuf {
56    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
57        if self.0.get_ref().len() == 0 {
58            use std::io::ErrorKind;
59            return Err(io::Error::new(
60                ErrorKind::UnexpectedEof,
61                "There is no more read data",
62            ));
63        }
64
65        let len = cmp::min(self.0.get_ref().len(), buf.len());
66        Buf::copy_to_slice(&mut self.0, &mut buf[0..len]);
67        Ok(len)
68    }
69}
70
71impl InnerBuf {
72    pub fn new() -> Self {
73        Self(io::Cursor::new(BytesMut::new()))
74    }
75
76    pub fn as_ref(&self) -> &io::Cursor<bytes::BytesMut> {
77        &self.0
78    }
79}
80
81impl<T> Unpacker<T>
82where
83    T: io::Read,
84{
85    pub fn from_reader(inner: T) -> Self {
86        Unpacker { inner }
87    }
88
89    pub fn iter(&mut self) -> UnpackerIter<&mut Unpacker<T>> {
90        UnpackerIter::new(self)
91    }
92}
93
94impl Unpacker<InnerBuf> {
95    pub fn new() -> Self {
96        Unpacker {
97            inner: InnerBuf::new(),
98        }
99    }
100}
101
102impl<T> io::Write for Unpacker<T>
103where
104    T: io::Write,
105{
106    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
107        self.inner.write(buf)
108    }
109
110    fn flush(&mut self) -> io::Result<()> {
111        self.inner.flush()
112    }
113}
114
115impl<T> io::Read for Unpacker<T>
116where
117    T: io::Read,
118{
119    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
120        self.inner.read(buf)
121    }
122}
123
124#[derive(Debug)]
125pub struct UnpackerIter<R>(R);
126
127impl<R> UnpackerIter<R> {
128    pub fn new(inner: R) -> Self {
129        UnpackerIter(inner)
130    }
131}
132
133impl<'a, R> Iterator for UnpackerIter<R>
134where
135    R: io::Read,
136{
137    type Item = Value;
138
139    fn next(&mut self) -> Option<Self::Item> {
140        unpack::unpack_value(&mut self.0).ok()
141    }
142}
143
144// impl<'a, R> Unpacker<R>
145// where
146//     R: io::Write,
147// {
148//     pub fn feed_slice(&mut self, slice: &[u8]) -> io::Result<usize> {
149//         self.inner.write(slice)
150//     }
151// }
152
153// impl<'a, R> Unpacker<R>
154// where
155//     R: io::Write + io::Read,
156// {
157//     pub fn feed_each<F>(&mut self, slice: &[u8], f: F) -> io::Result<usize>
158//     where
159//         F: Fn(Value) -> (),
160//     {
161//         let r = self.feed_slice(slice)?;
162//         for val in self.iter() {
163//             f(val)
164//         }
165
166//         Ok(r)
167//     }
168// }
169
170impl<R: io::Read> Unpacker<R> {
171    pub fn unpack_u8(&mut self) -> Result<u8, UnpackError> {
172        unpack::unpack_u8(&mut self.inner)
173    }
174
175    pub fn unpack_u16(&mut self) -> Result<u16, UnpackError> {
176        unpack::unpack_u16(&mut self.inner)
177    }
178
179    pub fn unpack_u32(&mut self) -> Result<u32, UnpackError> {
180        unpack::unpack_u32(&mut self.inner)
181    }
182
183    pub fn unpack_u64(&mut self) -> Result<u64, UnpackError> {
184        unpack::unpack_u64(&mut self.inner)
185    }
186
187    pub fn unpack_i8(&mut self) -> Result<i8, UnpackError> {
188        unpack::unpack_i8(&mut self.inner)
189    }
190
191    pub fn unpack_i16(&mut self) -> Result<i16, UnpackError> {
192        unpack::unpack_i16(&mut self.inner)
193    }
194
195    pub fn unpack_i32(&mut self) -> Result<i32, UnpackError> {
196        unpack::unpack_i32(&mut self.inner)
197    }
198
199    pub fn unpack_i64(&mut self) -> Result<i64, UnpackError> {
200        unpack::unpack_i64(&mut self.inner)
201    }
202
203    pub fn unpack_nil<T>(&mut self) -> Result<Option<T>, UnpackError> {
204        unpack::unpack_nil(&mut self.inner)
205    }
206
207    pub fn unpack_bool(&mut self) -> Result<bool, UnpackError> {
208        unpack::unpack_bool(&mut self.inner)
209    }
210
211    pub fn unpack_string(&mut self) -> Result<String, UnpackError> {
212        unpack::unpack_str(&mut self.inner)
213    }
214
215    pub fn unpack_str_header(&mut self) -> Result<usize, UnpackError> {
216        unpack::unpack_str_header(&mut self.inner)
217    }
218
219    pub fn unpack_ary_header(&mut self) -> Result<usize, UnpackError> {
220        unpack::unpack_ary_header(&mut self.inner)
221    }
222
223    pub fn unpack_map_header(&mut self) -> Result<usize, UnpackError> {
224        unpack::unpack_map_header(&mut self.inner)
225    }
226
227    pub fn unpack_bin_header(&mut self) -> Result<usize, UnpackError> {
228        unpack::unpack_bin_header(&mut self.inner)
229    }
230
231    pub fn unpack_fixext1(&mut self) -> Result<(i8, u8), UnpackError> {
232        unpack::unpack_fixext1(&mut self.inner)
233    }
234
235    pub fn unpack_fixext2(&mut self) -> Result<(i8, [u8; 2]), UnpackError> {
236        unpack::unpack_fixext2(&mut self.inner)
237    }
238
239    pub fn unpack_fixext4(&mut self) -> Result<(i8, [u8; 4]), UnpackError> {
240        unpack::unpack_fixext4(&mut self.inner)
241    }
242
243    pub fn unpack_fixext8(&mut self) -> Result<(i8, [u8; 8]), UnpackError> {
244        unpack::unpack_fixext8(&mut self.inner)
245    }
246
247    pub fn unpack_fixext16(&mut self) -> Result<(i8, [u8; 16]), UnpackError> {
248        unpack::unpack_fixext16(&mut self.inner)
249    }
250
251    pub fn unpack_value(&mut self) -> Result<Value, UnpackError> {
252        unpack::unpack_value(&mut self.inner)
253    }
254}