1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
// Copyright (C) 2023 Shun Sakai
//

//! Error types for this crate.

use core::fmt;

/// The error type indicating that a [`OffsetDateTime`](time::OffsetDateTime)
/// was out of range.
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OffsetDateTimeRangeError;

impl fmt::Display for OffsetDateTimeRangeError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "date and time is out of range for `OffsetDateTime`")
    }
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl std::error::Error for OffsetDateTimeRangeError {}

/// The error type indicating that a [`FileTime`](crate::FileTime) was out of
/// range.
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FileTimeRangeError(FileTimeRangeErrorKind);

impl FileTimeRangeError {
    #[inline]
    pub(crate) const fn new(kind: FileTimeRangeErrorKind) -> Self {
        Self(kind)
    }

    const fn kind(self) -> FileTimeRangeErrorKind {
        self.0
    }
}

impl fmt::Display for FileTimeRangeError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind() {
            FileTimeRangeErrorKind::Negative => {
                write!(f, "date and time is before `1601-01-01 00:00:00 UTC`")
            }
            FileTimeRangeErrorKind::Overflow => {
                write!(
                    f,
                    "date and time is after `+60056-05-28 05:36:10.955161500 UTC`"
                )
            }
        }
    }
}

#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl std::error::Error for FileTimeRangeError {}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FileTimeRangeErrorKind {
    Negative,
    Overflow,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clone_offset_date_time_range_error() {
        assert_eq!(OffsetDateTimeRangeError.clone(), OffsetDateTimeRangeError);
    }

    #[test]
    fn copy_offset_date_time_range_error() {
        let a = OffsetDateTimeRangeError;
        let b = a;
        assert_eq!(a, b);
    }

    #[test]
    fn debug_offset_date_time_range_error() {
        assert_eq!(
            format!("{OffsetDateTimeRangeError:?}"),
            "OffsetDateTimeRangeError"
        );
    }

    #[test]
    fn offset_date_time_range_error_equality() {
        assert_eq!(OffsetDateTimeRangeError, OffsetDateTimeRangeError);
    }

    #[test]
    fn display_offset_date_time_range_error() {
        assert_eq!(
            format!("{OffsetDateTimeRangeError}"),
            "date and time is out of range for `OffsetDateTime`"
        );
    }

    #[cfg(feature = "std")]
    #[test]
    fn source_offset_date_time_range_error() {
        use std::error::Error;

        assert!(OffsetDateTimeRangeError.source().is_none());
    }

    #[test]
    fn clone_file_time_range_error() {
        assert_eq!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative).clone(),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
        );
        assert_eq!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow).clone(),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
        );
    }

    #[test]
    fn copy_file_time_range_error() {
        let a = FileTimeRangeError::new(FileTimeRangeErrorKind::Negative);
        let b = a;
        assert_eq!(a, b);

        let c = FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow);
        let d = c;
        assert_eq!(c, d);
    }

    #[test]
    fn debug_file_time_range_error() {
        assert_eq!(
            format!(
                "{:?}",
                FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
            ),
            "FileTimeRangeError(Negative)"
        );
        assert_eq!(
            format!(
                "{:?}",
                FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
            ),
            "FileTimeRangeError(Overflow)"
        );
    }

    #[test]
    fn file_time_range_error_equality() {
        assert_eq!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
        );
        assert_ne!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
        );
        assert_ne!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
        );
        assert_eq!(
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow),
            FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
        );
    }

    #[test]
    fn display_file_time_range_error() {
        assert_eq!(
            format!(
                "{}",
                FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
            ),
            "date and time is before `1601-01-01 00:00:00 UTC`"
        );
        assert_eq!(
            format!(
                "{}",
                FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
            ),
            "date and time is after `+60056-05-28 05:36:10.955161500 UTC`"
        );
    }

    #[cfg(feature = "std")]
    #[test]
    fn source_file_time_range_error() {
        use std::error::Error;

        assert!(FileTimeRangeError::new(FileTimeRangeErrorKind::Negative)
            .source()
            .is_none());
        assert!(FileTimeRangeError::new(FileTimeRangeErrorKind::Overflow)
            .source()
            .is_none());
    }
}