1 | use crate::grid::{ |
2 | config::Entity, |
3 | records::{ExactRecords, Records, RecordsMut}, |
4 | }; |
5 | |
6 | /// A trait for configuring a single cell. |
7 | /// |
8 | /// ~~~~ Where cell represented by 'row' and 'column' indexes. ~~~~ |
9 | /// |
10 | /// A cell can be targeted by [`Cell`]. |
11 | /// |
12 | /// [`Cell`]: crate::object::Cell |
13 | pub trait CellOption<R, C> { |
14 | /// Modification function of a certail part of a grid targeted by [`Entity`]. |
15 | fn change(self, records: &mut R, cfg: &mut C, entity: Entity); |
16 | |
17 | /// A hint whether an [`TableOption`] is going to change table layout. |
18 | /// |
19 | /// Return [`None`] if no changes are being done. |
20 | /// Otherwise return: |
21 | /// |
22 | /// - [Entity::Global] - a grand layout changed. |
23 | /// - [Entity::Row] - a certain row was changed. |
24 | /// - [Entity::Column] - a certain column was changed. |
25 | /// - [Entity::Cell] - a certain cell was changed. |
26 | /// |
27 | /// By default it's considered to be a grand change. |
28 | /// |
29 | /// This methods primarily is used as an optimization, |
30 | /// to not make unnessary calculations if they're not needed, after using the [`TableOption`]. |
31 | /// |
32 | /// [`TableOption`]: crate::settings::TableOption |
33 | fn hint_change(&self) -> Option<Entity> { |
34 | Some(Entity::Global) |
35 | } |
36 | } |
37 | |
38 | #[cfg (feature = "std" )] |
39 | impl<R, C> CellOption<R, C> for String |
40 | where |
41 | R: Records + ExactRecords + RecordsMut<String>, |
42 | { |
43 | fn change(self, records: &mut R, cfg: &mut C, entity: Entity) { |
44 | (&self).change(records, cfg, entity); |
45 | } |
46 | } |
47 | |
48 | #[cfg (feature = "std" )] |
49 | impl<R, C> CellOption<R, C> for &String |
50 | where |
51 | R: Records + ExactRecords + RecordsMut<String>, |
52 | { |
53 | fn change(self, records: &mut R, _: &mut C, entity: Entity) { |
54 | let count_rows: usize = records.count_rows(); |
55 | let count_cols: usize = records.count_columns(); |
56 | |
57 | for pos: (usize, usize) in entity.iter(count_rows, count_cols) { |
58 | records.set(pos, self.clone()); |
59 | } |
60 | } |
61 | } |
62 | |
63 | impl<'a, R, C> CellOption<R, C> for &'a str |
64 | where |
65 | R: Records + ExactRecords + RecordsMut<&'a str>, |
66 | { |
67 | fn change(self, records: &mut R, _: &mut C, entity: Entity) { |
68 | let count_rows: usize = records.count_rows(); |
69 | let count_cols: usize = records.count_columns(); |
70 | |
71 | for pos: (usize, usize) in entity.iter(count_rows, count_cols) { |
72 | records.set(pos, self); |
73 | } |
74 | } |
75 | } |
76 | |