use crate::time::calendar::{BusinessDayConvention, Calendar};
use crate::time::date::{Date, DateError, Period};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum DateGenerationRule {
#[default]
Backward,
Forward,
Zero,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Schedule {
dates: Vec<Date>,
tenor: Period,
convention: BusinessDayConvention,
rule: DateGenerationRule,
}
impl Schedule {
#[allow(clippy::too_many_arguments)]
pub fn new<C: Calendar>(
effective_date: Date,
termination_date: Date,
tenor: Period,
calendar: &C,
convention: BusinessDayConvention,
termination_date_convention: BusinessDayConvention,
rule: DateGenerationRule,
end_of_month: bool,
) -> Result<Self, DateError> {
if effective_date >= termination_date {
return Err(DateError::InvalidDay {
day: effective_date.day(),
month: effective_date.month().as_number(),
year: effective_date.year(),
});
}
let mut dates = Vec::new();
match rule {
DateGenerationRule::Zero => {
dates.push(calendar.adjust(effective_date, convention));
dates.push(calendar.adjust(termination_date, termination_date_convention));
}
DateGenerationRule::Forward => {
let current = effective_date;
dates.push(calendar.adjust(current, convention));
let mut step = 1;
loop {
let next_raw = match tenor {
Period::Days(d) => {
effective_date.advance(Period::Days(d * step), end_of_month)?
}
Period::Weeks(w) => {
effective_date.advance(Period::Weeks(w * step), end_of_month)?
}
Period::Months(m) => {
effective_date.advance(Period::Months(m * step), end_of_month)?
}
Period::Years(y) => {
effective_date.advance(Period::Years(y * step), end_of_month)?
}
};
if next_raw >= termination_date {
break;
}
dates.push(calendar.adjust(next_raw, convention));
step += 1;
}
dates.push(calendar.adjust(termination_date, termination_date_convention));
}
DateGenerationRule::Backward => {
let mut unadjusted = Vec::new();
unadjusted.push(termination_date);
let mut step = 1;
loop {
let prev_raw = match tenor {
Period::Days(d) => {
termination_date.advance(Period::Days(-d * step), end_of_month)?
}
Period::Weeks(w) => {
termination_date.advance(Period::Weeks(-w * step), end_of_month)?
}
Period::Months(m) => {
termination_date.advance(Period::Months(-m * step), end_of_month)?
}
Period::Years(y) => {
termination_date.advance(Period::Years(-y * step), end_of_month)?
}
};
if prev_raw <= effective_date {
break;
}
unadjusted.push(prev_raw);
step += 1;
}
unadjusted.push(effective_date);
unadjusted.reverse();
for (idx, &d) in unadjusted.iter().enumerate() {
let conv = if idx == unadjusted.len() - 1 {
termination_date_convention
} else {
convention
};
dates.push(calendar.adjust(d, conv));
}
}
}
Ok(Self {
dates,
tenor,
convention,
rule,
})
}
pub fn dates(&self) -> &[Date] {
&self.dates
}
pub fn len(&self) -> usize {
self.dates.len()
}
pub fn is_empty(&self) -> bool {
self.dates.is_empty()
}
pub fn tenor(&self) -> Period {
self.tenor
}
pub fn convention(&self) -> BusinessDayConvention {
self.convention
}
pub fn rule(&self) -> DateGenerationRule {
self.rule
}
pub fn date(&self, index: usize) -> Option<Date> {
self.dates.get(index).copied()
}
}