quoted_string/
error.rs

1//! module containing all errors
2use std::error::{Error as StdError};
3use std::fmt::{self, Display};
4
5#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
6pub enum CoreError {
7    AdvancedFailedAutomaton,
8    QuotedStringAlreadyEnded,
9    UnquoteableCharQuoted,
10    DoesNotStartWithDQuotes,
11    DoesNotEndWithDQuotes,
12    InvalidChar,
13    ZeroSizedValue
14}
15
16impl CoreError {
17
18    pub fn id(&self) -> u8 {
19        use self::CoreError::*;
20        match *self {
21            AdvancedFailedAutomaton => 0,
22            QuotedStringAlreadyEnded => 1,
23            UnquoteableCharQuoted => 2,
24            DoesNotStartWithDQuotes => 3,
25            DoesNotEndWithDQuotes => 4,
26            InvalidChar => 5,
27            ZeroSizedValue => 6,
28        }
29    }
30
31    pub fn from_id(id: u8) -> Option<Self> {
32        use self::CoreError::*;
33        Some(match id {
34            0 => AdvancedFailedAutomaton,
35            1 => QuotedStringAlreadyEnded,
36            2 => UnquoteableCharQuoted,
37            3 => DoesNotStartWithDQuotes,
38            4 => DoesNotEndWithDQuotes,
39            5 => InvalidChar,
40            6 => ZeroSizedValue,
41            _ => return None
42        })
43    }
44}
45impl Display for CoreError {
46    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
47        fter.write_str(self.description())
48    }
49}
50
51impl StdError for CoreError {
52    fn description(&self) -> &'static str {
53        use self::CoreError::*;
54        match *self {
55            AdvancedFailedAutomaton =>
56                "advanced automaton after it entered the failed state",
57            QuotedStringAlreadyEnded =>
58                "continued to use the automaton after the end of the quoted string was found",
59            UnquoteableCharQuoted =>
60                "a char was escaped with a quoted-pair which can not be represented with a quoted-pair",
61            DoesNotStartWithDQuotes =>
62                "quoted string did not start with \"",
63            DoesNotEndWithDQuotes =>
64                "quoted string did not end with \"",
65            InvalidChar =>
66                "char can not be represented in a quoted string (without encoding)",
67            ZeroSizedValue =>
68                "value had a size of zero chars/bytes but has to have at last one"
69        }
70    }
71}