Skip to content

Commit

Permalink
tabled/ support Rows::last() + 2 operation (#442)
Browse files Browse the repository at this point in the history
It might be usefull in some cases I guess?
  • Loading branch information
zhiburt authored Nov 13, 2024
1 parent 36aa1de commit e20e2f8
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions tabled/src/settings/object/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -156,14 +156,36 @@ impl Sub<usize> for LastRow {
type Output = LastRowOffset;

fn sub(self, rhs: usize) -> Self::Output {
LastRowOffset { offset: rhs }
LastRowOffset::sub(rhs)
}
}

impl Add<usize> for LastRow {
type Output = LastRowOffset;

fn add(self, rhs: usize) -> Self::Output {
LastRowOffset::add(rhs)
}
}

/// A row which is located by an offset from the last row.
#[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<I> Object<I> for LastRowOffset
Expand All @@ -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)))
}
}
}

Expand Down

0 comments on commit e20e2f8

Please sign in to comment.