use icu_calendar::cal::Hebrew;
use icu_calendar::types::Month;
use super::HebrewCalendar;
pub const TISHREI: Month = Month::new(1);
pub const ḤESHVAN: Month = Month::new(2);
pub const KISLEV: Month = Month::new(3);
pub const TEVET: Month = Month::new(4);
pub const SHEVAT: Month = Month::new(5);
pub const ADARI: Month = Month::leap(5);
pub const ADAR: Month = Month::new(6);
pub const NISAN: Month = Month::new(7);
pub const IYYAR: Month = Month::new(8);
pub const SIVAN: Month = Month::new(9);
pub const TAMMUZ: Month = Month::new(10);
pub const AV: Month = Month::new(11);
pub const ELUL: Month = Month::new(12);
const COMMON_HEBREW_MONTHS: [Month; 12] = [
TISHREI, ḤESHVAN, KISLEV, TEVET, SHEVAT, ADAR, NISAN, IYYAR, SIVAN, TAMMUZ, AV, ELUL,
];
const LEAP_HEBREW_MONTHS: [Month; 13] = [
TISHREI, ḤESHVAN, KISLEV, TEVET, SHEVAT, ADARI, ADAR, NISAN, IYYAR, SIVAN, TAMMUZ, AV, ELUL,
];
pub trait HebrewMonthExt {
fn he(&self, year: i32) -> Option<&'static str>;
}
impl HebrewMonthExt for Month {
fn he(&self, year: i32) -> Option<&'static str> {
if !Self::hebrew_months_in_year(year).contains(self) {
return None;
}
let result = match *self {
NISAN => "ניסן",
IYYAR => "אייר",
SIVAN => "סיון",
TAMMUZ => "תמוז",
AV => "אב",
ELUL => "אלול",
TISHREI => "תשרי",
ḤESHVAN => "חשון",
KISLEV => "כסלו",
TEVET => "טבת",
SHEVAT => "שבט",
ADARI => "אדר א",
ADAR => {
if Hebrew::is_hebrew_leap_year(year) {
"אדר ב"
} else {
"אדר"
}
}
_ => return None,
};
Some(result)
}
}
pub(crate) trait HebrewMonthHelpers {
fn hebrew_months_in_year(year: i32) -> &'static [Month];
fn hebrew_month_of_year(self, year: i32) -> Option<u8>;
}
impl HebrewMonthHelpers for Month {
fn hebrew_months_in_year(year: i32) -> &'static [Month] {
if Hebrew::is_hebrew_leap_year(year) {
&LEAP_HEBREW_MONTHS
} else {
&COMMON_HEBREW_MONTHS
}
}
fn hebrew_month_of_year(self, year: i32) -> Option<u8> {
Self::hebrew_months_in_year(year)
.iter()
.position(|candidate| *candidate == self)
.map(|index| index as u8 + 1)
}
}