use core::{error::Error, fmt};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DateRangeError(DateRangeErrorKind);
impl DateRangeError {
pub(crate) const fn new(kind: DateRangeErrorKind) -> Self {
Self(kind)
}
#[must_use]
pub const fn kind(self) -> DateRangeErrorKind {
self.0
}
}
impl fmt::Display for DateRangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.kind().fmt(f)
}
}
impl Error for DateRangeError {}
impl From<DateRangeErrorKind> for DateRangeError {
fn from(kind: DateRangeErrorKind) -> Self {
Self::new(kind)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DateRangeErrorKind {
Negative,
Overflow,
}
impl fmt::Display for DateRangeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Negative => write!(f, "MS-DOS date is before `1980-01-01`"),
Self::Overflow => write!(f, "MS-DOS date is after `2107-12-31`"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clone_date_range_error() {
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Negative).clone(),
DateRangeError::new(DateRangeErrorKind::Negative)
);
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Overflow).clone(),
DateRangeError::new(DateRangeErrorKind::Overflow)
);
}
#[test]
fn copy_date_range_error() {
{
let a = DateRangeError::new(DateRangeErrorKind::Negative);
let b = a;
assert_eq!(a, b);
}
{
let a = DateRangeError::new(DateRangeErrorKind::Overflow);
let b = a;
assert_eq!(a, b);
}
}
#[test]
fn debug_date_range_error() {
assert_eq!(
format!("{:?}", DateRangeError::new(DateRangeErrorKind::Negative)),
"DateRangeError(Negative)"
);
assert_eq!(
format!("{:?}", DateRangeError::new(DateRangeErrorKind::Overflow)),
"DateRangeError(Overflow)"
);
}
#[test]
fn date_range_error_equality() {
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Negative),
DateRangeError::new(DateRangeErrorKind::Negative)
);
assert_ne!(
DateRangeError::new(DateRangeErrorKind::Negative),
DateRangeError::new(DateRangeErrorKind::Overflow)
);
assert_ne!(
DateRangeError::new(DateRangeErrorKind::Overflow),
DateRangeError::new(DateRangeErrorKind::Negative)
);
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Overflow),
DateRangeError::new(DateRangeErrorKind::Overflow)
);
}
#[test]
fn kind_date_range_error() {
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Negative).kind(),
DateRangeErrorKind::Negative
);
assert_eq!(
DateRangeError::new(DateRangeErrorKind::Overflow).kind(),
DateRangeErrorKind::Overflow
);
}
#[test]
const fn kind_date_range_error_is_const_fn() {
const _: DateRangeErrorKind = DateRangeError::new(DateRangeErrorKind::Negative).kind();
}
#[test]
fn display_date_range_error() {
assert_eq!(
format!("{}", DateRangeError::new(DateRangeErrorKind::Negative)),
"MS-DOS date is before `1980-01-01`"
);
assert_eq!(
format!("{}", DateRangeError::new(DateRangeErrorKind::Overflow)),
"MS-DOS date is after `2107-12-31`"
);
}
#[test]
fn source_date_range_error() {
assert!(
DateRangeError::new(DateRangeErrorKind::Negative)
.source()
.is_none()
);
assert!(
DateRangeError::new(DateRangeErrorKind::Overflow)
.source()
.is_none()
);
}
#[test]
fn from_date_range_error_kind_to_date_range_error() {
assert_eq!(
DateRangeError::from(DateRangeErrorKind::Negative),
DateRangeError::new(DateRangeErrorKind::Negative)
);
assert_eq!(
DateRangeError::from(DateRangeErrorKind::Overflow),
DateRangeError::new(DateRangeErrorKind::Overflow)
);
}
#[test]
fn clone_date_range_error_kind() {
assert_eq!(
DateRangeErrorKind::Negative.clone(),
DateRangeErrorKind::Negative
);
assert_eq!(
DateRangeErrorKind::Overflow.clone(),
DateRangeErrorKind::Overflow
);
}
#[test]
fn copy_date_range_error_kind() {
{
let a = DateRangeErrorKind::Negative;
let b = a;
assert_eq!(a, b);
}
{
let a = DateRangeErrorKind::Overflow;
let b = a;
assert_eq!(a, b);
}
}
#[test]
fn debug_date_range_error_kind() {
assert_eq!(format!("{:?}", DateRangeErrorKind::Negative), "Negative");
assert_eq!(format!("{:?}", DateRangeErrorKind::Overflow), "Overflow");
}
#[test]
fn date_range_error_kind_equality() {
assert_eq!(DateRangeErrorKind::Negative, DateRangeErrorKind::Negative);
assert_ne!(DateRangeErrorKind::Negative, DateRangeErrorKind::Overflow);
assert_ne!(DateRangeErrorKind::Overflow, DateRangeErrorKind::Negative);
assert_eq!(DateRangeErrorKind::Overflow, DateRangeErrorKind::Overflow);
}
#[test]
fn display_date_range_error_kind() {
assert_eq!(
format!("{}", DateRangeErrorKind::Negative),
"MS-DOS date is before `1980-01-01`"
);
assert_eq!(
format!("{}", DateRangeErrorKind::Overflow),
"MS-DOS date is after `2107-12-31`"
);
}
}