| 1 |
use crate::{civil::Weekday, error, tz::Offset, util::escape}; |
| 2 |
|
| 3 |
#[derive(Clone, Debug)] |
| 4 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 5 |
pub(crate) enum Error { |
| 6 |
ColonCount { |
| 7 |
directive: u8, |
| 8 |
}, |
| 9 |
DirectiveFailure { |
| 10 |
directive: u8, |
| 11 |
colons: u8, |
| 12 |
}, |
| 13 |
DirectiveFailureDot { |
| 14 |
directive: u8, |
| 15 |
}, |
| 16 |
ExpectedDirectiveAfterColons, |
| 17 |
ExpectedDirectiveAfterFlag { |
| 18 |
flag: u8, |
| 19 |
}, |
| 20 |
ExpectedDirectiveAfterWidth, |
| 21 |
FailedStrftime, |
| 22 |
FailedStrptime, |
| 23 |
FailedWidth, |
| 24 |
InvalidDate, |
| 25 |
InvalidISOWeekDate, |
| 26 |
InvalidWeekdayMonday { |
| 27 |
got: Weekday, |
| 28 |
}, |
| 29 |
InvalidWeekdaySunday { |
| 30 |
got: Weekday, |
| 31 |
}, |
| 32 |
MismatchOffset { |
| 33 |
parsed: Offset, |
| 34 |
got: Offset, |
| 35 |
}, |
| 36 |
MismatchWeekday { |
| 37 |
parsed: Weekday, |
| 38 |
got: Weekday, |
| 39 |
}, |
| 40 |
MissingTimeHourForFractional, |
| 41 |
MissingTimeHourForMinute, |
| 42 |
MissingTimeHourForSecond, |
| 43 |
MissingTimeMinuteForFractional, |
| 44 |
MissingTimeMinuteForSecond, |
| 45 |
MissingTimeSecondForFractional, |
| 46 |
RangeTimestamp, |
| 47 |
RangeWidth, |
| 48 |
RequiredDateForDateTime, |
| 49 |
RequiredDateTimeForTimestamp, |
| 50 |
RequiredDateTimeForZoned, |
| 51 |
RequiredOffsetForTimestamp, |
| 52 |
RequiredSomeDayForDate, |
| 53 |
RequiredTimeForDateTime, |
| 54 |
RequiredYearForDate, |
| 55 |
UnconsumedStrptime { |
| 56 |
#[cfg(feature = "alloc")] |
| 57 |
remaining: alloc::boxed::Box<[u8]>, |
| 58 |
}, |
| 59 |
UnexpectedEndAfterDot, |
| 60 |
UnexpectedEndAfterPercent, |
| 61 |
UnknownDirectiveAfterDot { |
| 62 |
directive: u8, |
| 63 |
}, |
| 64 |
UnknownDirective { |
| 65 |
directive: u8, |
| 66 |
}, |
| 67 |
ZonedOffsetOrTz, |
| 68 |
} |
| 69 |
|
| 70 |
impl Error { |
| 71 |
pub(crate) fn unconsumed(_remaining: &[u8]) -> Error { |
| 72 |
Error::UnconsumedStrptime { |
| 73 |
#[cfg(feature = "alloc")] |
| 74 |
remaining: _remaining.into(), |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl error::IntoError for Error { |
| 80 |
fn into_error(self) -> error::Error { |
| 81 |
self.into() |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl From<Error> for error::Error { |
| 86 |
#[cold] |
| 87 |
#[inline(never)] |
| 88 |
fn from(err: Error) -> error::Error { |
| 89 |
error::ErrorKind::FmtStrtime(err).into() |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl core::fmt::Display for Error { |
| 94 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 95 |
use self::Error::*; |
| 96 |
|
| 97 |
match *self { |
| 98 |
ColonCount { directive } => write!( |
| 99 |
f, |
| 100 |
"invalid number of `:` in `%{directive}` directive", |
| 101 |
directive = escape::Byte(directive), |
| 102 |
), |
| 103 |
DirectiveFailure { directive, colons } => write!( |
| 104 |
f, |
| 105 |
"%{colons}{directive} failed", |
| 106 |
colons = escape::RepeatByte { byte: b':', count: colons }, |
| 107 |
directive = escape::Byte(directive), |
| 108 |
), |
| 109 |
DirectiveFailureDot { directive } => write!( |
| 110 |
f, |
| 111 |
"%.{directive} failed", |
| 112 |
directive = escape::Byte(directive), |
| 113 |
), |
| 114 |
ExpectedDirectiveAfterColons => f.write_str( |
| 115 |
"expected to find specifier directive after colons, \ |
| 116 |
but found end of format string", |
| 117 |
), |
| 118 |
ExpectedDirectiveAfterFlag { flag } => write!( |
| 119 |
f, |
| 120 |
"expected to find specifier directive after flag \ |
| 121 |
`{flag}`, but found end of format string", |
| 122 |
flag = escape::Byte(flag), |
| 123 |
), |
| 124 |
ExpectedDirectiveAfterWidth => f.write_str( |
| 125 |
"expected to find specifier directive after parsed width, \ |
| 126 |
but found end of format string", |
| 127 |
), |
| 128 |
FailedStrftime => f.write_str("strftime formatting failed"), |
| 129 |
FailedStrptime => f.write_str("strptime parsing failed"), |
| 130 |
FailedWidth => { |
| 131 |
f.write_str("failed to parse conversion specifier width") |
| 132 |
} |
| 133 |
InvalidDate => f.write_str("invalid date"), |
| 134 |
InvalidISOWeekDate => f.write_str("invalid ISO 8601 week date"), |
| 135 |
InvalidWeekdayMonday { got } => write!( |
| 136 |
f, |
| 137 |
"weekday `{got:?}` is not valid for \ |
| 138 |
Monday based week number", |
| 139 |
), |
| 140 |
InvalidWeekdaySunday { got } => write!( |
| 141 |
f, |
| 142 |
"weekday `{got:?}` is not valid for \ |
| 143 |
Sunday based week number", |
| 144 |
), |
| 145 |
MissingTimeHourForFractional => f.write_str( |
| 146 |
"parsing format did not include hour directive, \ |
| 147 |
but did include fractional second directive (cannot have \ |
| 148 |
smaller time units with bigger time units missing)", |
| 149 |
), |
| 150 |
MissingTimeHourForMinute => f.write_str( |
| 151 |
"parsing format did not include hour directive, \ |
| 152 |
but did include minute directive (cannot have \ |
| 153 |
smaller time units with bigger time units missing)", |
| 154 |
), |
| 155 |
MissingTimeHourForSecond => f.write_str( |
| 156 |
"parsing format did not include hour directive, \ |
| 157 |
but did include second directive (cannot have \ |
| 158 |
smaller time units with bigger time units missing)", |
| 159 |
), |
| 160 |
MissingTimeMinuteForFractional => f.write_str( |
| 161 |
"parsing format did not include minute directive, \ |
| 162 |
but did include fractional second directive (cannot have \ |
| 163 |
smaller time units with bigger time units missing)", |
| 164 |
), |
| 165 |
MissingTimeMinuteForSecond => f.write_str( |
| 166 |
"parsing format did not include minute directive, \ |
| 167 |
but did include second directive (cannot have \ |
| 168 |
smaller time units with bigger time units missing)", |
| 169 |
), |
| 170 |
MissingTimeSecondForFractional => f.write_str( |
| 171 |
"parsing format did not include second directive, \ |
| 172 |
but did include fractional second directive (cannot have \ |
| 173 |
smaller time units with bigger time units missing)", |
| 174 |
), |
| 175 |
MismatchOffset { parsed, got } => write!( |
| 176 |
f, |
| 177 |
"parsed time zone offset `{parsed}`, but \ |
| 178 |
offset from timestamp and time zone is `{got}`", |
| 179 |
), |
| 180 |
MismatchWeekday { parsed, got } => write!( |
| 181 |
f, |
| 182 |
"parsed weekday `{parsed:?}` does not match \ |
| 183 |
weekday `{got:?}` from parsed date", |
| 184 |
), |
| 185 |
RangeTimestamp => f.write_str( |
| 186 |
"parsed datetime and offset, \ |
| 187 |
but combining them into a zoned datetime \ |
| 188 |
is outside Jiff's supported timestamp range", |
| 189 |
), |
| 190 |
RangeWidth => write!( |
| 191 |
f, |
| 192 |
"parsed width is too big, max is {max}", |
| 193 |
max = u8::MAX |
| 194 |
), |
| 195 |
RequiredDateForDateTime => { |
| 196 |
f.write_str("date required to parse datetime") |
| 197 |
} |
| 198 |
RequiredDateTimeForTimestamp => { |
| 199 |
f.write_str("datetime required to parse timestamp") |
| 200 |
} |
| 201 |
RequiredDateTimeForZoned => { |
| 202 |
f.write_str("datetime required to parse zoned datetime") |
| 203 |
} |
| 204 |
RequiredOffsetForTimestamp => { |
| 205 |
f.write_str("offset required to parse timestamp") |
| 206 |
} |
| 207 |
RequiredSomeDayForDate => f.write_str( |
| 208 |
"a month/day, day-of-year or week date must be \ |
| 209 |
present to create a date, but none were found", |
| 210 |
), |
| 211 |
RequiredTimeForDateTime => { |
| 212 |
f.write_str("time required to parse datetime") |
| 213 |
} |
| 214 |
RequiredYearForDate => f.write_str("year required to parse date"), |
| 215 |
#[cfg(feature = "alloc")] |
| 216 |
UnconsumedStrptime { ref remaining } => write!( |
| 217 |
f, |
| 218 |
"strptime expects to consume the entire input, but \ |
| 219 |
`{remaining}` remains unparsed", |
| 220 |
remaining = escape::Bytes(remaining), |
| 221 |
), |
| 222 |
#[cfg(not(feature = "alloc"))] |
| 223 |
UnconsumedStrptime {} => f.write_str( |
| 224 |
"strptime expects to consume the entire input, but \ |
| 225 |
there is unparsed input remaining", |
| 226 |
), |
| 227 |
UnexpectedEndAfterDot => f.write_str( |
| 228 |
"invalid format string, expected directive after `%.`", |
| 229 |
), |
| 230 |
UnexpectedEndAfterPercent => f.write_str( |
| 231 |
"invalid format string, expected byte after `%`, \ |
| 232 |
but found end of format string", |
| 233 |
), |
| 234 |
UnknownDirective { directive } => write!( |
| 235 |
f, |
| 236 |
"found unrecognized specifier directive `{directive}`", |
| 237 |
directive = escape::Byte(directive), |
| 238 |
), |
| 239 |
UnknownDirectiveAfterDot { directive } => write!( |
| 240 |
f, |
| 241 |
"found unrecognized specifier directive `{directive}` \ |
| 242 |
following `%.`", |
| 243 |
directive = escape::Byte(directive), |
| 244 |
), |
| 245 |
ZonedOffsetOrTz => f.write_str( |
| 246 |
"either offset (from `%z`) or IANA time zone identifier \ |
| 247 |
(from `%Q`) is required for parsing zoned datetime", |
| 248 |
), |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
#[derive(Clone, Debug)] |
| 254 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 255 |
pub(crate) enum ParseError { |
| 256 |
ExpectedAmPm, |
| 257 |
ExpectedAmPmTooShort, |
| 258 |
ExpectedIanaTz, |
| 259 |
ExpectedIanaTzEndOfInput, |
| 260 |
ExpectedMonthAbbreviation, |
| 261 |
ExpectedMonthAbbreviationTooShort, |
| 262 |
ExpectedWeekdayAbbreviation, |
| 263 |
ExpectedWeekdayAbbreviationTooShort, |
| 264 |
ExpectedChoice { |
| 265 |
available: &'static [&'static [u8]], |
| 266 |
}, |
| 267 |
ExpectedFractionalDigit, |
| 268 |
ExpectedMatchLiteralByte { |
| 269 |
expected: u8, |
| 270 |
got: u8, |
| 271 |
}, |
| 272 |
ExpectedMatchLiteralEndOfInput { |
| 273 |
expected: u8, |
| 274 |
}, |
| 275 |
ExpectedNonEmpty { |
| 276 |
directive: u8, |
| 277 |
}, |
| 278 |
#[cfg(not(feature = "alloc"))] |
| 279 |
NotAllowedAlloc { |
| 280 |
directive: u8, |
| 281 |
colons: u8, |
| 282 |
}, |
| 283 |
NotAllowedLocaleClockTime, |
| 284 |
NotAllowedLocaleDate, |
| 285 |
NotAllowedLocaleDateAndTime, |
| 286 |
NotAllowedLocaleTwelveHourClockTime, |
| 287 |
NotAllowedTimeZoneAbbreviation, |
| 288 |
ParseDay, |
| 289 |
ParseDayOfYear, |
| 290 |
ParseCentury, |
| 291 |
ParseFractionalSeconds, |
| 292 |
ParseHour, |
| 293 |
ParseIsoWeekNumber, |
| 294 |
ParseIsoWeekYear, |
| 295 |
ParseIsoWeekYearTwoDigit, |
| 296 |
ParseMinute, |
| 297 |
ParseMondayWeekNumber, |
| 298 |
ParseMonth, |
| 299 |
ParseSecond, |
| 300 |
ParseSundayWeekNumber, |
| 301 |
ParseTimestamp, |
| 302 |
ParseWeekdayNumber, |
| 303 |
ParseYear, |
| 304 |
ParseYearTwoDigit, |
| 305 |
UnknownMonthName, |
| 306 |
UnknownWeekdayAbbreviation, |
| 307 |
} |
| 308 |
|
| 309 |
impl error::IntoError for ParseError { |
| 310 |
fn into_error(self) -> error::Error { |
| 311 |
self.into() |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
impl From<ParseError> for error::Error { |
| 316 |
#[cold] |
| 317 |
#[inline(never)] |
| 318 |
fn from(err: ParseError) -> error::Error { |
| 319 |
error::ErrorKind::FmtStrtimeParse(err).into() |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
impl core::fmt::Display for ParseError { |
| 324 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 325 |
use self::ParseError::*; |
| 326 |
|
| 327 |
match *self { |
| 328 |
ExpectedAmPm => f.write_str("expected to find `AM` or `PM`"), |
| 329 |
ExpectedAmPmTooShort => f.write_str( |
| 330 |
"expected to find `AM` or `PM`, \ |
| 331 |
but the remaining input is too short \ |
| 332 |
to contain one", |
| 333 |
), |
| 334 |
ExpectedIanaTz => f.write_str( |
| 335 |
"expected to find the start of an IANA time zone \ |
| 336 |
identifier name or component", |
| 337 |
), |
| 338 |
ExpectedIanaTzEndOfInput => f.write_str( |
| 339 |
"expected to find the start of an IANA time zone \ |
| 340 |
identifier name or component, \ |
| 341 |
but found end of input instead", |
| 342 |
), |
| 343 |
ExpectedMonthAbbreviation => { |
| 344 |
f.write_str("expected to find month name abbreviation") |
| 345 |
} |
| 346 |
ExpectedMonthAbbreviationTooShort => f.write_str( |
| 347 |
"expected to find month name abbreviation, \ |
| 348 |
but the remaining input is too short \ |
| 349 |
to contain one", |
| 350 |
), |
| 351 |
ExpectedWeekdayAbbreviation => { |
| 352 |
f.write_str("expected to find weekday abbreviation") |
| 353 |
} |
| 354 |
ExpectedWeekdayAbbreviationTooShort => f.write_str( |
| 355 |
"expected to find weekday abbreviation, \ |
| 356 |
but the remaining input is too short \ |
| 357 |
to contain one", |
| 358 |
), |
| 359 |
ExpectedChoice { available } => { |
| 360 |
f.write_str( |
| 361 |
"failed to find expected value, available choices are: ", |
| 362 |
)?; |
| 363 |
for (i, choice) in available.iter().enumerate() { |
| 364 |
if i > 0 { |
| 365 |
f.write_str(", ")?; |
| 366 |
} |
| 367 |
write!(f, "{}", escape::Bytes(choice))?; |
| 368 |
} |
| 369 |
Ok(()) |
| 370 |
} |
| 371 |
ExpectedFractionalDigit => f.write_str( |
| 372 |
"expected at least one fractional decimal digit, \ |
| 373 |
but did not find any", |
| 374 |
), |
| 375 |
ExpectedMatchLiteralByte { expected, got } => write!( |
| 376 |
f, |
| 377 |
"expected to match literal byte `{expected}` from \ |
| 378 |
format string, but found byte `{got}` in input", |
| 379 |
expected = escape::Byte(expected), |
| 380 |
got = escape::Byte(got), |
| 381 |
), |
| 382 |
ExpectedMatchLiteralEndOfInput { expected } => write!( |
| 383 |
f, |
| 384 |
"expected to match literal byte `{expected}` from \ |
| 385 |
format string, but found end of input", |
| 386 |
expected = escape::Byte(expected), |
| 387 |
), |
| 388 |
ExpectedNonEmpty { directive } => write!( |
| 389 |
f, |
| 390 |
"expected non-empty input for directive `%{directive}`, \ |
| 391 |
but found end of input", |
| 392 |
directive = escape::Byte(directive), |
| 393 |
), |
| 394 |
#[cfg(not(feature = "alloc"))] |
| 395 |
NotAllowedAlloc { directive, colons } => write!( |
| 396 |
f, |
| 397 |
"cannot parse `%{colons}{directive}` \ |
| 398 |
without Jiff's `alloc` feature enabled", |
| 399 |
colons = escape::RepeatByte { byte: b':', count: colons }, |
| 400 |
directive = escape::Byte(directive), |
| 401 |
), |
| 402 |
NotAllowedLocaleClockTime => { |
| 403 |
f.write_str("parsing locale clock time is not allowed") |
| 404 |
} |
| 405 |
NotAllowedLocaleDate => { |
| 406 |
f.write_str("parsing locale date is not allowed") |
| 407 |
} |
| 408 |
NotAllowedLocaleDateAndTime => { |
| 409 |
f.write_str("parsing locale date and time is not allowed") |
| 410 |
} |
| 411 |
NotAllowedLocaleTwelveHourClockTime => { |
| 412 |
f.write_str("parsing locale 12-hour clock time is not allowed") |
| 413 |
} |
| 414 |
NotAllowedTimeZoneAbbreviation => { |
| 415 |
f.write_str("parsing time zone abbreviation is not allowed") |
| 416 |
} |
| 417 |
ParseCentury => { |
| 418 |
f.write_str("failed to parse year number for century") |
| 419 |
} |
| 420 |
ParseDay => f.write_str("failed to parse day number"), |
| 421 |
ParseDayOfYear => { |
| 422 |
f.write_str("failed to parse day of year number") |
| 423 |
} |
| 424 |
ParseFractionalSeconds => f.write_str( |
| 425 |
"failed to parse fractional second component \ |
| 426 |
(up to 9 digits, nanosecond precision)", |
| 427 |
), |
| 428 |
ParseHour => f.write_str("failed to parse hour number"), |
| 429 |
ParseMinute => f.write_str("failed to parse minute number"), |
| 430 |
ParseWeekdayNumber => { |
| 431 |
f.write_str("failed to parse weekday number") |
| 432 |
} |
| 433 |
ParseIsoWeekNumber => { |
| 434 |
f.write_str("failed to parse ISO 8601 week number") |
| 435 |
} |
| 436 |
ParseIsoWeekYear => { |
| 437 |
f.write_str("failed to parse ISO 8601 week year") |
| 438 |
} |
| 439 |
ParseIsoWeekYearTwoDigit => { |
| 440 |
f.write_str("failed to parse 2-digit ISO 8601 week year") |
| 441 |
} |
| 442 |
ParseMondayWeekNumber => { |
| 443 |
f.write_str("failed to parse Monday-based week number") |
| 444 |
} |
| 445 |
ParseMonth => f.write_str("failed to parse month number"), |
| 446 |
ParseSecond => f.write_str("failed to parse second number"), |
| 447 |
ParseSundayWeekNumber => { |
| 448 |
f.write_str("failed to parse Sunday-based week number") |
| 449 |
} |
| 450 |
ParseTimestamp => { |
| 451 |
f.write_str("failed to parse Unix timestamp (in seconds)") |
| 452 |
} |
| 453 |
ParseYear => f.write_str("failed to parse year"), |
| 454 |
ParseYearTwoDigit => f.write_str("failed to parse 2-digit year"), |
| 455 |
UnknownMonthName => f.write_str("unrecognized month name"), |
| 456 |
UnknownWeekdayAbbreviation => { |
| 457 |
f.write_str("unrecognized weekday abbreviation") |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
#[derive(Clone, Debug)] |
| 464 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 465 |
pub(crate) enum FormatError { |
| 466 |
RequiresDate, |
| 467 |
RequiresInstant, |
| 468 |
RequiresOffset, |
| 469 |
RequiresTime, |
| 470 |
RequiresTimeZone, |
| 471 |
RequiresTimeZoneOrOffset, |
| 472 |
InvalidUtf8, |
| 473 |
ZeroPrecisionFloat, |
| 474 |
ZeroPrecisionNano, |
| 475 |
} |
| 476 |
|
| 477 |
impl error::IntoError for FormatError { |
| 478 |
fn into_error(self) -> error::Error { |
| 479 |
self.into() |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
impl From<FormatError> for error::Error { |
| 484 |
#[cold] |
| 485 |
#[inline(never)] |
| 486 |
fn from(err: FormatError) -> error::Error { |
| 487 |
error::ErrorKind::FmtStrtimeFormat(err).into() |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
impl core::fmt::Display for FormatError { |
| 492 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 493 |
use self::FormatError::*; |
| 494 |
|
| 495 |
match *self { |
| 496 |
RequiresDate => f.write_str("requires date to format"), |
| 497 |
RequiresInstant => f.write_str( |
| 498 |
"requires instant (a timestamp or a date, time and offset)", |
| 499 |
), |
| 500 |
RequiresTime => f.write_str("requires time to format"), |
| 501 |
RequiresOffset => f.write_str("requires time zone offset"), |
| 502 |
RequiresTimeZone => { |
| 503 |
f.write_str("requires IANA time zone identifier") |
| 504 |
} |
| 505 |
RequiresTimeZoneOrOffset => f.write_str( |
| 506 |
"requires IANA time zone identifier or \ |
| 507 |
time zone offset, but neither were present", |
| 508 |
), |
| 509 |
InvalidUtf8 => { |
| 510 |
f.write_str("invalid format string, it must be valid UTF-8") |
| 511 |
} |
| 512 |
ZeroPrecisionFloat => { |
| 513 |
f.write_str("zero precision with %f is not allowed") |
| 514 |
} |
| 515 |
ZeroPrecisionNano => { |
| 516 |
f.write_str("zero precision with %N is not allowed") |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
} |