use chrono::NaiveDate;
use super::kind::HolidayKind;
use super::tier::EvidenceTier;
use crate::calendar::schedules::holidays::fences::{day_key, holiday_date};
use crate::calendar::schedules::timeline::SourceRef;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Holiday {
kind: HolidayKind,
tier: EvidenceTier,
document: &'static str,
}
impl Holiday {
#[must_use]
pub const fn kind(self) -> HolidayKind {
self.kind
}
#[must_use]
pub const fn tier(self) -> EvidenceTier {
self.tier
}
#[must_use]
pub const fn document_id(self) -> &'static str {
self.document
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct HolidayWindow {
first: NaiveDate,
last: NaiveDate,
first_key: i64,
last_key: i64,
}
impl HolidayWindow {
pub(crate) const fn new(first: (i32, u32, u32), last: (i32, u32, u32)) -> Self {
Self {
first: holiday_date(first.0, first.1, first.2),
last: holiday_date(last.0, last.1, last.2),
first_key: day_key(first),
last_key: day_key(last),
}
}
pub(crate) const fn first(self) -> NaiveDate {
self.first
}
pub(crate) const fn last(self) -> NaiveDate {
self.last
}
pub(crate) const fn first_key(self) -> i64 {
self.first_key
}
pub(crate) const fn last_key(self) -> i64 {
self.last_key
}
pub(crate) fn contains(self, trade_date: NaiveDate) -> bool {
self.first <= trade_date && trade_date <= self.last
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HolidayCoverage {
first: NaiveDate,
last: NaiveDate,
windows: &'static [HolidayWindow],
}
impl HolidayCoverage {
#[must_use]
pub const fn first(self) -> NaiveDate {
self.first
}
#[must_use]
pub const fn last(self) -> NaiveDate {
self.last
}
#[must_use]
pub fn windows(self) -> Vec<(NaiveDate, NaiveDate)> {
self.windows
.iter()
.map(|window| (window.first(), window.last()))
.collect()
}
#[must_use]
pub fn contains(self, trade_date: NaiveDate) -> bool {
self.windows
.iter()
.any(|window| window.contains(trade_date))
}
}
#[derive(Clone, Copy)]
pub(crate) struct HolidayRow {
pub(crate) trade_date: NaiveDate,
pub(crate) kind: HolidayKind,
pub(crate) tier: EvidenceTier,
pub(crate) document: SourceRef,
}
#[derive(Clone, Copy)]
pub(crate) struct HolidayTable {
pub(crate) windows: &'static [HolidayWindow],
pub(crate) rows: &'static [HolidayRow],
pub(crate) has_blocks: bool,
}
impl HolidayTable {
pub(crate) const fn carries_replacement_blocks(&self) -> bool {
self.has_blocks
}
pub(crate) fn blocks_may_affect(&self, first: NaiveDate, last: NaiveDate) -> bool {
if !self.has_blocks {
return false;
}
let start = self.rows.partition_point(|row| row.trade_date < first);
self.rows[start..]
.iter()
.take_while(|row| row.trade_date <= last)
.any(|row| matches!(row.kind, HolidayKind::ReplacementBlocks(_)))
}
pub(crate) const fn coverage(&self) -> HolidayCoverage {
HolidayCoverage {
first: crate::calendar::schedules::holidays::fences::window_first(self.windows),
last: crate::calendar::schedules::holidays::fences::window_last(self.windows),
windows: self.windows,
}
}
pub(crate) fn holiday_on(&self, trade_date: NaiveDate) -> Option<Holiday> {
let index = self
.rows
.binary_search_by_key(&trade_date, |row| row.trade_date)
.ok()?;
self.rows.get(index).map(|row| Holiday {
kind: row.kind,
tier: row.tier,
document: row.document.as_str(),
})
}
pub(crate) fn may_affect(&self, first: NaiveDate, last: NaiveDate) -> bool {
let start = self.rows.partition_point(|row| row.trade_date < first);
self.rows
.get(start)
.is_some_and(|row| row.trade_date <= last)
}
}