use chrono::NaiveDate;
use super::timeline::{effective_date, horizon};
use crate::calendar::coverage::{CoverageGapReason, PhaseGap};
use crate::calendar::{CalendarSource, Exchange, MarketHoursKey};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct DeclaredSourcing {
pub(crate) carried_below: Option<NaiveDate>,
pub(crate) observes_no_holidays: bool,
pub(crate) phase_gaps: &'static [PhaseGap],
}
impl DeclaredSourcing {
const fn carried_below(horizon: Option<NaiveDate>) -> Self {
Self {
carried_below: horizon,
observes_no_holidays: false,
phase_gaps: &[],
}
}
const fn nothing_carried() -> Self {
Self {
carried_below: None,
observes_no_holidays: false,
phase_gaps: &[],
}
}
const fn no_holidays() -> Self {
Self {
carried_below: None,
observes_no_holidays: true,
phase_gaps: &[],
}
}
const fn with_phase_gaps(mut self, gaps: &'static [PhaseGap]) -> Self {
self.phase_gaps = gaps;
self
}
}
static HKEX_AHT_SUSPENSION_GAPS: [PhaseGap; 1] =
[
PhaseGap::new(CoverageGapReason::UnpublishedClosureDates, "#332")
.since(effective_date(2013, 4, 8))
.until(effective_date(2017, 1, 1)),
];
pub(crate) const fn declared(source: CalendarSource) -> DeclaredSourcing {
match source {
CalendarSource::Exchange(exchange) => for_exchange(exchange),
CalendarSource::MarketHoursKey(key) => for_market_hours_key(key),
}
}
#[expect(
clippy::match_same_arms,
reason = "the per-variant arms are the fence: one arm per identity is what a \
reviewer edits when that identity's ledger row moves, and collapsing \
them would let the next venue inherit a boundary nobody decided"
)]
const fn for_exchange(exchange: Exchange) -> DeclaredSourcing {
match exchange {
Exchange::Unknown => DeclaredSourcing::no_holidays(),
Exchange::Nasdaq => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqBx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqPsx => DeclaredSourcing::nothing_carried(),
Exchange::CboeBzx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::CboeByx => DeclaredSourcing::nothing_carried(),
Exchange::CboeEdga => DeclaredSourcing::nothing_carried(),
Exchange::CboeEdgx => DeclaredSourcing::nothing_carried(),
Exchange::Nyse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NyseArca => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NyseAmerican => DeclaredSourcing::carried_below(horizon!(2017, 7, 24)),
Exchange::NyseNational => DeclaredSourcing::carried_below(horizon!(2010, 8, 2)),
Exchange::NyseTexas => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::MemxEq => DeclaredSourcing::nothing_carried(),
Exchange::MiaxPearlEq => DeclaredSourcing::nothing_carried(),
Exchange::Iex => DeclaredSourcing::nothing_carried(),
Exchange::Ltse => DeclaredSourcing::nothing_carried(),
Exchange::TwentyFourX => DeclaredSourcing::nothing_carried(),
Exchange::Txse => DeclaredSourcing::nothing_carried(),
Exchange::BlueOceanAts => DeclaredSourcing::nothing_carried(),
Exchange::FinraTrfCarteret => DeclaredSourcing::carried_below(horizon!(2026, 3, 30)),
Exchange::FinraTrfChicago => DeclaredSourcing::nothing_carried(),
Exchange::FinraTrfNyse => DeclaredSourcing::carried_below(horizon!(2026, 3, 30)),
Exchange::CboeOptionsC1 => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::CboeC2Options => DeclaredSourcing::nothing_carried(),
Exchange::CboeBzxOptions => DeclaredSourcing::nothing_carried(),
Exchange::CboeEdgxOptions => DeclaredSourcing::nothing_carried(),
Exchange::NyseArcaOptions => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NyseAmericanOptions => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqPhlx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqIse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqNom => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqMrx => DeclaredSourcing::nothing_carried(),
Exchange::NasdaqGemx => DeclaredSourcing::nothing_carried(),
Exchange::NasdaqBxOptions => DeclaredSourcing::nothing_carried(),
Exchange::MiaxOptions => DeclaredSourcing::nothing_carried(),
Exchange::MiaxEmeraldOptions => DeclaredSourcing::nothing_carried(),
Exchange::MiaxPearlOptions => DeclaredSourcing::nothing_carried(),
Exchange::MiaxSapphireOptions => DeclaredSourcing::nothing_carried(),
Exchange::BoxOptions => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::MemxOptions => DeclaredSourcing::nothing_carried(),
Exchange::Cme => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Cbot => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Comex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Nymex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Cfe => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::CoinbaseDerivatives => DeclaredSourcing::nothing_carried(),
Exchange::Smfe => DeclaredSourcing::nothing_carried(),
Exchange::Eurex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Eex => DeclaredSourcing::nothing_carried(),
Exchange::Iceus => DeclaredSourcing::nothing_carried(),
Exchange::Iceeu => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::IceEuropeCommodities => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::IceEuropeFinancials => DeclaredSourcing::nothing_carried(),
Exchange::IceEndex => DeclaredSourcing::nothing_carried(),
Exchange::IceAbuDhabi => DeclaredSourcing::nothing_carried(),
Exchange::IceCanada => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Sgx => DeclaredSourcing::nothing_carried(),
Exchange::Asx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::TmxAustralia => DeclaredSourcing::nothing_carried(),
Exchange::Nzx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Tse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NseIndia => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::BseIndia => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Hkex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::SgxSecurities => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::BursaMalaysia => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::SetThailand => DeclaredSourcing::carried_below(horizon!(2024, 3, 25)),
Exchange::Idx => DeclaredSourcing::carried_below(horizon!(2010, 8, 31)),
Exchange::Pse => DeclaredSourcing::carried_below(horizon!(2011, 10, 1)),
Exchange::Hose => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Sse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Szse => DeclaredSourcing::carried_below(horizon!(2016, 5, 9)),
Exchange::Krx => DeclaredSourcing::carried_below(horizon!(2016, 8, 1)),
Exchange::Twse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Lse => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Xetra => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Six => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::EuronextParis => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::EuronextAmsterdam => DeclaredSourcing::carried_below(horizon!(2010, 12, 24)),
Exchange::EuronextBrussels => DeclaredSourcing::carried_below(horizon!(2010, 12, 24)),
Exchange::EuronextLisbon => DeclaredSourcing::carried_below(horizon!(2010, 12, 24)),
Exchange::EuronextDublin => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::EuronextMilan => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Bme => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqStockholm => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqHelsinki => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::NasdaqCopenhagen => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Vienna => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::BorsaIstanbul => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Tsx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Jse => DeclaredSourcing::carried_below(horizon!(2012, 5, 25)),
Exchange::Tadawul => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::B3 => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
Exchange::Bmv => DeclaredSourcing::carried_below(horizon!(2010, 2, 18)),
Exchange::BinanceFutures => DeclaredSourcing::no_holidays(),
Exchange::Oanda => DeclaredSourcing::carried_below(horizon!(2021, 12, 8)),
}
}
#[expect(
clippy::match_same_arms,
reason = "the per-variant arms are the fence: one arm per family is what a \
reviewer edits when that family's ledger row moves, and collapsing \
them would let the next key inherit a boundary nobody decided"
)]
const fn for_market_hours_key(key: MarketHoursKey) -> DeclaredSourcing {
match key {
MarketHoursKey::GlobexEquityIndex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexEnergy => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexSilver100Oz => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexGrains => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexMiniGrains => DeclaredSourcing::carried_below(horizon!(2010, 4, 5)),
MarketHoursKey::GlobexFx => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexInterestRates => {
DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
}
MarketHoursKey::GlobexLivestock => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::GlobexCryptocurrency => DeclaredSourcing::nothing_carried(),
MarketHoursKey::CfeVix => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::Eurex => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::IceUs => DeclaredSourcing::nothing_carried(),
MarketHoursKey::IceUsSugar => DeclaredSourcing::carried_below(horizon!(2011, 8, 1)),
MarketHoursKey::IceUsCoffee => DeclaredSourcing::carried_below(horizon!(2011, 8, 1)),
MarketHoursKey::IceUsCocoa => DeclaredSourcing::carried_below(horizon!(2011, 8, 1)),
MarketHoursKey::IceUsCotton => DeclaredSourcing::carried_below(horizon!(2011, 8, 1)),
MarketHoursKey::IceUsOrangeJuice => DeclaredSourcing::carried_below(horizon!(2011, 8, 1)),
MarketHoursKey::IceUsDollarIndex => DeclaredSourcing::carried_below(horizon!(2011, 2, 7)),
MarketHoursKey::GlobexNikkei225Dollar => {
DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
}
MarketHoursKey::EurexFixedIncome => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::EurexSmi => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::EuronextIndexFutures => {
DeclaredSourcing::carried_below(horizon!(2026, 10, 11))
}
MarketHoursKey::SgxEquityIndexJapan => {
DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
}
MarketHoursKey::SgxEquityIndexChina => {
DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
}
MarketHoursKey::SgxEquityIndexSingapore => {
DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
}
MarketHoursKey::SgxEquityIndexTaiwan => DeclaredSourcing::nothing_carried(),
MarketHoursKey::SgxEquityIndexNtrUsd => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexRoughRice => DeclaredSourcing::carried_below(horizon!(2010, 3, 15)),
MarketHoursKey::GlobexWeather => DeclaredSourcing::carried_below(horizon!(2010, 2, 6)),
MarketHoursKey::GlobexSpotQuoted => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexEventContracts => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexEventContractsBtc => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexGoldTas => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexSilverTas => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexCopperTas => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexPlatinumTas => DeclaredSourcing::nothing_carried(),
MarketHoursKey::GlobexPalladiumTas => DeclaredSourcing::nothing_carried(),
MarketHoursKey::OandaFx => DeclaredSourcing::carried_below(horizon!(2021, 12, 8)),
MarketHoursKey::OandaTryFx => DeclaredSourcing::carried_below(horizon!(2026, 10, 10)),
MarketHoursKey::OandaMetals => DeclaredSourcing::carried_below(horizon!(2021, 12, 8)),
MarketHoursKey::OandaUsIndexCfd => DeclaredSourcing::carried_below(horizon!(2021, 8, 4)),
MarketHoursKey::OandaUsBondCfd => DeclaredSourcing::carried_below(horizon!(2021, 3, 3)),
MarketHoursKey::OandaUsCommodityCfd => {
DeclaredSourcing::carried_below(horizon!(2021, 3, 3))
}
MarketHoursKey::OandaBrent => DeclaredSourcing::carried_below(horizon!(2024, 2, 21)),
MarketHoursKey::OandaChina50Cfd | MarketHoursKey::OandaSingapore30Cfd => {
DeclaredSourcing::carried_below(horizon!(2021, 3, 3))
}
MarketHoursKey::BmeIbex => DeclaredSourcing::carried_below(horizon!(2010, 6, 18)),
MarketHoursKey::IceEuropeGilts => DeclaredSourcing::carried_below(horizon!(2026, 3, 11)),
MarketHoursKey::AsxSpi200 => DeclaredSourcing::carried_below(horizon!(2010, 1, 1)),
MarketHoursKey::HkexIndexFutures => DeclaredSourcing::carried_below(horizon!(2010, 1, 1))
.with_phase_gaps(&HKEX_AHT_SUSPENSION_GAPS),
MarketHoursKey::Sgx => DeclaredSourcing::nothing_carried(),
MarketHoursKey::AlwaysOpen => DeclaredSourcing::no_holidays(),
}
}