blob: 72a25e8847e56e826ba211616a544026b98e3406 [file] [log] [blame]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001use std::error::Error;
2use cursor::Cursor;
3use std::fmt::{self, Display};
4
5/// The result of a parser
6pub type PResult<'a, O> = Result<(Cursor<'a>, O), ParseError>;
7
8/// An error with a default error message.
9///
10/// NOTE: We should provide better error messages in the future.
11pub fn parse_error<O>() -> PResult<'static, O> {
12 Err(ParseError(None))
13}
14
15#[derive(Debug)]
16pub struct ParseError(Option<String>);
17
18impl Error for ParseError {
19 fn description(&self) -> &str {
20 match self.0 {
21 Some(ref desc) => desc,
22 None => "failed to parse",
23 }
24 }
25}
26
27impl Display for ParseError {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 <str as fmt::Display>::fmt(self.description(), f)
30 }
31}
32
David Tolnayc5ab8c62017-12-26 16:43:39 -050033impl ParseError {
34 // For syn use only. Not public API.
35 #[doc(hidden)]
36 pub fn new<T: Into<String>>(msg: T) -> Self {
37 ParseError(Some(msg.into()))
38 }
39}