From 1bc54960dbc4fdd1a57051995fe9a2a4f21d12d4 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 4 Jan 2024 10:29:41 -0800 Subject: [PATCH] saturate --- utils/calendrical_calculations/src/hebrew_keviyah.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/utils/calendrical_calculations/src/hebrew_keviyah.rs b/utils/calendrical_calculations/src/hebrew_keviyah.rs index bb73d47ce7f..1dbab000008 100644 --- a/utils/calendrical_calculations/src/hebrew_keviyah.rs +++ b/utils/calendrical_calculations/src/hebrew_keviyah.rs @@ -204,11 +204,17 @@ impl YearInfo { let (yi, h_year) = match cmp { // The approx year is a year greater. Go one year down - Ordering::Greater => (Self::compute_for(approx - 1), approx - 1), + Ordering::Greater => { + let prev = approx.saturating_sub(1); + (Self::compute_for(prev), prev) + } // Bullseye Ordering::Equal => (yi, approx), // The approx year is a year lower. Go one year up. - Ordering::Less => (Self::compute_for(approx + 1), approx + 1), + Ordering::Less => { + let next = approx.saturating_add(1); + (Self::compute_for(next), next) + } }; debug_assert!(yi.compare(date).is_eq() || maybe_approx.is_err(), // The data will be incorrect if we saturate, and that's expected