| 1 | use crate::grid::config::Entity; |
| 2 | |
| 3 | /// A trait which is responsible for configuration of a [`Table`]. |
| 4 | /// |
| 5 | /// [`Table`]: crate::Table |
| 6 | pub trait TableOption<R, C, D> { |
| 7 | /// The function modificaties of records and a grid configuration. |
| 8 | fn change(self, records: &mut R, cfg: &mut C, dimension: &mut D); |
| 9 | |
| 10 | /// A hint whether an [`TableOption`] is going to change table layout. |
| 11 | /// |
| 12 | /// Return [`None`] if no changes are being done. |
| 13 | /// Otherwise return: |
| 14 | /// |
| 15 | /// - [Entity::Global] - a grand layout changed. (a change which MIGHT require height/width update) |
| 16 | /// - [Entity::Row] - a certain row was changed. (a change which MIGHT require height update) |
| 17 | /// - [Entity::Column] - a certain column was changed. (a change which MIGHT require width update) |
| 18 | /// - [Entity::Cell] - a certain cell was changed. (a local change, no width/height update) |
| 19 | /// |
| 20 | /// By default it's considered to be a grand change. |
| 21 | /// |
| 22 | /// This methods primarily is used as an optimization, |
| 23 | /// to not make unnessary calculations if they're not needed, after using the [`TableOption`]. |
| 24 | fn hint_change(&self) -> Option<Entity> { |
| 25 | Some(Entity::Global) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // todo: probably we could add one more hint but it likely require Vec<Entity>, |
| 30 | // so, as I am not sure about exact interface it's better be commented. |
| 31 | // /// A hint which layout part a [`TableOption`] is going to change. |
| 32 | // /// |
| 33 | // /// Return [`None`] if no part are being changed. |
| 34 | // /// Otherwise return: |
| 35 | // /// |
| 36 | // /// - [Entity::Global] - a total layout affection. |
| 37 | // /// - [Entity::Row] - a certain row affection. |
| 38 | // /// - [Entity::Column] - a certain column affection. |
| 39 | // /// - [Entity::Cell] - a certain cell affection. |
| 40 | // /// |
| 41 | // /// By default it's considered to be a grand change. |
| 42 | // /// |
| 43 | // /// This methods primarily is used as an optimization, |
| 44 | // /// to not make unnessary calculations if they're not needed, after using the [`TableOption`]. |
| 45 | // fn hint_target(&self, records: &R) -> Option<Vec<Entity>> { |
| 46 | // let _ = records; |
| 47 | // Some(vec![Entity::Global]) |
| 48 | // } |
| 49 | |
| 50 | impl<T, R, C, D> TableOption<R, C, D> for &[T] |
| 51 | where |
| 52 | for<'a> &'a T: TableOption<R, C, D>, |
| 53 | { |
| 54 | fn change(self, records: &mut R, cfg: &mut C, dimension: &mut D) { |
| 55 | for opt: &T in self { |
| 56 | opt.change(records, cfg, dimension) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[cfg (feature = "std" )] |
| 62 | impl<T, R, D, C> TableOption<R, C, D> for Vec<T> |
| 63 | where |
| 64 | T: TableOption<R, C, D>, |
| 65 | { |
| 66 | fn change(self, records: &mut R, cfg: &mut C, dimension: &mut D) { |
| 67 | for opt: T in self { |
| 68 | opt.change(records, cfg, dimension) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |