From a5aa9cb817b2e7fbe35fd31b90fbc9ac00cea74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BB=A9c=20To=C3=A0n?= <33489972+ndtoan96@users.noreply.github.com> Date: Sun, 1 Oct 2023 08:42:56 +0700 Subject: [PATCH] fix: a logic error in `Step` (#233) --- core/src/manager/folder.rs | 4 ++-- core/src/step.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/src/manager/folder.rs b/core/src/manager/folder.rs index 6899d1f7..02f92965 100644 --- a/core/src/manager/folder.rs +++ b/core/src/manager/folder.rs @@ -106,9 +106,9 @@ impl Folder { pub fn hover(&mut self, url: &Url) -> bool { let new = self.files.position(url).unwrap_or(self.cursor); if new > self.cursor { - self.next(Step::from(new - self.cursor)) + self.next(Step::next(new - self.cursor)) } else { - self.prev(Step::from(self.cursor - new)) + self.prev(Step::prev(self.cursor - new)) } } diff --git a/core/src/step.rs b/core/src/step.rs index 5faa2459..153d637e 100644 --- a/core/src/step.rs +++ b/core/src/step.rs @@ -1,5 +1,6 @@ use std::{num::ParseIntError, str::FromStr}; +#[derive(Clone, Copy)] pub enum Step { Fixed(isize), Percent(i8), @@ -25,29 +26,28 @@ impl From for Step { fn from(n: isize) -> Self { Self::Fixed(n) } } -impl From for Step { - fn from(n: usize) -> Self { Self::Fixed(n as isize) } +impl Step { + #[inline] + pub fn prev(n: usize) -> Self { Self::Fixed(-(n as isize)) } + + #[inline] + pub fn next(n: usize) -> Self { Self::Fixed(n as isize) } } impl Step { #[inline] - fn fixed usize>(self, f: F) -> isize { - match self { + pub fn add usize>(self, pos: usize, f: F) -> usize { + let fixed = match self { Self::Fixed(n) => n, Self::Percent(0) => 0, Self::Percent(n) => n as isize * f() as isize / 100, - } - } - - #[inline] - pub fn add usize>(self, pos: usize, f: F) -> usize { - let fixed = self.fixed(f); + }; if fixed > 0 { pos + fixed as usize } else { pos.saturating_sub(fixed.unsigned_abs()) } } #[inline] - pub fn is_positive(&self) -> bool { - match *self { + pub fn is_positive(self) -> bool { + match self { Self::Fixed(n) => n > 0, Self::Percent(n) => n > 0, }