Tidy stream_parser

This commit is contained in:
Tangent 128 2018-04-04 19:46:28 -04:00
parent be92d04c09
commit 45012385c2

View file

@ -40,40 +40,29 @@ impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlStreamingParser<S> {
loop { loop {
match T::check_space(&self.buffer) { match T::check_space(&self.buffer) {
Err(err) => {
return Err(ParsingError::EbmlError(err))
},
Ok(None) => { Ok(None) => {
// need to refill buffer, below // need to refill buffer, below
}, },
Ok(Some(_)) => { other => return other.map_err(ParsingError::EbmlError).and_then(move |_| {
return match T::decode_element(&self.buffer) { match T::decode_element(&self.buffer) {
Err(err) => { Err(err) => Err(ParsingError::EbmlError(err)),
Err(ParsingError::EbmlError(err)) Ok(None) => panic!("Buffer was supposed to have enough data to parse element, somehow did not."),
},
Ok(None) => {
// buffer should have the data already
panic!("Buffer was supposed to have enough data to parse element, somehow did not.")
},
Ok(Some((element, element_size))) => { Ok(Some((element, element_size))) => {
self.last_read = element_size; self.last_read = element_size;
return Ok(Async::Ready(Some(element))) Ok(Async::Ready(Some(element)))
} }
} }
} })
} }
match self.stream.poll() { match self.stream.poll().map_err(ParsingError::OtherError) {
Ok(Async::NotReady) => return Ok(Async::NotReady),
Ok(Async::Ready(None)) => return Ok(Async::Ready(None)),
Ok(Async::Ready(Some(chunk))) => { Ok(Async::Ready(Some(chunk))) => {
self.buffer.reserve(chunk.as_ref().len()); self.buffer.reserve(chunk.as_ref().len());
self.buffer.put_slice(chunk.as_ref()); self.buffer.put_slice(chunk.as_ref());
//println!("Read {} into Buffer", chunk.as_ref().len());
// ok can retry decoding now // ok can retry decoding now
} },
Err(err) => return Err(ParsingError::OtherError(err)) other => return other.map(|async| async.map(|_| None))
}; }
} }
} }
} }