use chrono::NaiveDate;
use super::{EvidenceTier, HolidayKind, HolidayRow, HolidayWindow};
use crate::calendar::exceptions::{BlockViolation, ExceptionBlock, first_block_violation};
const SECONDS_PER_DAY: u32 = 86_400;
#[expect(
clippy::panic,
reason = "const-eval only: an invalid sourced date must fail the build"
)]
pub(crate) const fn holiday_date(year: i32, month: u32, day: u32) -> NaiveDate {
match NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => date,
None => panic!("invalid hard-coded holiday trade date"),
}
}
pub(crate) const fn day_key(date: (i32, u32, u32)) -> i64 {
date.0 as i64 * 10_000 + date.1 as i64 * 100 + date.2 as i64
}
pub(crate) const fn assert_table(
windows: &[HolidayWindow],
dates: &[(i32, u32, u32)],
rows: &[HolidayRow],
) {
assert!(
!windows.is_empty(),
"a holiday table must declare at least one coverage window"
);
assert!(
dates.len() == rows.len(),
"holiday fence received a different number of trade dates than rows"
);
assert_ordered_windows(windows);
let mut index = 0;
while index < dates.len() {
if index > 0 {
assert!(
day_key(dates[index]) > day_key(dates[index - 1]),
"holiday table is not strictly ascending by trade date; \
a row is out of order or shadowed by a duplicate"
);
}
assert!(
in_windows(windows, dates[index]),
"holiday row falls outside every coverage window its table declares"
);
assert!(
!rows[index].document.as_str().is_empty(),
"holiday row carries no document id"
);
assert!(
matches!(rows[index].tier, EvidenceTier::T1 | EvidenceTier::T2),
"holiday row is sourced below T2; LAW-PRIMARY-SOURCES admits only the \
operator's own statement or the operator's own machine channel"
);
assert_instants(rows[index].kind);
index += 1;
}
}
pub(crate) const fn window_first(windows: &[HolidayWindow]) -> NaiveDate {
assert!(
!windows.is_empty(),
"a holiday table must declare at least one coverage window"
);
let mut best = windows[0];
let mut index = 1;
while index < windows.len() {
let candidate = windows[index];
if candidate.first_key() < best.first_key() {
best = candidate;
}
index += 1;
}
best.first()
}
pub(crate) const fn window_last(windows: &[HolidayWindow]) -> NaiveDate {
assert!(
!windows.is_empty(),
"a holiday table must declare at least one coverage window"
);
let mut best = windows[0];
let mut index = 1;
while index < windows.len() {
let candidate = windows[index];
if candidate.last_key() > best.last_key() {
best = candidate;
}
index += 1;
}
best.last()
}
const fn assert_ordered_windows(windows: &[HolidayWindow]) {
let mut index = 0;
while index < windows.len() {
let window = windows[index];
assert!(
window.first_key() <= window.last_key(),
"a holiday coverage window is inverted: its last date precedes its first"
);
if index > 0 {
let previous = windows[index - 1];
assert!(
previous.last_key() < window.first_key(),
"holiday coverage windows overlap or are out of order; a table \
ships one window per audited era, ascending"
);
}
index += 1;
}
}
const fn in_windows(windows: &[HolidayWindow], date: (i32, u32, u32)) -> bool {
let key = day_key(date);
let mut index = 0;
while index < windows.len() {
let window = windows[index];
if window.first_key() <= key && key <= window.last_key() {
return true;
}
index += 1;
}
false
}
const fn assert_instants(kind: HolidayKind) {
let (open_ssm, close_ssm) = match kind {
HolidayKind::Closed | HolidayKind::Unsourced => return,
HolidayKind::EarlyClose { close_ssm } => (0, close_ssm),
HolidayKind::LateOpen { open_ssm } => (open_ssm, 0),
HolidayKind::LateOpenAndEarlyClose {
open_ssm,
close_ssm,
} => (open_ssm, close_ssm),
HolidayKind::ReplacementBlocks(set) => {
assert_blocks(set);
return;
}
};
assert!(
open_ssm < SECONDS_PER_DAY,
"holiday row's late open is outside 0..86_400"
);
assert!(
close_ssm <= SECONDS_PER_DAY,
"holiday row's early close is outside 0..=86_400"
);
}
pub(crate) const fn early_close(close_ssm: u32) -> HolidayKind {
HolidayKind::EarlyClose { close_ssm }
}
pub(crate) const fn late_open(open_ssm: u32) -> HolidayKind {
HolidayKind::LateOpen { open_ssm }
}
pub(crate) const fn late_open_and_early_close(open_ssm: u32, close_ssm: u32) -> HolidayKind {
HolidayKind::LateOpenAndEarlyClose {
open_ssm,
close_ssm,
}
}
pub(crate) const fn any_blocks(rows: &[HolidayRow]) -> bool {
let mut index = 0;
while index < rows.len() {
if matches!(rows[index].kind, HolidayKind::ReplacementBlocks(_)) {
return true;
}
index += 1;
}
false
}
#[expect(
clippy::panic,
reason = "const-eval only: a malformed built-in replacement row must fail the build"
)]
const fn assert_blocks(set: &[ExceptionBlock]) {
match first_block_violation(set) {
None => {}
Some(BlockViolation::Empty) => panic!(
"holiday replacement row carries an empty block set; a trade date with no \
blocks is HolidayKind::Closed"
),
Some(BlockViolation::OffsetOutOfRange { .. }) => {
panic!("holiday replacement block's opening day offset is outside the block day range")
}
Some(BlockViolation::OpenOutOfRange { .. }) => {
panic!("holiday replacement block's open is outside 0..86_400")
}
Some(BlockViolation::CloseOutOfRange { .. }) => {
panic!("holiday replacement block's close is outside 0..=86_400")
}
Some(BlockViolation::ClosesAfterTradeDate { .. }) => panic!(
"holiday replacement block opens on its own trade date and closes after it; \
a trade date is the local date of its final close"
),
Some(BlockViolation::NotOrdered { .. }) => {
panic!("holiday replacement blocks are not ordered by opening day and open time")
}
}
}