1use crate::{ByteCount, Decode, Encode, Eos, ErrorKind, Result, SizedEncode};
3use std::cmp;
4use std::mem;
5use trackable::error::ErrorKindExt;
6
7#[derive(Debug)]
23pub struct BytesEncoder<B = Vec<u8>> {
24 bytes: Option<B>,
25 offset: usize,
26}
27impl<B> BytesEncoder<B> {
28 pub fn new() -> Self {
30 Self::default()
31 }
32}
33impl<B> Default for BytesEncoder<B> {
34 fn default() -> Self {
35 BytesEncoder {
36 bytes: None,
37 offset: 0,
38 }
39 }
40}
41impl<B: AsRef<[u8]>> Encode for BytesEncoder<B> {
42 type Item = B;
43
44 fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
45 let mut size = 0;
46 let drop_item = if let Some(ref b) = self.bytes {
47 size = cmp::min(buf.len(), b.as_ref().len() - self.offset);
48 buf[..size].copy_from_slice(&b.as_ref()[self.offset..][..size]);
49 self.offset += size;
50 if self.offset == b.as_ref().len() {
51 true
52 } else {
53 track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos;
54 buf.len(), size, self.offset, b.as_ref().len());
55 false
56 }
57 } else {
58 false
59 };
60 if drop_item {
61 self.bytes = None;
62 }
63 Ok(size)
64 }
65
66 fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
67 track_assert!(self.is_idle(), ErrorKind::EncoderFull);
68 self.bytes = Some(item);
69 self.offset = 0;
70 Ok(())
71 }
72
73 fn requiring_bytes(&self) -> ByteCount {
74 ByteCount::Finite(self.exact_requiring_bytes())
75 }
76
77 fn is_idle(&self) -> bool {
78 self.bytes.is_none()
79 }
80}
81impl<B: AsRef<[u8]>> SizedEncode for BytesEncoder<B> {
82 fn exact_requiring_bytes(&self) -> u64 {
83 self.bytes
84 .as_ref()
85 .map_or(0, |b| b.as_ref().len() - self.offset) as u64
86 }
87}
88
89#[derive(Debug, Default)]
119pub struct CopyableBytesDecoder<B> {
120 bytes: B,
121 offset: usize,
122}
123impl<B> CopyableBytesDecoder<B> {
124 pub fn new(bytes: B) -> Self {
126 CopyableBytesDecoder { bytes, offset: 0 }
127 }
128
129 pub fn inner_ref(&self) -> &B {
131 &self.bytes
132 }
133
134 pub fn inner_mut(&mut self) -> &mut B {
136 &mut self.bytes
137 }
138
139 pub fn into_inner(self) -> B {
141 self.bytes
142 }
143}
144impl<B: AsRef<[u8]> + AsMut<[u8]> + Copy> Decode for CopyableBytesDecoder<B> {
145 type Item = B;
146
147 fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
148 let size = cmp::min(buf.len(), self.bytes.as_ref().len() - self.offset);
149 self.bytes.as_mut()[self.offset..][..size].copy_from_slice(&buf[..size]);
150 self.offset += size;
151 if self.offset != self.bytes.as_mut().len() {
152 track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos;
153 self.offset, self.bytes.as_ref().len());
154 }
155 Ok(size)
156 }
157
158 fn finish_decoding(&mut self) -> Result<Self::Item> {
159 track_assert_eq!(
160 self.offset,
161 self.bytes.as_ref().len(),
162 ErrorKind::IncompleteDecoding
163 );
164 self.offset = 0;
165 Ok(self.bytes)
166 }
167
168 fn requiring_bytes(&self) -> ByteCount {
169 ByteCount::Finite((self.bytes.as_ref().len() - self.offset) as u64)
170 }
171
172 fn is_idle(&self) -> bool {
173 self.offset == self.bytes.as_ref().len()
174 }
175}
176
177#[derive(Debug)]
196pub struct BytesDecoder<B = Vec<u8>> {
197 bytes: Option<B>,
198 offset: usize,
199}
200impl<B: AsRef<[u8]> + AsMut<[u8]>> BytesDecoder<B> {
201 pub fn new(bytes: B) -> Self {
203 BytesDecoder {
204 bytes: Some(bytes),
205 offset: 0,
206 }
207 }
208
209 pub fn set_bytes(&mut self, bytes: B) {
211 self.bytes = Some(bytes);
212 self.offset = 0;
213 }
214
215 fn exact_requiring_bytes(&self) -> u64 {
216 self.bytes
217 .as_ref()
218 .map_or(0, |b| b.as_ref().len() - self.offset) as u64
219 }
220
221 fn buf_len(&self) -> usize {
222 self.bytes.as_ref().map_or(0, |b| b.as_ref().len())
223 }
224}
225impl<B> Default for BytesDecoder<B> {
226 fn default() -> Self {
227 BytesDecoder {
228 bytes: None,
229 offset: 0,
230 }
231 }
232}
233impl<B: AsRef<[u8]> + AsMut<[u8]>> Decode for BytesDecoder<B> {
234 type Item = B;
235
236 fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
237 let size = {
238 let bytes = track_assert_some!(self.bytes.as_mut(), ErrorKind::DecoderTerminated);
239 let size = cmp::min(buf.len(), bytes.as_ref().len() - self.offset);
240 bytes.as_mut()[self.offset..][..size].copy_from_slice(&buf[..size]);
241 self.offset += size;
242 size
243 };
244 if self.exact_requiring_bytes() != 0 {
245 track_assert!(!eos.is_reached(), ErrorKind::UnexpectedEos; self.offset, self.buf_len());
246 }
247 Ok(size)
248 }
249
250 fn finish_decoding(&mut self) -> Result<Self::Item> {
251 track_assert_eq!(
252 self.exact_requiring_bytes(),
253 0,
254 ErrorKind::IncompleteDecoding
255 );
256 let bytes = track_assert_some!(self.bytes.take(), ErrorKind::DecoderTerminated);
257 Ok(bytes)
258 }
259
260 fn requiring_bytes(&self) -> ByteCount {
261 ByteCount::Finite(self.exact_requiring_bytes())
262 }
263
264 fn is_idle(&self) -> bool {
265 self.exact_requiring_bytes() == 0
266 }
267}
268
269#[derive(Debug, Default)]
290pub struct RemainingBytesDecoder {
291 buf: Vec<u8>,
292 eos: bool,
293}
294impl RemainingBytesDecoder {
295 pub fn new() -> Self {
297 Self::default()
298 }
299}
300impl Decode for RemainingBytesDecoder {
301 type Item = Vec<u8>;
302
303 fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
304 if self.eos {
305 return Ok(0);
306 }
307
308 if let Some(remaining) = eos.remaining_bytes().to_u64() {
309 self.buf.reserve_exact(buf.len() + remaining as usize);
310 }
311 self.buf.extend_from_slice(buf);
312 self.eos = eos.is_reached();
313 Ok(buf.len())
314 }
315
316 fn finish_decoding(&mut self) -> Result<Self::Item> {
317 track_assert!(self.eos, ErrorKind::IncompleteDecoding);
318 self.eos = false;
319 let bytes = mem::take(&mut self.buf);
320 Ok(bytes)
321 }
322
323 fn requiring_bytes(&self) -> ByteCount {
324 if self.eos {
325 ByteCount::Finite(0)
326 } else {
327 ByteCount::Infinite
328 }
329 }
330
331 fn is_idle(&self) -> bool {
332 self.eos
333 }
334}
335
336#[derive(Debug)]
337struct Utf8Bytes<T>(T);
338impl<T: AsRef<str>> AsRef<[u8]> for Utf8Bytes<T> {
339 fn as_ref(&self) -> &[u8] {
340 self.0.as_ref().as_bytes()
341 }
342}
343
344#[derive(Debug)]
360pub struct Utf8Encoder<S = String>(BytesEncoder<Utf8Bytes<S>>);
361impl<S> Utf8Encoder<S> {
362 pub fn new() -> Self {
364 Utf8Encoder(BytesEncoder::new())
365 }
366}
367impl<S> Default for Utf8Encoder<S> {
368 fn default() -> Self {
369 Self::new()
370 }
371}
372impl<S: AsRef<str>> Encode for Utf8Encoder<S> {
373 type Item = S;
374
375 fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
376 track!(self.0.encode(buf, eos))
377 }
378
379 fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
380 track!(self.0.start_encoding(Utf8Bytes(item)))
381 }
382
383 fn requiring_bytes(&self) -> ByteCount {
384 self.0.requiring_bytes()
385 }
386
387 fn is_idle(&self) -> bool {
388 self.0.is_idle()
389 }
390}
391impl<S: AsRef<str>> SizedEncode for Utf8Encoder<S> {
392 fn exact_requiring_bytes(&self) -> u64 {
393 self.0.exact_requiring_bytes()
394 }
395}
396
397#[derive(Debug, Default)]
411pub struct Utf8Decoder<D = RemainingBytesDecoder>(D);
412impl Utf8Decoder<RemainingBytesDecoder> {
413 pub fn new() -> Self {
415 Utf8Decoder(RemainingBytesDecoder::new())
416 }
417}
418impl<D> Utf8Decoder<D>
419where
420 D: Decode<Item = Vec<u8>>,
421{
422 pub fn with_bytes_decoder(bytes_decoder: D) -> Self {
424 Utf8Decoder(bytes_decoder)
425 }
426
427 pub fn inner_ref(&self) -> &D {
429 &self.0
430 }
431
432 pub fn inner_mut(&mut self) -> &mut D {
434 &mut self.0
435 }
436
437 pub fn into_inner(self) -> D {
439 self.0
440 }
441}
442impl<D> Decode for Utf8Decoder<D>
443where
444 D: Decode<Item = Vec<u8>>,
445{
446 type Item = String;
447
448 fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
449 track!(self.0.decode(buf, eos))
450 }
451
452 fn finish_decoding(&mut self) -> Result<Self::Item> {
453 let b = track!(self.0.finish_decoding())?;
454 let s = track!(String::from_utf8(b).map_err(|e| ErrorKind::InvalidInput.cause(e)))?;
455 Ok(s)
456 }
457
458 fn requiring_bytes(&self) -> ByteCount {
459 self.0.requiring_bytes()
460 }
461
462 fn is_idle(&self) -> bool {
463 self.0.is_idle()
464 }
465}
466
467#[cfg(test)]
468mod test {
469 use super::*;
470 use crate::io::{IoDecodeExt, IoEncodeExt};
471 use crate::{Encode, EncodeExt, ErrorKind};
472
473 #[test]
474 fn bytes_decoder_works() {
475 let mut decoder = BytesDecoder::new([0; 3]);
476 assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));
477
478 let mut input = b"foobar".as_ref();
479 let item = decoder.decode_exact(&mut input).unwrap();
480 assert_eq!(item.as_ref(), b"foo");
481 assert_eq!(decoder.requiring_bytes().to_u64(), Some(0));
482
483 assert_eq!(
484 decoder.decode_exact(&mut input).err().map(|e| *e.kind()),
485 Some(ErrorKind::DecoderTerminated)
486 );
487 }
488
489 #[test]
490 fn utf8_encoder_works() {
491 let mut buf = Vec::new();
492 let mut encoder = Utf8Encoder::with_item("foo").unwrap();
493 encoder.encode_all(&mut buf).unwrap();
494 assert!(encoder.is_idle());
495 assert_eq!(buf, b"foo");
496 }
497}