From 88c75d73b695804a7813910a8c6f318d75067f03 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 14 Jan 2021 19:58:27 +0100 Subject: [PATCH] Pass in the the negative offset day to write_rated --- rslib/src/search/parser.rs | 6 +++--- rslib/src/search/sqlwriter.rs | 40 +++++++++++++++-------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/rslib/src/search/parser.rs b/rslib/src/search/parser.rs index a6db30cba..67365d7ec 100644 --- a/rslib/src/search/parser.rs +++ b/rslib/src/search/parser.rs @@ -87,7 +87,7 @@ pub enum PropertyKind { Lapses(u32), Ease(f32), Position(u32), - Rated(u32, EaseKind), + Rated(i32, EaseKind), } #[derive(Debug, PartialEq, Clone)] @@ -439,8 +439,8 @@ fn parse_prop(s: &str) -> ParseResult { } else if key == "resched" { let mut it = val.splitn(2, ':'); - let n: u32 = it.next().unwrap().parse()?; - let days = n.max(1).min(365); + let n: i32 = it.next().unwrap().parse()?; + let days = n.max(-365).min(0); let ease = EaseKind::ManualReschedule; diff --git a/rslib/src/search/sqlwriter.rs b/rslib/src/search/sqlwriter.rs index 60da1b8a4..3c9997529 100644 --- a/rslib/src/search/sqlwriter.rs +++ b/rslib/src/search/sqlwriter.rs @@ -144,7 +144,7 @@ impl SqlWriter<'_> { write!(self.sql, "c.did = {}", did).unwrap(); } SearchNode::NoteType(notetype) => self.write_note_type(&norm(notetype))?, - SearchNode::Rated { days, ease } => self.write_rated(*days, ease, "<")?, + SearchNode::Rated { days, ease } => self.write_rated(-i64::from(*days), ease, ">")?, SearchNode::Tag(tag) => self.write_tag(&norm(tag))?, SearchNode::State(state) => self.write_state(state)?, @@ -214,28 +214,22 @@ impl SqlWriter<'_> { Ok(()) } - fn write_rated(&mut self, days: u32, ease: &EaseKind, op: &str) -> Result<()> { + fn write_rated(&mut self, days: i64, ease: &EaseKind, op: &str) -> Result<()> { let today_cutoff = self.col.timing_today()?.next_day_at; - let target_cutoff_ms = (today_cutoff - 86_400 * i64::from(days)) * 1_000; - let day_before_cutoff_ms = (today_cutoff - 86_400 * i64::from(days - 1)) * 1_000; + let target_cutoff_ms = (today_cutoff + 86_400 * days) * 1_000; + let day_before_cutoff_ms = (today_cutoff + 86_400 * (days - 1)) * 1_000; - write!( - self.sql, - "c.id in (select cid from revlog where id>{}", - target_cutoff_ms, - ) - .unwrap(); + write!(self.sql, "c.id in (select cid from revlog where id").unwrap(); - // We use positive numbers for negative offsets - // which is why operators are reversed match op { - "<" => write!(self.sql, "{} {}", ">", target_cutoff_ms), - ">" => write!(self.sql, "{} {}", "<", day_before_cutoff_ms), - "<=" => write!(self.sql, "{} {}", ">", day_before_cutoff_ms), - ">=" => write!(self.sql, "{} {}", "<", target_cutoff_ms), - "=" => write!(self.sql, "between {} and {}", target_cutoff_ms, day_before_cutoff_ms), - _ /* "!=" */ => write!(self.sql, "not between {} and {}", target_cutoff_ms, day_before_cutoff_ms), - }.unwrap(); + ">" => write!(self.sql, " {} {}", ">", target_cutoff_ms), + "<" => write!(self.sql, " {} {}", "<", day_before_cutoff_ms), + ">=" => write!(self.sql, " {} {}", ">", day_before_cutoff_ms), + "<=" => write!(self.sql, " {} {}", "<", target_cutoff_ms), + "=" => write!(self.sql, " between {} and {}", day_before_cutoff_ms, target_cutoff_ms), + _ /* "!=" */ => write!(self.sql, " not between {} and {}", day_before_cutoff_ms, target_cutoff_ms), + } + .unwrap(); match ease { EaseKind::AnswerButton(u) => write!(self.sql, " and ease = {})", u), @@ -285,7 +279,7 @@ impl SqlWriter<'_> { PropertyKind::Ease(ease) => { write!(self.sql, "factor {} {}", op, (ease * 1000.0) as u32).unwrap() } - PropertyKind::Rated(days, ease) => self.write_rated(*days, ease, op)? + PropertyKind::Rated(days, ease) => self.write_rated(i64::from(*days), ease, op)? } Ok(()) @@ -733,14 +727,14 @@ mod test { assert_eq!( s(ctx, "rated:2").0, format!( - "(c.id in (select cid from revlog where id>{} and ease > 0))", + "(c.id in (select cid from revlog where id > {} and ease > 0))", (timing.next_day_at - (86_400 * 2)) * 1_000 ) ); assert_eq!( s(ctx, "rated:400:1").0, format!( - "(c.id in (select cid from revlog where id>{} and ease = 1))", + "(c.id in (select cid from revlog where id > {} and ease = 1))", (timing.next_day_at - (86_400 * 365)) * 1_000 ) ); @@ -750,7 +744,7 @@ mod test { assert_eq!( s(ctx, "resched:400").0, format!( - "(c.id in (select cid from revlog where id>{} and ease = 0))", + "(c.id in (select cid from revlog where id > {} and ease = 0))", (timing.next_day_at - (86_400 * 365)) * 1_000 ) );