ixdtf/
error.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5//! An error enum for representing `ixdtf` parsing errors.
6
7use core::fmt;
8
9#[non_exhaustive]
10#[derive(PartialEq, Clone, Copy, Debug)]
11/// The error returned by `ixdtf`'s parsers.
12pub enum ParseError {
13    ImplAssert,
14    NonAsciiCodePoint,
15    ParseFloat,
16    AbruptEnd { location: &'static str },
17    InvalidEnd,
18
19    // Date related errors
20    InvalidMonthRange,
21    InvalidDayRange,
22    DateYear,
23    DateExtendedYear,
24    DateMonth,
25    DateDay,
26    DateUnexpectedEnd,
27
28    // Time Related errors
29    TimeRequired,
30    TimeHour,
31    TimeMinuteSecond,
32    TimeSecond,
33    FractionPart,
34    DateSeparator,
35    TimeSeparator,
36    DecimalSeparator,
37
38    // Annotation Related Errors
39    InvalidAnnotation,
40    AnnotationOpen,
41    AnnotationClose,
42    AnnotationChar,
43    AnnotationKeyValueSeparator,
44    AnnotationKeyLeadingChar,
45    AnnotationKeyChar,
46    AnnotationValueCharPostHyphen,
47    AnnotationValueChar,
48    InvalidMinutePrecisionOffset,
49
50    // Duplicate calendar with critical.
51    CriticalDuplicateCalendar,
52    UnrecognizedCritical,
53
54    // Time Zone Errors
55    TzLeadingChar,
56    IanaCharPostSeparator,
57    IanaChar,
58    UtcTimeSeparator,
59    OffsetNeedsSign,
60
61    // MonthDay Errors
62    MonthDayHyphen,
63
64    // Duration Errors
65    DurationDisgnator,
66    DurationValueExceededRange,
67    DateDurationPartOrder,
68    TimeDurationPartOrder,
69    TimeDurationDesignator,
70
71    AmbiguousTimeMonthDay,
72    AmbiguousTimeYearMonth,
73    InvalidMonthDay,
74}
75
76impl core::error::Error for ParseError {}
77
78impl ParseError {
79    pub(crate) fn abrupt_end(location: &'static str) -> Self {
80        ParseError::AbruptEnd { location }
81    }
82
83    /// Convert this error to a static string representation
84    pub fn to_static_string(&self) -> &'static str {
85        match *self {
86            ParseError::ImplAssert => "Implementation error: this error must not throw.",
87
88            ParseError::NonAsciiCodePoint => "Code point was not ASCII",
89
90            ParseError::ParseFloat => "Invalid float while parsing fraction part.",
91
92            ParseError::AbruptEnd { .. } => "Parsing ended abruptly.",
93
94            ParseError::InvalidEnd => "Unexpected character found after parsing was completed.",
95            // Date related errors
96            ParseError::InvalidMonthRange => "Parsed month value not in a valid range.",
97
98            ParseError::InvalidDayRange => "Parsed day value not in a valid range.",
99
100            ParseError::DateYear => "Invalid chracter while parsing year value.",
101
102            ParseError::DateExtendedYear => "Invalid character while parsing extended year value.",
103
104            ParseError::DateMonth => "Invalid character while parsing month value.",
105
106            ParseError::DateDay => "Invalid character while parsing day value.",
107
108            ParseError::DateUnexpectedEnd => "Unexpected end while parsing a date value.",
109
110            ParseError::TimeRequired => "Time is required.",
111
112            ParseError::TimeHour => "Invalid character while parsing hour value.",
113
114            ParseError::TimeMinuteSecond => {
115                "Invalid character while parsing minute/second value in (0, 59] range."
116            }
117
118            ParseError::TimeSecond => {
119                "Invalid character while parsing second value in (0, 60] range."
120            }
121
122            ParseError::FractionPart => "Invalid character while parsing fraction part value.",
123
124            ParseError::DateSeparator => "Invalid character while parsing date separator.",
125
126            ParseError::TimeSeparator => "Invalid character while parsing time separator.",
127
128            ParseError::DecimalSeparator => "Invalid character while parsing decimal separator.",
129            // Annotation Related Errors
130            ParseError::InvalidAnnotation => "Invalid annotation.",
131
132            ParseError::AnnotationOpen => "Invalid annotation open character.",
133
134            ParseError::AnnotationClose => "Invalid annotation close character.",
135
136            ParseError::AnnotationChar => "Invalid annotation character.",
137
138            ParseError::AnnotationKeyValueSeparator => {
139                "Invalid annotation key-value separator character."
140            }
141
142            ParseError::AnnotationKeyLeadingChar => "Invalid annotation key leading character.",
143
144            ParseError::AnnotationKeyChar => "Invalid annotation key character.",
145
146            ParseError::AnnotationValueCharPostHyphen => {
147                "Expected annotation value character must exist after hyphen."
148            }
149
150            ParseError::AnnotationValueChar => "Invalid annotation value character.",
151
152            ParseError::InvalidMinutePrecisionOffset => "Offset must be minute precision",
153
154            ParseError::CriticalDuplicateCalendar => {
155                "Duplicate calendars cannot be provided when one is critical."
156            }
157
158            ParseError::UnrecognizedCritical => "Unrecognized annoation is marked as critical.",
159
160            ParseError::TzLeadingChar => "Invalid time zone leading character.",
161
162            ParseError::IanaCharPostSeparator => "Expected time zone character after '/'.",
163
164            ParseError::IanaChar => "Invalid IANA time zone character after '/'.",
165
166            ParseError::UtcTimeSeparator => "Invalid time zone character after '/'.",
167
168            ParseError::OffsetNeedsSign => "UTC offset needs a sign",
169
170            ParseError::MonthDayHyphen => "MonthDay must begin with a month or '--'",
171
172            ParseError::DurationDisgnator => "Invalid duration designator.",
173
174            ParseError::DurationValueExceededRange => {
175                "Provided Duration field value exceeds supported range."
176            }
177
178            ParseError::DateDurationPartOrder => "Invalid date duration part order.",
179
180            ParseError::TimeDurationPartOrder => "Invalid time duration part order.",
181
182            ParseError::TimeDurationDesignator => "Invalid time duration designator.",
183
184            ParseError::AmbiguousTimeMonthDay => "Time is ambiguous with MonthDay",
185
186            ParseError::AmbiguousTimeYearMonth => "Time is ambiguous with YearMonth",
187
188            ParseError::InvalidMonthDay => "MonthDay was not valid.",
189        }
190    }
191}
192
193impl fmt::Display for ParseError {
194    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195        if let ParseError::AbruptEnd { location } = *self {
196            write!(f, "Parsing ended abruptly while parsing {location}")
197        } else {
198            f.write_str(self.to_static_string())
199        }
200    }
201}