Skip to main content

time/
error.rs

1//! The single error type returned by fallible operations.
2
3use core::fmt;
4
5/// The category of an [`Error`].
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum ErrorKind {
9    /// The input string could not be parsed.
10    Parse,
11    /// A field, or the result of an operation, is outside the supported range.
12    OutOfRange,
13    /// A date or time is not valid (for example `February 30`).
14    Invalid,
15    /// A civil time is ambiguous in the given time zone (a DST "fold").
16    Ambiguous,
17    /// The requested time zone name is not known.
18    UnknownTimeZone,
19    /// An RFC 9557 offset disagrees with the supplied time zone.
20    OffsetConflict,
21    /// The system time zone could not be determined.
22    UnknownSystemTimeZone,
23    /// A caller-provided buffer was too small for the formatted value.
24    BufferTooSmall,
25}
26
27/// An error returned by fallible operations in this crate.
28///
29/// Errors carry a stable [`ErrorKind`] plus a short human-readable message.
30/// They are deliberately small and allocation free.
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct Error {
33    kind: ErrorKind,
34    message: &'static str,
35}
36
37impl Error {
38    pub(crate) const fn new(kind: ErrorKind, message: &'static str) -> Error {
39        Error {
40            kind,
41            message,
42        }
43    }
44
45    /// Returns the category of this error.
46    #[must_use]
47    pub const fn kind(&self) -> ErrorKind {
48        self.kind
49    }
50
51    /// Returns a short human-readable description of this error.
52    #[must_use]
53    pub const fn message(&self) -> &'static str {
54        self.message
55    }
56}
57
58impl fmt::Display for Error {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.write_str(self.message)
61    }
62}
63
64impl core::error::Error for Error {}
65
66pub(crate) const fn parse(message: &'static str) -> Error {
67    Error::new(ErrorKind::Parse, message)
68}
69
70pub(crate) const fn out_of_range(message: &'static str) -> Error {
71    Error::new(ErrorKind::OutOfRange, message)
72}
73
74pub(crate) const fn invalid(message: &'static str) -> Error {
75    Error::new(ErrorKind::Invalid, message)
76}
77
78pub(crate) const fn unknown_time_zone(message: &'static str) -> Error {
79    Error::new(ErrorKind::UnknownTimeZone, message)
80}
81
82#[cfg(feature = "timezone-system")]
83pub(crate) const fn unknown_system_time_zone(message: &'static str) -> Error {
84    Error::new(ErrorKind::UnknownSystemTimeZone, message)
85}
86
87pub(crate) const fn offset_conflict(message: &'static str) -> Error {
88    Error::new(ErrorKind::OffsetConflict, message)
89}
90
91pub(crate) const fn ambiguous(message: &'static str) -> Error {
92    Error::new(ErrorKind::Ambiguous, message)
93}
94
95pub(crate) const fn buffer_too_small(message: &'static str) -> Error {
96    Error::new(ErrorKind::BufferTooSmall, message)
97}