use crate::grid::{
config::Entity,
records::{ExactRecords, Records, RecordsMut},
};
pub trait CellOption<R, C> {
fn change(self, records: &mut R, cfg: &mut C, entity: Entity);
fn hint_change(&self) -> Option<Entity> {
Some(Entity::Global)
}
}
#[cfg(feature = "std")]
impl<R, C> CellOption<R, C> for String
where
R: Records + ExactRecords + RecordsMut<String>,
{
fn change(self, records: &mut R, cfg: &mut C, entity: Entity) {
(&self).change(records, cfg, entity);
}
}
#[cfg(feature = "std")]
impl<R, C> CellOption<R, C> for &String
where
R: Records + ExactRecords + RecordsMut<String>,
{
fn change(self, records: &mut R, _: &mut C, entity: Entity) {
let count_rows = records.count_rows();
let count_cols = records.count_columns();
for pos in entity.iter(count_rows, count_cols) {
records.set(pos, self.clone());
}
}
}
impl<'a, R, C> CellOption<R, C> for &'a str
where
R: Records + ExactRecords + RecordsMut<&'a str>,
{
fn change(self, records: &mut R, _: &mut C, entity: Entity) {
let count_rows = records.count_rows();
let count_cols = records.count_columns();
for pos in entity.iter(count_rows, count_cols) {
records.set(pos, self);
}
}
}
#[cfg(feature = "std")]
macro_rules! tuple_trait_impl {
( $($name:ident)+ ) => {
impl<R, C, $($name: CellOption<R, C>),+> CellOption<R, C> for ($($name,)+) {
fn change(self, records: &mut R, cfg: &mut C, entity: Entity) {
#![allow(non_snake_case)]
let ($($name,)+) = self;
$(
$name::change($name, records, cfg, entity);
)+
}
fn hint_change(&self) -> Option<Entity> {
#![allow(non_snake_case)]
let ($($name,)+) = &self;
let list = [
$(
$name::hint_change($name),
)+
];
crate::settings::table_option::hint_change_list(&list)
}
}
};
}
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4 T5);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4 T5 T6);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4 T5 T6 T7);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4 T5 T6 T7 T8);
#[cfg(feature = "std")]
tuple_trait_impl!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9);