piston_meta/
parse_error.rs

1use std::fmt::{ Display, Formatter };
2use std::fmt::Error as FormatError;
3use read_token::{ ParseNumberError, ParseStringError };
4use std::sync::Arc;
5
6use DebugId;
7
8/// Errors reporting expected values.
9#[derive(Debug, PartialEq)]
10pub enum ParseError {
11    /// Whitespace is required.
12    ExpectedWhitespace(DebugId),
13    /// New line is required.
14    ExpectedNewLine(DebugId),
15    /// Something is required.
16    ExpectedSomething(DebugId),
17    /// Expected number.
18    ExpectedNumber(DebugId),
19    /// Error when parsing float.
20    ParseNumberError(ParseNumberError, DebugId),
21    /// Expected text.
22    ExpectedText(DebugId),
23    /// Empty text not allowed.
24    EmptyTextNotAllowed(DebugId),
25    /// Invalid string format.
26    ParseStringError(ParseStringError, DebugId),
27    /// Expected token.
28    ExpectedTag(Arc<String>, DebugId),
29    /// Did not expected token.
30    DidNotExpectTag(Arc<String>, DebugId),
31    /// An invalid rule.
32    InvalidRule(&'static str, DebugId),
33    /// No rules are specified.
34    NoRules,
35    /// Expected to reach the end.
36    ExpectedEnd,
37    /// Conversion error.
38    Conversion(String),
39}
40
41impl Display for ParseError {
42    fn fmt(&self, fmt: &mut Formatter) -> Result<(), FormatError> {
43        match self {
44            &ParseError::ExpectedWhitespace(debug_id) =>
45                write!(fmt, "#{}, Expected whitespace", debug_id)?,
46            &ParseError::ExpectedNewLine(debug_id) =>
47                write!(fmt, "#{}, Expected new line", debug_id)?,
48            &ParseError::ExpectedSomething(debug_id) =>
49                write!(fmt, "#{}, Expected something", debug_id)?,
50            &ParseError::ExpectedNumber(debug_id) =>
51                write!(fmt, "#{}, Expected number", debug_id)?,
52            &ParseError::ParseNumberError(ref err, debug_id) =>
53                write!(fmt, "#{}, Invalid number format: {}", debug_id, err)?,
54            &ParseError::ExpectedTag(ref token, debug_id) =>
55                write!(fmt, "#{}, Expected: `{}`", debug_id, token)?,
56            &ParseError::DidNotExpectTag(ref token, debug_id) =>
57                write!(fmt, "#{}, Did not expect: `{}`", debug_id, token)?,
58            &ParseError::ExpectedText(debug_id) =>
59                write!(fmt, "#{}, Expected text", debug_id)?,
60            &ParseError::EmptyTextNotAllowed(debug_id) =>
61                write!(fmt, "#{}, Empty text not allowed", debug_id)?,
62            &ParseError::ParseStringError(err, debug_id) =>
63                write!(fmt, "#{}, Invalid string format: {}", debug_id, err)?,
64            &ParseError::InvalidRule(msg, debug_id) =>
65                write!(fmt, "#{}, Invalid rule: {}", debug_id, msg)?,
66            &ParseError::NoRules =>
67                write!(fmt, "No rules are specified")?,
68            &ParseError::ExpectedEnd =>
69                write!(fmt, "Expected end")?,
70            &ParseError::Conversion(ref msg) =>
71                write!(fmt, "Conversion, {}", msg)?,
72        }
73        Ok(())
74    }
75}