blob: a9f8c39d5d4436cafd37905539992ad383804bb3 [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
David Tolnayf4aa6b42017-12-31 16:40:33 -05006pub type PResult<'a, O> = Result<(O, Cursor<'a>), ParseError>;
David Tolnayc5ab8c62017-12-26 16:43:39 -05007
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}