1use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum ErrorKind {
9 Parse,
11 OutOfRange,
13 Invalid,
15 Ambiguous,
17 UnknownTimeZone,
19 OffsetConflict,
21 UnknownSystemTimeZone,
23 BufferTooSmall,
25}
26
27#[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 #[must_use]
47 pub const fn kind(&self) -> ErrorKind {
48 self.kind
49 }
50
51 #[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}