use crate::core::error::ChartError;
use crate::core::placement::natal::life_body::{LunarDay, LunarMonth};
use super::tyme::{LunarDateInput, TymeCalendar};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ResolvedLunarDate {
lunar_year: i32,
lunar_month: LunarMonth,
lunar_day: LunarDay,
is_leap_month: bool,
month_days: u8,
}
impl ResolvedLunarDate {
pub(crate) const fn lunar_year(&self) -> i32 {
self.lunar_year
}
pub(crate) const fn lunar_month(&self) -> LunarMonth {
self.lunar_month
}
pub(crate) const fn lunar_day(&self) -> LunarDay {
self.lunar_day
}
pub(crate) const fn is_leap_month(&self) -> bool {
self.is_leap_month
}
pub(crate) const fn month_days(&self) -> u8 {
self.month_days
}
}
pub(crate) fn resolve_lunar_date(
lunar_year: i32,
lunar_month: LunarMonth,
lunar_day: LunarDay,
is_leap_month: bool,
) -> Result<ResolvedLunarDate, ChartError> {
let month = lunar_month.value();
let day = lunar_day.value();
let conversion_failed = || ChartError::CalendarConversionFailed {
year: lunar_year,
month,
day,
};
let resolved = TymeCalendar.resolve_lunar(LunarDateInput {
year: lunar_year,
month,
day,
is_leap_month,
})?;
Ok(ResolvedLunarDate {
lunar_year: resolved.year,
lunar_month: LunarMonth::new(resolved.month).map_err(|_| conversion_failed())?,
lunar_day: LunarDay::new(resolved.day).map_err(|_| conversion_failed())?,
is_leap_month: resolved.is_leap_month,
month_days: resolved.month_day_count,
})
}
#[cfg(test)]
mod tests;