#![allow(clippy::module_name_repetitions)]
use core::fmt;
use core::num::TryFromIntError;
use core::time::TryFromFloatSecsError;
use std::error;
use std::str::Utf8Error;
use tz::error::{DateTimeError, ProjectDateTimeError, TzError};
#[derive(Debug)]
pub enum TimeError {
ProjectionError(ProjectDateTimeError),
ComponentOutOfRangeError(DateTimeError),
UnknownTzError(TzError),
TzStringError(TzStringError),
TzOutOfRangeError(TzOutOfRangeError),
IntOverflowError(IntOverflowError),
Unknown,
}
impl PartialEq for TimeError {
fn eq(&self, other: &Self) -> bool {
match self {
Self::ProjectionError(_) => matches!(other, TimeError::ProjectionError(_)),
Self::ComponentOutOfRangeError(_) => matches!(other, TimeError::ComponentOutOfRangeError(_)),
Self::UnknownTzError(_) => matches!(other, TimeError::UnknownTzError(_)),
Self::TzStringError(_) => matches!(other, TimeError::TzStringError(_)),
Self::TzOutOfRangeError(_) => matches!(other, TimeError::TzOutOfRangeError(_)),
Self::IntOverflowError(_) => matches!(other, TimeError::IntOverflowError(_)),
Self::Unknown => matches!(other, TimeError::Unknown),
}
}
}
impl error::Error for TimeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::ProjectionError(err) => Some(err),
Self::ComponentOutOfRangeError(err) => Some(err),
Self::UnknownTzError(err) => Some(err),
Self::TzStringError(err) => Some(err),
Self::TzOutOfRangeError(err) => Some(err),
Self::IntOverflowError(err) => Some(err),
Self::Unknown => None,
}
}
}
impl fmt::Display for TimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ProjectionError(error) => error.fmt(f),
Self::ComponentOutOfRangeError(error) => error.fmt(f),
Self::UnknownTzError(error) => error.fmt(f),
Self::TzStringError(error) => error.fmt(f),
Self::TzOutOfRangeError(error) => error.fmt(f),
Self::IntOverflowError(error) => error.fmt(f),
Self::Unknown => write!(f, "An unknown error occurred"),
}
}
}
impl From<ProjectDateTimeError> for TimeError {
fn from(err: ProjectDateTimeError) -> Self {
Self::ProjectionError(err)
}
}
impl From<DateTimeError> for TimeError {
fn from(err: DateTimeError) -> Self {
Self::ComponentOutOfRangeError(err)
}
}
impl From<TzError> for TimeError {
fn from(error: TzError) -> Self {
#[allow(clippy::match_same_arms)]
match error {
TzError::ProjectDateTimeError(error) => Self::from(error),
TzError::SystemTimeError(_) => Self::UnknownTzError(error),
TzError::TzFileError(_) => Self::UnknownTzError(error),
TzError::TzStringError(_) => Self::UnknownTzError(error),
TzError::OutOfRangeError(_) => Self::UnknownTzError(error),
TzError::LocalTimeTypeError(_) => Self::UnknownTzError(error),
TzError::TransitionRuleError(_) => Self::UnknownTzError(error),
TzError::TimeZoneError(_) => Self::UnknownTzError(error),
TzError::FindLocalTimeTypeError(_) => Self::UnknownTzError(error),
TzError::IoError(_) => Self::UnknownTzError(error),
TzError::Utf8Error(_) => Self::UnknownTzError(error),
TzError::TryFromSliceError(_) => Self::UnknownTzError(error),
_ => Self::Unknown,
}
}
}
impl From<TzStringError> for TimeError {
fn from(err: TzStringError) -> Self {
Self::TzStringError(err)
}
}
impl From<TzOutOfRangeError> for TimeError {
fn from(err: TzOutOfRangeError) -> Self {
Self::TzOutOfRangeError(err)
}
}
impl From<IntOverflowError> for TimeError {
fn from(err: IntOverflowError) -> Self {
Self::IntOverflowError(err)
}
}
impl From<TryFromFloatSecsError> for TimeError {
fn from(err: TryFromFloatSecsError) -> Self {
Self::TzOutOfRangeError(err.into())
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TzStringError {
_private: (),
}
impl fmt::Display for TzStringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(r#""+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset"#)
}
}
impl error::Error for TzStringError {}
impl TzStringError {
pub(crate) const fn new() -> Self {
Self { _private: () }
}
}
impl From<Utf8Error> for TzStringError {
fn from(_: Utf8Error) -> Self {
TzStringError::new()
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TzOutOfRangeError {
_private: (),
}
impl fmt::Display for TzOutOfRangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("utc_offset out of range")
}
}
impl error::Error for TzOutOfRangeError {}
impl TzOutOfRangeError {
pub(crate) const fn new() -> Self {
Self { _private: () }
}
}
impl From<TryFromFloatSecsError> for TzOutOfRangeError {
fn from(_err: TryFromFloatSecsError) -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct IntOverflowError {
_private: (),
}
impl fmt::Display for IntOverflowError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("out of int range")
}
}
impl error::Error for IntOverflowError {}
impl IntOverflowError {
pub(crate) const fn new() -> Self {
Self { _private: () }
}
}
impl From<TryFromIntError> for TimeError {
fn from(_: TryFromIntError) -> Self {
Self::IntOverflowError(IntOverflowError::new())
}
}