use crate::core::error::ChartError;
use crate::core::model::chart::{
TemporalPalaceLayout,
temporal_layout::{build_life_branch_palace_layout, yin_first_branch_index},
};
use crate::core::model::star::mutagen::Scope;
use lunar_lite::{EarthlyBranch, StemBranch};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct YearlyPeriod {
index: usize,
lunar_year: i32,
palace_branch: EarthlyBranch,
stem_branch: StemBranch,
palace_layout: TemporalPalaceLayout,
}
impl YearlyPeriod {
fn new(
index: usize,
lunar_year: i32,
palace_branch: EarthlyBranch,
stem_branch: StemBranch,
palace_layout: TemporalPalaceLayout,
) -> Self {
Self {
index,
lunar_year,
palace_branch,
stem_branch,
palace_layout,
}
}
pub const fn index(&self) -> usize {
self.index
}
pub const fn lunar_year(&self) -> i32 {
self.lunar_year
}
pub const fn palace_branch(&self) -> EarthlyBranch {
self.palace_branch
}
pub const fn stem_branch(&self) -> StemBranch {
self.stem_branch
}
pub const fn palace_layout(&self) -> &TemporalPalaceLayout {
&self.palace_layout
}
}
pub fn build_yearly_period(lunar_year: i32) -> Result<YearlyPeriod, ChartError> {
let stem_branch = StemBranch::from_lunar_year(lunar_year);
let palace_branch = stem_branch.branch();
let index = yin_first_branch_index(palace_branch);
let palace_layout = build_life_branch_palace_layout(Scope::Yearly, palace_branch)?;
Ok(YearlyPeriod::new(
index,
lunar_year,
palace_branch,
stem_branch,
palace_layout,
))
}