use std::marker::PhantomData;
use papergrid::records::{Records, Resizable};
use crate::{locator::Locator, Table, TableOption};
#[derive(Debug)]
pub struct Disable<L, Target> {
locator: L,
target: PhantomData<Target>,
}
impl<L> Disable<L, TargetColumn> {
pub fn column(locator: L) -> Self
where
L: Locator<Coordinate = usize>,
{
Self {
locator,
target: PhantomData,
}
}
}
impl<L> Disable<L, TargetRow> {
pub fn row(locator: L) -> Self
where
L: Locator<Coordinate = usize>,
{
Self {
locator,
target: PhantomData,
}
}
}
#[derive(Debug)]
pub struct TargetRow;
#[derive(Debug)]
pub struct TargetColumn;
impl<L, D> TableOption<D> for Disable<L, TargetColumn>
where
L: Locator<Coordinate = usize>,
D: Records + Resizable,
{
fn change(&mut self, table: &mut Table<D>) {
let columns = self.locator.locate(table.get_records());
let records = table.get_records_mut();
let mut shift = 0;
for col in columns.into_iter() {
if col - shift > records.count_columns() {
continue;
}
records.remove_column(col - shift);
shift += 1;
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}
impl<L, D> TableOption<D> for Disable<L, TargetRow>
where
L: Locator<Coordinate = usize>,
D: Records + Resizable,
{
fn change(&mut self, table: &mut Table<D>) {
let rows = self.locator.locate(table.get_records());
let records = table.get_records_mut();
let mut shift = 0;
for row in rows.into_iter() {
if row - shift > records.count_rows() {
continue;
}
records.remove_row(row - shift);
shift += 1;
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}