Skip to main content

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    /// Convert this error to a static string representation
80    pub fn to_static_string(&self) -> &'static str {
81        match *self {
82            ParseError::ImplAssert => "Implementation error: this error must not throw.",
83
84            ParseError::NonAsciiCodePoint => "Code point was not ASCII",
85
86            ParseError::ParseFloat => "Invalid float while parsing fraction part.",
87
88            ParseError::AbruptEnd { .. } => "Parsing ended abruptly.",
89
90            ParseError::InvalidEnd => "Unexpected character found after parsing was completed.",
91            // Date related errors
92            ParseError::InvalidMonthRange => "Parsed month value not in a valid range.",
93
94            ParseError::InvalidDayRange => "Parsed day value not in a valid range.",
95
96            ParseError::DateYear => "Invalid chracter while parsing year value.",
97
98            ParseError::DateExtendedYear => "Invalid character while parsing extended year value.",
99
100            ParseError::DateMonth => "Invalid character while parsing month value.",
101
102            ParseError::DateDay => "Invalid character while parsing day value.",
103
104            ParseError::DateUnexpectedEnd => "Unexpected end while parsing a date value.",
105
106            ParseError::TimeRequired => "Time is required.",
107
108            ParseError::TimeHour => "Invalid character while parsing hour value.",
109
110            ParseError::TimeMinuteSecond => {
111                "Invalid character while parsing minute/second value in (0, 59] range."
112            }
113
114            ParseError::TimeSecond => {
115                "Invalid character while parsing second value in (0, 60] range."
116            }
117
118            ParseError::FractionPart => "Invalid character while parsing fraction part value.",
119
120            ParseError::DateSeparator => "Invalid character while parsing date separator.",
121
122            ParseError::TimeSeparator => "Invalid character while parsing time separator.",
123
124            ParseError::DecimalSeparator => "Invalid character while parsing decimal separator.",
125            // Annotation Related Errors
126            ParseError::InvalidAnnotation => "Invalid annotation.",
127
128            ParseError::AnnotationOpen => "Invalid annotation open character.",
129
130            ParseError::AnnotationClose => "Invalid annotation close character.",
131
132            ParseError::AnnotationChar => "Invalid annotation character.",
133
134            ParseError::AnnotationKeyValueSeparator => {
135                "Invalid annotation key-value separator character."
136            }
137
138            ParseError::AnnotationKeyLeadingChar => "Invalid annotation key leading character.",
139
140            ParseError::AnnotationKeyChar => "Invalid annotation key character.",
141
142            ParseError::AnnotationValueCharPostHyphen => {
143                "Expected annotation value character must exist after hyphen."
144            }
145
146            ParseError::AnnotationValueChar => "Invalid annotation value character.",
147
148            ParseError::InvalidMinutePrecisionOffset => "Offset must be minute precision",
149
150            ParseError::CriticalDuplicateCalendar => {
151                "Duplicate calendars cannot be provided when one is critical."
152            }
153
154            ParseError::UnrecognizedCritical => "Unrecognized annoation is marked as critical.",
155
156            ParseError::TzLeadingChar => "Invalid time zone leading character.",
157
158            ParseError::IanaCharPostSeparator => "Expected time zone character after '/'.",
159
160            ParseError::IanaChar => "Invalid IANA time zone character after '/'.",
161
162            ParseError::UtcTimeSeparator => "Invalid time zone character after '/'.",
163
164            ParseError::OffsetNeedsSign => "UTC offset needs a sign",
165
166            ParseError::MonthDayHyphen => "MonthDay must begin with a month or '--'",
167
168            ParseError::DurationDisgnator => "Invalid duration designator.",
169
170            ParseError::DurationValueExceededRange => {
171                "Provided Duration field value exceeds supported range."
172            }
173
174            ParseError::DateDurationPartOrder => "Invalid date duration part order.",
175
176            ParseError::TimeDurationPartOrder => "Invalid time duration part order.",
177
178            ParseError::TimeDurationDesignator => "Invalid time duration designator.",
179
180            ParseError::AmbiguousTimeMonthDay => "Time is ambiguous with MonthDay",
181
182            ParseError::AmbiguousTimeYearMonth => "Time is ambiguous with YearMonth",
183
184            ParseError::InvalidMonthDay => "MonthDay was not valid.",
185        }
186    }
187}
188
189impl fmt::Display for ParseError {
190    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
191        if let ParseError::AbruptEnd { location } = *self {
192            write!(f, "Parsing ended abruptly while parsing {location}")
193        } else {
194            f.write_str(self.to_static_string())
195        }
196    }
197}