use chrono::NaiveDate;
use super::SUPPORT_FLOOR;
use crate::calendar::exchange_calendar::CalendarSource;
use super::baseline::NormalWeekBaseline;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CalendarQueryError {
BeforeSupportFloor {
source: CalendarSource,
date: NaiveDate,
},
OutsideCoveredRange {
source: CalendarSource,
date: NaiveDate,
},
UnresolvedGap {
source: CalendarSource,
date: NaiveDate,
},
SearchExhausted {
source: CalendarSource,
date: NaiveDate,
bound: NaiveDate,
},
}
impl CalendarQueryError {
#[must_use]
pub const fn source(self) -> CalendarSource {
match self {
Self::BeforeSupportFloor { source, .. }
| Self::OutsideCoveredRange { source, .. }
| Self::UnresolvedGap { source, .. }
| Self::SearchExhausted { source, .. } => source,
}
}
#[must_use]
pub const fn date(self) -> NaiveDate {
match self {
Self::BeforeSupportFloor { date, .. }
| Self::OutsideCoveredRange { date, .. }
| Self::UnresolvedGap { date, .. }
| Self::SearchExhausted { date, .. } => date,
}
}
#[must_use]
pub fn normal_week_baseline(self) -> Option<NormalWeekBaseline> {
super::baseline::for_error(self)
}
}
const fn identity_name(source: CalendarSource) -> &'static str {
match source {
CalendarSource::Exchange(exchange) => exchange.as_str(),
CalendarSource::MarketHoursKey(key) => key.as_str(),
}
}
impl core::fmt::Display for CalendarQueryError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let name = identity_name(self.source());
match *self {
Self::BeforeSupportFloor { date, .. } => write!(
f,
"{name}: {date} precedes the {SUPPORT_FLOOR} venue-local support floor"
),
Self::OutsideCoveredRange { date, .. } => {
write!(
f,
"{name}: {date} is outside this identity's covered ranges"
)
}
Self::UnresolvedGap { date, .. } => {
write!(
f,
"{name}: {date} is an unresolved gap in the covered range"
)?;
if let Some(baseline) = self.normal_week_baseline() {
write!(f, " ({baseline}; the holiday arrangement is unsourced)")?;
}
Ok(())
}
Self::SearchExhausted { date, bound, .. } => write!(
f,
"{name}: no answer for {date}; the bounded search ended at {bound}"
),
}
}
}
impl std::error::Error for CalendarQueryError {}