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;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ParseErrorKind {
InvalidYear,
InvalidMonth,
InvalidDay,
InvalidHour,
InvalidMinute,
InvalidSeconds,
InvalidNanoseconds,
InvalidFormat,
InvalidToken,
InvalidLowValue,
InvalidHighValue,
InvalidDate,
InvalidTime,
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."),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct ParseError {
pub error_kind: ParseErrorKind,
pub position_begin: usize,
pub position_end: usize,
}
impl ParseError {
pub fn invalid(error_kind: ParseErrorKind,position: usize,length: usize) -> ParseError {
ParseError {
position_begin: position,
position_end: position + length,
error_kind: error_kind,
}
}
pub fn invalid_format(position: usize,length: usize) -> ParseError {
ParseError {
position_begin: position,
position_end: position + length,
error_kind: ParseErrorKind::InvalidFormat,
}
}
pub fn invalid_token(position: usize,length: usize) -> ParseError {
ParseError {
position_begin: position,
position_end: position + length,
error_kind: ParseErrorKind::InvalidToken,
}
}
pub fn invalid_low_value(position: usize,length: usize) -> ParseError {
ParseError {
position_begin: position,
position_end: position + length,
error_kind: ParseErrorKind::InvalidLowValue,
}
}
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);
}
}
pub type ParseResult<T> = Result<T, ParseError>;