From e20e2f8129addb6a7527c1a1a3f8d4cd30d65535 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Wed, 13 Nov 2024 20:26:44 +0300 Subject: [PATCH] tabled/ support `Rows::last() + 2` operation (#442) It might be usefull in some cases I guess? --- tabled/src/settings/object/rows.rs | 44 ++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/tabled/src/settings/object/rows.rs b/tabled/src/settings/object/rows.rs index 1f34988e..c4977819 100644 --- a/tabled/src/settings/object/rows.rs +++ b/tabled/src/settings/object/rows.rs @@ -146,7 +146,7 @@ where return EntityOnce::new(None); } - let row = if count_rows == 0 { 0 } else { count_rows - 1 }; + let row = count_rows - 1; EntityOnce::new(Some(Entity::Row(row))) } @@ -156,7 +156,15 @@ impl Sub for LastRow { type Output = LastRowOffset; fn sub(self, rhs: usize) -> Self::Output { - LastRowOffset { offset: rhs } + LastRowOffset::sub(rhs) + } +} + +impl Add for LastRow { + type Output = LastRowOffset; + + fn add(self, rhs: usize) -> Self::Output { + LastRowOffset::add(rhs) } } @@ -164,6 +172,20 @@ impl Sub for LastRow { #[derive(Debug)] pub struct LastRowOffset { offset: usize, + sign: bool, +} + +impl LastRowOffset { + fn sub(offset: usize) -> Self { + Self { + offset, + sign: false, + } + } + + fn add(offset: usize) -> Self { + Self { offset, sign: true } + } } impl Object for LastRowOffset @@ -178,13 +200,19 @@ where return EntityOnce::new(None); } - let row = if count_rows == 0 { 0 } else { count_rows - 1 }; - if self.offset > row { - return EntityOnce::new(None); - } + let last_row = count_rows - 1; - let row = row - self.offset; - EntityOnce::new(Some(Entity::Row(row))) + if self.sign { + let row = last_row + self.offset; + EntityOnce::new(Some(Entity::Row(row))) + } else { + if self.offset > last_row { + return EntityOnce::new(None); + } + + let row = last_row - self.offset; + EntityOnce::new(Some(Entity::Row(row))) + } } }