1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::error::Error;
use std::fmt;

/// Error kind of parsing.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ParseErrorKind {
    /// Unable to parse year.
    InvalidYear,
    /// Unable to parse month.
    InvalidMonth,
    /// Unable to parse day.
    InvalidDay,
    /// Unable to parse hour.
    InvalidHour,
    /// Unable to parse minutes.
    InvalidMinute,
    /// Unable to parse seconds.
    InvalidSeconds,
    /// Unable to parse nanoseconds.
    InvalidNanoseconds,
    /// Invalid format.
    InvalidFormat,
    /// Unexpected token.
    InvalidToken,
    /// Invalid value range. Value is too low.
    InvalidLowValue,
    /// Invalid value range. Value is too high.
    InvalidHighValue,
    /// Date is not exists.
    InvalidDate,
    /// Time is not exists.
    InvalidTime,
    /// Date is parsed, but there is some text after date.
    StringNotEnded,
}

impl Error for ParseErrorKind {
    fn description(&self) -> &str {
        match self {
            &ParseErrorKind::InvalidYear => "Unable to parse year.",
            &ParseErrorKind::InvalidMonth => "Unable to parse month.",
            &ParseErrorKind::InvalidDay => "Unable to parse day.",
            &ParseErrorKind::InvalidHour => "Unable to parse hour.",
            &ParseErrorKind::InvalidMinute => "Unable to parse minutes.",
            &ParseErrorKind::InvalidSeconds => "Unable to parse seconds.",
            &ParseErrorKind::InvalidNanoseconds => "Unable to parse nanoseconds.",
            &ParseErrorKind::InvalidFormat => "Invalid format.",
            &ParseErrorKind::InvalidToken => "Unexpected token.",
            &ParseErrorKind::InvalidLowValue => "Invalid value range. Value is too low.",
            &ParseErrorKind::InvalidHighValue => "Invalid value range. Value is too high.",
            &ParseErrorKind::InvalidDate => "Date is not exists.",
            &ParseErrorKind::InvalidTime => "Time is not exists.",
            &ParseErrorKind::StringNotEnded => "Date is parsed, but there is some text after date.",
        }
    }
}

impl fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &ParseErrorKind::InvalidYear => write!(f,"Unable to parse year."),
            &ParseErrorKind::InvalidMonth => write!(f,"Unable to parse month."),
            &ParseErrorKind::InvalidDay => write!(f,"Unable to parse day."),
            &ParseErrorKind::InvalidHour => write!(f,"Unable to parse hour."),
            &ParseErrorKind::InvalidMinute => write!(f,"Unable to parse minutes."),
            &ParseErrorKind::InvalidSeconds => write!(f,"Unable to parse seconds."),
            &ParseErrorKind::InvalidNanoseconds => write!(f,"Unable to parse nanoseconds."),
            &ParseErrorKind::InvalidFormat => write!(f,"Invalid write."),
            &ParseErrorKind::InvalidToken => write!(f,"Unexpected token."),
            &ParseErrorKind::InvalidLowValue => write!(f,"Invalid value range. Value is too low."),
            &ParseErrorKind::InvalidHighValue => write!(f,"Invalid value range. Value is too high."),
            &ParseErrorKind::InvalidDate => write!(f,"Date is not exists."),
            &ParseErrorKind::InvalidTime => write!(f,"Time is not exists."),
            &ParseErrorKind::StringNotEnded => write!(f,"Date is parsed, but there is some text after date."),
        }
    }
}

/// An error from the parse_* functions.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct ParseError {
    /// Kind of parse error.
    pub error_kind: ParseErrorKind,
    /// Start of error position.
    pub position_begin: usize,
    /// End of error position.
    pub position_end: usize,
}
impl ParseError {
    /// Returns error.
    pub fn invalid(error_kind: ParseErrorKind,position: usize,length: usize) -> ParseError {
        ParseError {
            position_begin: position,
            position_end: position + length,
            error_kind: error_kind,
        }
    }
    /// Returns error due the invalid format.
    pub fn invalid_format(position: usize,length: usize) -> ParseError {
        ParseError {
            position_begin: position,
            position_end: position + length,
            error_kind: ParseErrorKind::InvalidFormat,
        }
    }
    /// Returns error due the invalid token.
    pub fn invalid_token(position: usize,length: usize) -> ParseError {
        ParseError {
            position_begin: position,
            position_end: position + length,
            error_kind: ParseErrorKind::InvalidToken,
        }
    }
    /// Returns error due the value is too low.
    pub fn invalid_low_value(position: usize,length: usize) -> ParseError {
        ParseError {
            position_begin: position,
            position_end: position + length,
            error_kind: ParseErrorKind::InvalidLowValue,
        }
    }
    /// Returns error due the value is too high.
    pub fn invalid_high_value(position: usize,length: usize) -> ParseError {
        ParseError {
            position_begin: position,
            position_end: position + length,
            error_kind: ParseErrorKind::InvalidHighValue,
        }
    }
}

impl Error for ParseError {
    fn description(&self) -> &str {
        return self.error_kind.description();
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        return self.error_kind.fmt(f);
    }
}

/// Same to `Result<T, ParseError>`.
pub type ParseResult<T> = Result<T, ParseError>;