From cf090b8c60941c77af2a6fe605804af710cb0186 Mon Sep 17 00:00:00 2001 From: Jarrett Ye Date: Thu, 31 Jul 2025 18:52:55 +0800 Subject: [PATCH] Refactor rating logic in RevlogEntry and update related scheduling functions This commit introduces a new `has_rating` method in the `RevlogEntry` struct to encapsulate the logic for checking if an entry has a rating. The scheduling logic in `params.rs` and the calculation of normal answer counts in `card.rs` have been updated to use this new method, enhancing code clarity and maintainability. --- rslib/src/revlog/mod.rs | 6 +++++- rslib/src/scheduler/fsrs/params.rs | 13 ++----------- rslib/src/stats/card.rs | 2 +- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/rslib/src/revlog/mod.rs b/rslib/src/revlog/mod.rs index 336302eaa..1c278eab8 100644 --- a/rslib/src/revlog/mod.rs +++ b/rslib/src/revlog/mod.rs @@ -102,12 +102,16 @@ impl RevlogEntry { self.review_kind == RevlogReviewKind::Filtered && self.ease_factor == 0 } + pub(crate) fn has_rating(&self) -> bool { + self.button_chosen > 0 + } + /// Returns true if the review entry is not manually rescheduled and not /// cramming. Used to filter out entries that shouldn't be considered /// for statistics and scheduling. pub(crate) fn has_rating_and_affects_scheduling(&self) -> bool { // not rescheduled/set due date/reset - self.button_chosen > 0 + self.has_rating() // not cramming && !self.is_cramming() } diff --git a/rslib/src/scheduler/fsrs/params.rs b/rslib/src/scheduler/fsrs/params.rs index a96062b99..588440191 100644 --- a/rslib/src/scheduler/fsrs/params.rs +++ b/rslib/src/scheduler/fsrs/params.rs @@ -400,7 +400,7 @@ pub(crate) fn reviews_for_fsrs( // For incomplete review histories, initial memory state is based on the first // user-graded review after the cutoff date with interval >= 1d. let within_cutoff = entry.id.0 > ignore_revlogs_before.0; - let user_graded = matches!(entry.button_chosen, 1..=4); + let user_graded = entry.has_rating(); let interday = entry.interval >= 1 || entry.interval <= -86400; if user_graded && within_cutoff && interday { first_user_grade_idx = Some(index); @@ -469,16 +469,7 @@ pub(crate) fn reviews_for_fsrs( } // Filter out unwanted entries - entries.retain(|entry| { - !( - // set due date, reset or rescheduled - (entry.review_kind == RevlogReviewKind::Manual || entry.button_chosen == 0) - || // cram - entry.is_cramming() - || // rescheduled - (entry.review_kind == RevlogReviewKind::Rescheduled) - ) - }); + entries.retain(|entry| entry.has_rating_and_affects_scheduling()); // Compute delta_t for each entry let delta_ts = iter::once(0) diff --git a/rslib/src/stats/card.rs b/rslib/src/stats/card.rs index fdab209c8..e9a5a9bc4 100644 --- a/rslib/src/stats/card.rs +++ b/rslib/src/stats/card.rs @@ -187,7 +187,7 @@ impl Collection { } fn average_and_total_secs_strings(revlog: &[RevlogEntry]) -> (f32, f32) { - let normal_answer_count = revlog.iter().filter(|r| r.button_chosen > 0).count(); + let normal_answer_count = revlog.iter().filter(|r| r.has_rating()).count(); let total_secs: f32 = revlog .iter() .map(|entry| (entry.taken_millis as f32) / 1000.0)