Make EbmlStreamingParser use WebmetroErrors specifically
This commit is contained in:
parent
a9932ee6a1
commit
bf56789810
3 changed files with 19 additions and 39 deletions
|
@ -1,35 +1,11 @@
|
|||
use std::{
|
||||
error::Error,
|
||||
fmt::{Display, Formatter, Result as FmtResult}
|
||||
};
|
||||
|
||||
use bytes::BytesMut;
|
||||
use bytes::BufMut;
|
||||
use futures::Async;
|
||||
use futures::stream::Stream;
|
||||
|
||||
use ebml::EbmlError;
|
||||
use ebml::EbmlEventSource;
|
||||
use ebml::FromEbml;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParsingError<E> {
|
||||
EbmlError(EbmlError),
|
||||
OtherError(E)
|
||||
}
|
||||
impl<E: Display + Error> Display for ParsingError<E> {
|
||||
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||
write!(f, "Parsing error: {}", self.description())
|
||||
}
|
||||
}
|
||||
impl<E: Error> Error for ParsingError<E> {
|
||||
fn description(&self) -> &str {
|
||||
match self {
|
||||
&ParsingError::EbmlError(ref err) => err.description(),
|
||||
&ParsingError::OtherError(ref err) => err.description()
|
||||
}
|
||||
}
|
||||
}
|
||||
use error::WebmetroError;
|
||||
|
||||
pub struct EbmlStreamingParser<S> {
|
||||
stream: S,
|
||||
|
@ -47,10 +23,10 @@ pub trait StreamEbml where Self: Sized + Stream, Self::Item: AsRef<[u8]> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I>> StreamEbml for S {}
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I, Error = WebmetroError>> StreamEbml for S {}
|
||||
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlStreamingParser<S> {
|
||||
pub fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, ParsingError<S::Error>> {
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I, Error = WebmetroError>> EbmlStreamingParser<S> {
|
||||
pub fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, WebmetroError> {
|
||||
// release buffer from previous event
|
||||
self.buffer.advance(self.last_read);
|
||||
self.last_read = 0;
|
||||
|
@ -60,9 +36,9 @@ impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlStreamingParser<S> {
|
|||
Ok(None) => {
|
||||
// need to refill buffer, below
|
||||
},
|
||||
other => return other.map_err(ParsingError::EbmlError).and_then(move |_| {
|
||||
other => return other.map_err(WebmetroError::EbmlError).and_then(move |_| {
|
||||
match T::decode_element(&self.buffer) {
|
||||
Err(err) => Err(ParsingError::EbmlError(err)),
|
||||
Err(err) => Err(WebmetroError::EbmlError(err)),
|
||||
Ok(None) => panic!("Buffer was supposed to have enough data to parse element, somehow did not."),
|
||||
Ok(Some((element, element_size))) => {
|
||||
self.last_read = element_size;
|
||||
|
@ -72,7 +48,7 @@ impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlStreamingParser<S> {
|
|||
})
|
||||
}
|
||||
|
||||
match self.stream.poll().map_err(ParsingError::OtherError) {
|
||||
match self.stream.poll() {
|
||||
Ok(Async::Ready(Some(chunk))) => {
|
||||
self.buffer.reserve(chunk.as_ref().len());
|
||||
self.buffer.put_slice(chunk.as_ref());
|
||||
|
@ -84,10 +60,10 @@ impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlStreamingParser<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I>> EbmlEventSource for EbmlStreamingParser<S> {
|
||||
type Error = ParsingError<S::Error>;
|
||||
impl<I: AsRef<[u8]>, S: Stream<Item = I, Error = WebmetroError>> EbmlEventSource for EbmlStreamingParser<S> {
|
||||
type Error = WebmetroError;
|
||||
|
||||
fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, Self::Error> {
|
||||
fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result<Async<Option<T>>, WebmetroError> {
|
||||
return EbmlStreamingParser::poll_event(self);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue