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
use TokenReaderError; use bytes::float::ReadVarFloat; use bytes::strings::ReadStr; use bytes::varnum::ReadVarNum; use std::io::Cursor; pub struct VarFloatDecoder { data: Cursor<Vec<u8>>, } impl VarFloatDecoder { pub fn new(data: Vec<u8>) -> Self { VarFloatDecoder { data: Cursor::new(data), } } } impl Iterator for VarFloatDecoder { type Item = Result<Option<binjs_shared::F64>, TokenReaderError>; fn next(&mut self) -> Option<Self::Item> { if self.data.position() == self.data.get_ref().len() as u64 { return None; } match self.data.read_maybe_varfloat() { Ok(Some(result)) => Some(Ok(Some(result.into()))), Ok(None) => Some(Ok(None)), Err(err) => Some(Err(TokenReaderError::ReadError(err))), } } } pub struct MaybeVarNumDecoder { data: Cursor<Vec<u8>>, } impl MaybeVarNumDecoder { pub fn new(data: Vec<u8>) -> Self { MaybeVarNumDecoder { data: Cursor::new(data), } } } impl Iterator for MaybeVarNumDecoder { type Item = Result<Option<u32>, TokenReaderError>; fn next(&mut self) -> Option<Self::Item> { if self.data.position() == self.data.get_ref().len() as u64 { return None; } match self.data.read_maybe_varnum() { Ok(result) => Some(Ok(result)), Err(err) => Some(Err(TokenReaderError::ReadError(err))), } } } pub struct StringDecoder { data: Cursor<Vec<u8>>, lengths: Cursor<Vec<u8>>, } impl StringDecoder { pub fn try_new( data: Option<Vec<u8>>, lengths: Option<Vec<u8>>, ) -> Result<StringDecoder, TokenReaderError> { match (data, lengths) { (Some(data), Some(lengths)) => Ok(Self::new(data, lengths)), (None, None) => Ok(Self::new(vec![], vec![])), _ => Err(TokenReaderError::BadStringDecoder), } } pub fn new(data: Vec<u8>, lengths: Vec<u8>) -> Self { StringDecoder { data: Cursor::new(data), lengths: Cursor::new(lengths), } } } impl Iterator for StringDecoder { type Item = Result<Option<String>, TokenReaderError>; fn next(&mut self) -> Option<Self::Item> { if self.lengths.position() == self.lengths.get_ref().len() as u64 { debug!(target: "read", "StringDecoder::next - end reached"); return None; } fn aux(myself: &mut StringDecoder) -> Result<Option<String>, TokenReaderError> { let byte_len = myself .lengths .read_maybe_varnum() .map_err(TokenReaderError::ReadError)?; match byte_len { None => Ok(None), Some(byte_len) => { debug!(target: "read", "StringDecoder::next - byte length: {}", byte_len); let value = myself .data .read_string(byte_len as usize) .map_err(TokenReaderError::ReadError)?; debug!(target: "read", "StringDecoder::next - value: {:?}", value); Ok(Some(value)) } } } Some(aux(self)) } }