use std::error::Error;
use buffer::Cursor;
use std::fmt::{self, Display};
pub type PResult<'a, O> = Result<(O, Cursor<'a>), ParseError>;
pub fn parse_error<O>() -> PResult<'static, O> {
Err(ParseError(None))
}
#[derive(Debug)]
pub struct ParseError(Option<String>);
impl Error for ParseError {
fn description(&self) -> &str {
match self.0 {
Some(ref desc) => desc,
None => "failed to parse",
}
}
}
impl Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self.description(), f)
}
}
impl ParseError {
#[doc(hidden)]
pub fn new<T: Into<String>>(msg: T) -> Self {
ParseError(Some(msg.into()))
}
}