Skip to content

Commit

Permalink
Refactor everyone else
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 28, 2023
1 parent 4933817 commit be2520e
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 138 deletions.
27 changes: 11 additions & 16 deletions components/calendar/src/calendar_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ pub trait CalendarArithmetic: Calendar {
///
/// The name has `provided` in it to avoid clashes with Calendar
fn days_in_provided_year(year: i32, data: &Self::PrecomputedData) -> u16 {
let months_in_year = Self::months_for_every_year(year);
let months_in_year = Self::months_for_every_year(year, data);
let mut days: u16 = 0;
for month in 1..=months_in_year {
days += Self::month_days(year, month) as u16;
days += Self::month_days(year, month, data) as u16;
}
days
}
Expand Down Expand Up @@ -174,7 +174,11 @@ impl<C: CalendarArithmetic> ArithmeticDate<C> {
}

#[inline]
pub fn date_from_year_day_with_data(year: i32, year_day: u32, data: &C::PrecomputedData) -> ArithmeticDate<C> {
pub fn date_from_year_day_with_data(
year: i32,
year_day: u32,
data: &C::PrecomputedData,
) -> ArithmeticDate<C> {
let mut month = 1;
let mut day = year_day as i32;
while month <= C::months_for_every_year(year, data) {
Expand Down Expand Up @@ -310,18 +314,17 @@ impl<C: CalendarArithmetic> ArithmeticDate<C> {
day: u8,
data: &C::PrecomputedData,
) -> Result<Self, CalendarError> {
Self::new_from_ordinals(year, month, day, data)
Self::new_from_ordinals_with_data(year, month, day, data)
}
}


/// Convenience methods for dataless calendars (the majority)
///
/// May be removed in the long run
impl<C: CalendarArithmetic<PrecomputedData = ()>> ArithmeticDate<C> {
#[inline]
pub fn max_date() -> Self {
self.max_date_with_data(&())
Self::max_date_with_data(&())
}

#[inline]
Expand Down Expand Up @@ -391,21 +394,13 @@ impl<C: CalendarArithmetic<PrecomputedData = ()>> ArithmeticDate<C> {
/// Construct a new arithmetic date from a year, month ordinal, and day, bounds checking
/// the month and day
/// Originally (new_from_solar_ordinals) but renamed because it works for some lunar calendars
pub fn new_from_ordinals(
year: i32,
month: u8,
day: u8,
) -> Result<Self, CalendarError> {
pub fn new_from_ordinals(year: i32, month: u8, day: u8) -> Result<Self, CalendarError> {
Self::new_from_ordinals_with_data(year, month, day, &())
}

/// This fn currently just calls [`new_from_ordinals`], but exists separately for
/// lunar calendars in case different logic needs to be implemented later.
pub fn new_from_lunar_ordinals(
year: i32,
month: u8,
day: u8,
) -> Result<Self, CalendarError> {
pub fn new_from_lunar_ordinals(year: i32, month: u8, day: u8) -> Result<Self, CalendarError> {
Self::new_from_lunar_ordinals_with_data(year, month, day, &())
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/calendar/src/chinese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Calendar for Chinese {
}

fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Self::is_leap_year(date.0 .0.year)
Self::is_leap_year(date.0 .0.year, &())
}

/// The calendar-specific month code represented by `date`;
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Calendar for Chinese {
day_of_year: date.0.day_of_year(),
days_in_year: date.0.days_in_year_inner(),
prev_year: Self::format_chinese_year(prev_year, None),
days_in_prev_year: Self::days_in_provided_year(prev_year),
days_in_prev_year: Self::days_in_provided_year(prev_year, &()),
next_year: Self::format_chinese_year(next_year, None),
}
}
Expand Down
14 changes: 7 additions & 7 deletions components/calendar/src/chinese_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,22 @@ impl<C: ChineseBasedWithDataLoading> ChineseBasedDateInner<C> {
}

impl<C: ChineseBasedWithDataLoading> CalendarArithmetic for C {
type Cache = ();
fn month_days(year: i32, month: u8, cache: &()) -> u8 {
type PrecomputedData = ();
fn month_days(year: i32, month: u8, _data: &Self::PrecomputedData) -> u8 {
chinese_based::month_days::<C::CB>(year, month)
}

/// Returns the number of months in a given year, which is 13 in a leap year, and 12 in a common year.
fn months_for_every_year(year: i32, cache: &()) -> u8 {
if Self::is_leap_year(year) {
fn months_for_every_year(year: i32, data: &Self::PrecomputedData) -> u8 {
if Self::is_leap_year(year, data) {
13
} else {
12
}
}

/// Returns true if the given year is a leap year, and false if not.
fn is_leap_year(year: i32, cache: &()) -> bool {
fn is_leap_year(year: i32, _data: &Self::PrecomputedData) -> bool {
if let Some(data) = C::get_compiled_data_for_year(year) {
data.leap_month.is_some()
} else {
Expand All @@ -494,7 +494,7 @@ impl<C: ChineseBasedWithDataLoading> CalendarArithmetic for C {
/// The last month in a year will always be 12 in a common year or 13 in a leap year. The day is
/// determined by finding the day immediately before the next new year and calculating the number
/// of days since the last new moon (beginning of the last month in the year).
fn last_month_day_in_year(year: i32, cache: &()) -> (u8, u8) {
fn last_month_day_in_year(year: i32, _data: &Self::PrecomputedData) -> (u8, u8) {
if let Some(data) = C::get_compiled_data_for_year(year) {
if data.leap_month.is_some() {
(13, data.days_in_month(13))
Expand All @@ -506,7 +506,7 @@ impl<C: ChineseBasedWithDataLoading> CalendarArithmetic for C {
}
}

fn days_in_provided_year(year: i32, cache: &()) -> u16 {
fn days_in_provided_year(year: i32, _data: &Self::PrecomputedData) -> u16 {
if let Some(data) = C::get_compiled_data_for_year(year) {
data.last_day_of_month(13)
} else {
Expand Down
21 changes: 11 additions & 10 deletions components/calendar/src/coptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ pub struct Coptic;
pub struct CopticDateInner(pub(crate) ArithmeticDate<Coptic>);

impl CalendarArithmetic for Coptic {
fn month_days(year: i32, month: u8) -> u8 {
type PrecomputedData = ();
fn month_days(year: i32, month: u8, _data: &()) -> u8 {
if (1..=12).contains(&month) {
30
} else if month == 13 {
if Self::is_leap_year(year) {
if Self::is_leap_year(year, &()) {
6
} else {
5
Expand All @@ -80,24 +81,24 @@ impl CalendarArithmetic for Coptic {
}
}

fn months_for_every_year(_: i32) -> u8 {
fn months_for_every_year(_: i32, _data: &()) -> u8 {
13
}

fn is_leap_year(year: i32) -> bool {
fn is_leap_year(year: i32, _data: &()) -> bool {
year % 4 == 3
}

fn last_month_day_in_year(year: i32) -> (u8, u8) {
if Self::is_leap_year(year) {
fn last_month_day_in_year(year: i32, _data: &()) -> (u8, u8) {
if Self::is_leap_year(year, &()) {
(13, 6)
} else {
(13, 5)
}
}

fn days_in_provided_year(year: i32) -> u16 {
if Self::is_leap_year(year) {
fn days_in_provided_year(year: i32, _data: &()) -> u16 {
if Self::is_leap_year(year, &()) {
366
} else {
365
Expand Down Expand Up @@ -177,7 +178,7 @@ impl Calendar for Coptic {
}

fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Self::is_leap_year(date.0.year)
Self::is_leap_year(date.0.year, &())
}

fn month(&self, date: &Self::DateInner) -> types::FormattableMonth {
Expand Down Expand Up @@ -225,7 +226,7 @@ impl Coptic {
}

fn days_in_year_direct(year: i32) -> u16 {
if Coptic::is_leap_year(year) {
if Coptic::is_leap_year(year, &()) {
366
} else {
365
Expand Down
4 changes: 2 additions & 2 deletions components/calendar/src/dangi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Calendar for Dangi {
}

fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Self::is_leap_year(date.0 .0.year)
Self::is_leap_year(date.0 .0.year, &())
}

fn month(&self, date: &Self::DateInner) -> crate::types::FormattableMonth {
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Calendar for Dangi {
day_of_year: date.0 .0.day_of_year(),
days_in_year: date.0.days_in_year_inner(),
prev_year: Self::format_dangi_year(prev_year, None),
days_in_prev_year: Self::days_in_provided_year(prev_year),
days_in_prev_year: Self::days_in_provided_year(prev_year, &()),
next_year: Self::format_dangi_year(next_year, None),
}
}
Expand Down
21 changes: 11 additions & 10 deletions components/calendar/src/ethiopian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ pub struct Ethiopian(pub(crate) bool);
pub struct EthiopianDateInner(ArithmeticDate<Ethiopian>);

impl CalendarArithmetic for Ethiopian {
fn month_days(year: i32, month: u8) -> u8 {
type PrecomputedData = ();
fn month_days(year: i32, month: u8, _data: &()) -> u8 {
if (1..=12).contains(&month) {
30
} else if month == 13 {
if Self::is_leap_year(year) {
if Self::is_leap_year(year, &()) {
6
} else {
5
Expand All @@ -100,24 +101,24 @@ impl CalendarArithmetic for Ethiopian {
}
}

fn months_for_every_year(_: i32) -> u8 {
fn months_for_every_year(_: i32, _data: &()) -> u8 {
13
}

fn is_leap_year(year: i32) -> bool {
fn is_leap_year(year: i32, _data: &()) -> bool {
year % 4 == 3
}

fn last_month_day_in_year(year: i32) -> (u8, u8) {
if Self::is_leap_year(year) {
fn last_month_day_in_year(year: i32, _data: &()) -> (u8, u8) {
if Self::is_leap_year(year, &()) {
(13, 6)
} else {
(13, 5)
}
}

fn days_in_provided_year(year: i32) -> u16 {
if Self::is_leap_year(year) {
fn days_in_provided_year(year: i32, _data: &()) -> u16 {
if Self::is_leap_year(year, &()) {
366
} else {
365
Expand Down Expand Up @@ -199,7 +200,7 @@ impl Calendar for Ethiopian {
}

fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Self::is_leap_year(date.0.year)
Self::is_leap_year(date.0.year, &())
}

fn month(&self, date: &Self::DateInner) -> types::FormattableMonth {
Expand Down Expand Up @@ -277,7 +278,7 @@ impl Ethiopian {
}

fn days_in_year_direct(year: i32) -> u16 {
if Ethiopian::is_leap_year(year) {
if Ethiopian::is_leap_year(year, &()) {
366
} else {
365
Expand Down
21 changes: 11 additions & 10 deletions components/calendar/src/hebrew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,25 @@ impl Hebrew {
// HEBREW CALENDAR

impl CalendarArithmetic for Hebrew {
fn month_days(civil_year: i32, civil_month: u8) -> u8 {
type PrecomputedData = ();
fn month_days(civil_year: i32, civil_month: u8, _data: &()) -> u8 {
Self::last_day_of_civil_hebrew_month(civil_year, civil_month)
}

fn months_for_every_year(civil_year: i32) -> u8 {
fn months_for_every_year(civil_year: i32, _data: &()) -> u8 {
Self::last_month_of_civil_hebrew_year(civil_year)
}

fn days_in_provided_year(civil_year: i32) -> u16 {
fn days_in_provided_year(civil_year: i32, _data: &()) -> u16 {
BookHebrew::days_in_book_hebrew_year(civil_year) // number of days don't change between BookHebrew and Civil Hebrew
}

fn is_leap_year(civil_year: i32) -> bool {
fn is_leap_year(civil_year: i32, _data: &()) -> bool {
// civil and book years are the same
BookHebrew::is_hebrew_leap_year(civil_year)
}

fn last_month_day_in_year(civil_year: i32) -> (u8, u8) {
fn last_month_day_in_year(civil_year: i32, _data: &()) -> (u8, u8) {
let civil_month = Self::last_month_of_civil_hebrew_year(civil_year);
let civil_day = Self::last_day_of_civil_hebrew_month(civil_year, civil_month);

Expand All @@ -125,7 +126,7 @@ impl Calendar for Hebrew {
month_code: types::MonthCode,
day: u8,
) -> Result<Self::DateInner, CalendarError> {
let is_leap_year = Self::is_leap_year(year);
let is_leap_year = Self::is_leap_year(year, &());
let year = if era.0 == tinystr!(16, "hebrew") || era.0 == tinystr!(16, "am") {
year
} else {
Expand Down Expand Up @@ -229,12 +230,12 @@ impl Calendar for Hebrew {
}

fn is_in_leap_year(&self, date: &Self::DateInner) -> bool {
Self::is_leap_year(date.0.year)
Self::is_leap_year(date.0.year, &())
}

fn month(&self, date: &Self::DateInner) -> FormattableMonth {
let mut ordinal = date.0.month;
let is_leap_year = Self::is_leap_year(date.0.year);
let is_leap_year = Self::is_leap_year(date.0.year, &());

if is_leap_year {
if ordinal == 6 {
Expand Down Expand Up @@ -287,7 +288,7 @@ impl Calendar for Hebrew {
day_of_year: date.0.day_of_year(),
days_in_year: date.0.days_in_year(),
prev_year: Self::year_as_hebrew(prev_year),
days_in_prev_year: Self::days_in_provided_year(prev_year),
days_in_prev_year: Self::days_in_provided_year(prev_year, &()),
next_year: Self::year_as_hebrew(next_year),
}
}
Expand All @@ -311,7 +312,7 @@ impl Hebrew {
}

fn last_month_of_civil_hebrew_year(civil_year: i32) -> u8 {
if Self::is_leap_year(civil_year) {
if Self::is_leap_year(civil_year, &()) {
13 // there are 13 months in a leap year
} else {
12
Expand Down
Loading

0 comments on commit be2520e

Please sign in to comment.