pom/
result.rs

1use std::error;
2use std::fmt::{self, Display};
3
4/// Parser error.
5#[derive(Debug, PartialEq, Clone)]
6pub enum Error {
7	Incomplete,
8	Mismatch {
9		message: String,
10		position: usize,
11	},
12	Conversion {
13		message: String,
14		position: usize,
15	},
16	Expect {
17		message: String,
18		position: usize,
19		inner: Box<Error>,
20	},
21	Custom {
22		message: String,
23		position: usize,
24		inner: Option<Box<Error>>,
25	},
26}
27
28impl error::Error for Error {
29	fn description(&self) -> &'static str {
30		"Parse error"
31	}
32}
33
34impl Display for Error {
35	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36		match self {
37			Error::Incomplete => write!(f, "Incomplete"),
38			Error::Mismatch {
39				ref message,
40				ref position,
41			} => write!(f, "Mismatch at {}: {}", position, message),
42			Error::Conversion {
43				ref message,
44				ref position,
45			} => write!(f, "Conversion failed at {}: {}", position, message),
46			Error::Expect {
47				ref message,
48				ref position,
49				ref inner,
50			} => write!(f, "{} at {}: {}", message, position, inner),
51			Error::Custom {
52				ref message,
53				ref position,
54				inner: Some(ref inner),
55			} => write!(f, "{} at {}, (inner: {})", message, position, inner),
56			Error::Custom {
57				ref message,
58				ref position,
59				inner: None,
60			} => write!(f, "{} at {}", message, position),
61		}
62	}
63}
64
65/// Parser result, `Result<O>` ia alias of `Result<O, pom::Error>`.
66pub type Result<O> = ::std::result::Result<O, Error>;