| 1 |
#[cfg(any(not(test), not(feature = "alloc")))] |
| 2 |
pub(crate) use self::disabled::*; |
| 3 |
#[cfg(all(test, feature = "alloc"))] |
| 4 |
pub(crate) use self::enabled::*; |
| 5 |
|
| 6 |
#[cfg(any(not(test), not(feature = "alloc")))] |
| 7 |
mod disabled { |
| 8 |
#[derive(Clone, Debug)] |
| 9 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 10 |
pub(crate) enum Error {} |
| 11 |
|
| 12 |
impl core::fmt::Display for Error { |
| 13 |
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 14 |
unreachable!() |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
#[cfg(all(test, feature = "alloc"))] |
| 20 |
mod enabled { |
| 21 |
use alloc::boxed::Box; |
| 22 |
|
| 23 |
use crate::error; |
| 24 |
|
| 25 |
// `man zic` says that the max line length including the line |
| 26 |
// terminator is 2048. The `core::str::Lines` iterator doesn't include |
| 27 |
// the terminator, so we subtract 1 to account for that. Note that this |
| 28 |
// could potentially allow one extra byte in the case of a \r\n line |
| 29 |
// terminator, but this seems fine. |
| 30 |
pub(crate) const MAX_LINE_LEN: usize = 2047; |
| 31 |
|
| 32 |
#[derive(Clone, Debug)] |
| 33 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 34 |
pub(crate) enum Error { |
| 35 |
DuplicateLink { name: Box<str> }, |
| 36 |
DuplicateLinkZone { name: Box<str> }, |
| 37 |
DuplicateZone { name: Box<str> }, |
| 38 |
DuplicateZoneLink { name: Box<str> }, |
| 39 |
ExpectedCloseQuote, |
| 40 |
ExpectedColonAfterHour, |
| 41 |
ExpectedColonAfterMinute, |
| 42 |
ExpectedContinuationZoneThreeFields, |
| 43 |
ExpectedContinuationZoneLine { name: Box<str> }, |
| 44 |
ExpectedDotAfterSeconds, |
| 45 |
ExpectedFirstZoneFourFields, |
| 46 |
ExpectedLinkTwoFields, |
| 47 |
ExpectedMinuteAfterHours, |
| 48 |
ExpectedNameBegin, |
| 49 |
ExpectedNanosecondDigits, |
| 50 |
ExpectedNonEmptyAbbreviation, |
| 51 |
ExpectedNonEmptyAt, |
| 52 |
ExpectedNonEmptyName, |
| 53 |
ExpectedNonEmptySave, |
| 54 |
ExpectedNonEmptyZoneName, |
| 55 |
ExpectedNothingAfterTime, |
| 56 |
ExpectedRuleNineFields { got: usize }, |
| 57 |
ExpectedSecondAfterMinutes, |
| 58 |
ExpectedTimeOneHour, |
| 59 |
ExpectedUntilYear, |
| 60 |
ExpectedWhitespaceAfterQuotedField, |
| 61 |
ExpectedZoneNameComponentNoDots { component: Box<str> }, |
| 62 |
FailedContinuationZone, |
| 63 |
FailedLinkLine, |
| 64 |
FailedParseDay, |
| 65 |
FailedParseFieldAt, |
| 66 |
FailedParseFieldFormat, |
| 67 |
FailedParseFieldFrom, |
| 68 |
FailedParseFieldIn, |
| 69 |
FailedParseFieldLetters, |
| 70 |
FailedParseFieldLinkName, |
| 71 |
FailedParseFieldLinkTarget, |
| 72 |
FailedParseFieldName, |
| 73 |
FailedParseFieldOn, |
| 74 |
FailedParseFieldRules, |
| 75 |
FailedParseFieldSave, |
| 76 |
FailedParseFieldStdOff, |
| 77 |
FailedParseFieldTo, |
| 78 |
FailedParseFieldUntil, |
| 79 |
FailedParseHour, |
| 80 |
FailedParseMinute, |
| 81 |
FailedParseMonth, |
| 82 |
FailedParseNanosecond, |
| 83 |
FailedParseSecond, |
| 84 |
FailedParseTimeDuration, |
| 85 |
FailedParseYear, |
| 86 |
FailedRule { name: Box<str> }, |
| 87 |
FailedRuleLine, |
| 88 |
FailedZoneFirst, |
| 89 |
Line { number: usize }, |
| 90 |
LineMaxLength, |
| 91 |
LineNul, |
| 92 |
LineOverflow, |
| 93 |
InvalidAbbreviation, |
| 94 |
InvalidRuleYear { start: i16, end: i16 }, |
| 95 |
InvalidUtf8, |
| 96 |
UnrecognizedAtTimeSuffix, |
| 97 |
UnrecognizedDayOfMonthFormat, |
| 98 |
UnrecognizedDayOfWeek, |
| 99 |
UnrecognizedMonthName, |
| 100 |
UnrecognizedSaveTimeSuffix, |
| 101 |
UnrecognizedTrailingTimeDuration, |
| 102 |
UnrecognizedZicLine, |
| 103 |
} |
| 104 |
|
| 105 |
impl From<Error> for error::Error { |
| 106 |
#[cold] |
| 107 |
#[inline(never)] |
| 108 |
fn from(err: Error) -> error::Error { |
| 109 |
error::ErrorKind::TzZic(err).into() |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl error::IntoError for Error { |
| 114 |
fn into_error(self) -> error::Error { |
| 115 |
self.into() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl core::fmt::Display for Error { |
| 120 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 121 |
use self::Error::*; |
| 122 |
|
| 123 |
match *self { |
| 124 |
DuplicateLink { ref name } => { |
| 125 |
write!(f, "found duplicate link with name `{name}`") |
| 126 |
} |
| 127 |
DuplicateLinkZone { ref name } => write!( |
| 128 |
f, |
| 129 |
"found link with name `{name}` that conflicts \ |
| 130 |
with a zone of the same name", |
| 131 |
), |
| 132 |
DuplicateZone { ref name } => { |
| 133 |
write!(f, "found duplicate zone with name `{name}`") |
| 134 |
} |
| 135 |
DuplicateZoneLink { ref name } => write!( |
| 136 |
f, |
| 137 |
"found zone with name `{name}` that conflicts \ |
| 138 |
with a link of the same name", |
| 139 |
), |
| 140 |
ExpectedCloseQuote => { |
| 141 |
f.write_str("found unclosed quote for field") |
| 142 |
} |
| 143 |
ExpectedColonAfterHour => { |
| 144 |
f.write_str("expected `:` after hours") |
| 145 |
} |
| 146 |
ExpectedColonAfterMinute => { |
| 147 |
f.write_str("expected `:` after minutes") |
| 148 |
} |
| 149 |
ExpectedContinuationZoneLine { ref name } => write!( |
| 150 |
f, |
| 151 |
"expected continuation zone line for `{name}`, \ |
| 152 |
but found end of data instead", |
| 153 |
), |
| 154 |
ExpectedContinuationZoneThreeFields => f.write_str( |
| 155 |
"expected continuation `ZONE` line \ |
| 156 |
to have at least 3 fields", |
| 157 |
), |
| 158 |
ExpectedDotAfterSeconds => { |
| 159 |
f.write_str("expected `.` after seconds") |
| 160 |
} |
| 161 |
ExpectedFirstZoneFourFields => f.write_str( |
| 162 |
"expected first `ZONE` line to have at least 4 fields", |
| 163 |
), |
| 164 |
ExpectedLinkTwoFields => { |
| 165 |
f.write_str("expected exactly 2 fields after `LINK`") |
| 166 |
} |
| 167 |
ExpectedMinuteAfterHours => { |
| 168 |
f.write_str("expected minute digits after `HH:`") |
| 169 |
} |
| 170 |
ExpectedNameBegin => f.write_str( |
| 171 |
"`NAME` field cannot begin with a digit, `+` or `-`, \ |
| 172 |
but found `NAME` that begins with one of those", |
| 173 |
), |
| 174 |
ExpectedNanosecondDigits => { |
| 175 |
f.write_str("expected nanosecond digits after `HH:MM:SS.`") |
| 176 |
} |
| 177 |
ExpectedNonEmptyAbbreviation => f.write_str( |
| 178 |
"empty time zone abbreviations are not allowed", |
| 179 |
), |
| 180 |
ExpectedNonEmptyAt => { |
| 181 |
f.write_str("`AT` field for rule cannot be empty") |
| 182 |
} |
| 183 |
ExpectedNonEmptyName => { |
| 184 |
f.write_str("`NAME` field for rule cannot be empty") |
| 185 |
} |
| 186 |
ExpectedNonEmptySave => { |
| 187 |
f.write_str("`SAVE` field for rule cannot be empty") |
| 188 |
} |
| 189 |
ExpectedNonEmptyZoneName => { |
| 190 |
f.write_str("zone names cannot be empty") |
| 191 |
} |
| 192 |
ExpectedNothingAfterTime => f.write_str( |
| 193 |
"expected no more fields after time of day, \ |
| 194 |
but found at least one", |
| 195 |
), |
| 196 |
ExpectedRuleNineFields { got } => write!( |
| 197 |
f, |
| 198 |
"expected exactly 9 fields for rule, \ |
| 199 |
but found {got} fields", |
| 200 |
), |
| 201 |
ExpectedSecondAfterMinutes => { |
| 202 |
f.write_str("expected second digits after `HH:MM:`") |
| 203 |
} |
| 204 |
ExpectedTimeOneHour => f.write_str( |
| 205 |
"expected time duration to contain \ |
| 206 |
at least one hour digit", |
| 207 |
), |
| 208 |
ExpectedUntilYear => f.write_str("expected at least a year"), |
| 209 |
ExpectedWhitespaceAfterQuotedField => { |
| 210 |
f.write_str("expected whitespace after quoted field") |
| 211 |
} |
| 212 |
ExpectedZoneNameComponentNoDots { ref component } => write!( |
| 213 |
f, |
| 214 |
"component `{component}` in zone name cannot \ |
| 215 |
be \".\" or \"..\"", |
| 216 |
), |
| 217 |
FailedContinuationZone => { |
| 218 |
f.write_str("failed to parse continuation `Zone` line") |
| 219 |
} |
| 220 |
FailedLinkLine => f.write_str("failed to parse `Link` line"), |
| 221 |
FailedParseDay => f.write_str("failed to parse day"), |
| 222 |
FailedParseFieldAt => { |
| 223 |
f.write_str("failed to parse `NAME` field") |
| 224 |
} |
| 225 |
FailedParseFieldFormat => { |
| 226 |
f.write_str("failed to parse `FORMAT` field") |
| 227 |
} |
| 228 |
FailedParseFieldFrom => { |
| 229 |
f.write_str("failed to parse `FROM` field") |
| 230 |
} |
| 231 |
FailedParseFieldIn => { |
| 232 |
f.write_str("failed to parse `IN` field") |
| 233 |
} |
| 234 |
FailedParseFieldLetters => { |
| 235 |
f.write_str("failed to parse `LETTERS` field") |
| 236 |
} |
| 237 |
FailedParseFieldLinkName => { |
| 238 |
f.write_str("failed to parse `LINK` name field") |
| 239 |
} |
| 240 |
FailedParseFieldLinkTarget => { |
| 241 |
f.write_str("failed to parse `LINK` target field") |
| 242 |
} |
| 243 |
FailedParseFieldName => { |
| 244 |
f.write_str("failed to parse `NAME` field") |
| 245 |
} |
| 246 |
FailedParseFieldOn => { |
| 247 |
f.write_str("failed to parse `ON` field") |
| 248 |
} |
| 249 |
FailedParseFieldRules => { |
| 250 |
f.write_str("failed to parse `RULES` field") |
| 251 |
} |
| 252 |
FailedParseFieldSave => { |
| 253 |
f.write_str("failed to parse `SAVE` field") |
| 254 |
} |
| 255 |
FailedParseFieldStdOff => { |
| 256 |
f.write_str("failed to parse `STDOFF` field") |
| 257 |
} |
| 258 |
FailedParseFieldTo => { |
| 259 |
f.write_str("failed to parse `TO` field") |
| 260 |
} |
| 261 |
FailedParseFieldUntil => { |
| 262 |
f.write_str("failed to parse `UNTIL` field") |
| 263 |
} |
| 264 |
FailedParseHour => f.write_str("failed to parse hour"), |
| 265 |
FailedParseMinute => f.write_str("failed to parse minute"), |
| 266 |
FailedParseMonth => f.write_str("failed to parse month"), |
| 267 |
FailedParseNanosecond => { |
| 268 |
f.write_str("failed to parse nanosecond") |
| 269 |
} |
| 270 |
FailedParseSecond => f.write_str("failed to parse second"), |
| 271 |
FailedParseTimeDuration => { |
| 272 |
f.write_str("failed to parse time duration") |
| 273 |
} |
| 274 |
FailedParseYear => f.write_str("failed to parse year"), |
| 275 |
FailedRule { name: ref rule } => { |
| 276 |
write!(f, "failed to parse rule `{rule}`") |
| 277 |
} |
| 278 |
FailedRuleLine => f.write_str("failed to parse `Rule` line"), |
| 279 |
FailedZoneFirst => { |
| 280 |
f.write_str("failed to parse first `Zone` line") |
| 281 |
} |
| 282 |
InvalidAbbreviation => f.write_str( |
| 283 |
"time zone abbreviation \ |
| 284 |
contains invalid character; only \"+\", \"-\" and \ |
| 285 |
ASCII alpha-numeric characters are allowed", |
| 286 |
), |
| 287 |
InvalidRuleYear { start, end } => write!( |
| 288 |
f, |
| 289 |
"found start year={start} \ |
| 290 |
to be greater than end year={end}" |
| 291 |
), |
| 292 |
InvalidUtf8 => f.write_str("invalid UTF-8"), |
| 293 |
Line { number } => write!(f, "line {number}"), |
| 294 |
LineMaxLength => write!( |
| 295 |
f, |
| 296 |
"found line with length that exceeds \ |
| 297 |
max length of {MAX_LINE_LEN}", |
| 298 |
), |
| 299 |
LineNul => f.write_str( |
| 300 |
"found line with NUL byte, which isn't allowed", |
| 301 |
), |
| 302 |
LineOverflow => f.write_str("line count overflowed"), |
| 303 |
UnrecognizedAtTimeSuffix => { |
| 304 |
f.write_str("unrecognized `AT` time suffix") |
| 305 |
} |
| 306 |
UnrecognizedDayOfMonthFormat => { |
| 307 |
f.write_str("unrecognized format for day-of-month") |
| 308 |
} |
| 309 |
UnrecognizedDayOfWeek => { |
| 310 |
f.write_str("unrecognized day of the week") |
| 311 |
} |
| 312 |
UnrecognizedMonthName => { |
| 313 |
f.write_str("unrecognized month name") |
| 314 |
} |
| 315 |
UnrecognizedSaveTimeSuffix => { |
| 316 |
f.write_str("unrecognized `SAVE` time suffix") |
| 317 |
} |
| 318 |
UnrecognizedTrailingTimeDuration => { |
| 319 |
f.write_str("found unrecognized suffix in time duration") |
| 320 |
} |
| 321 |
UnrecognizedZicLine => f.write_str("unrecognized zic line"), |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |