1use crate::{
2 grid::records::{ExactRecords, Records},
3 settings::{object::Object, CellOption, Settings, TableOption},
4};
5
6/// Modify structure provide an abstraction, to be able to apply
7/// a set of [`CellOption`]s to the same object.
8///
9/// Be aware that the settings are applied all to a cell at a time.
10/// So sometimes you may need to make a several calls of [`Modify`] in order to achieve the desired affect.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub struct Modify<O> {
13 obj: O,
14}
15
16impl<O> Modify<O> {
17 /// Creates a new [`Modify`] without any options.
18 pub const fn new(obj: O) -> Self {
19 Self { obj }
20 }
21
22 /// A function which combines together [`Modify::new`] and [`Modify::with`] calls.
23 pub const fn list<M>(obj: O, next: M) -> ModifyList<O, M> {
24 ModifyList {
25 obj,
26 modifiers: next,
27 }
28 }
29
30 /// It's a generic function which stores a [`CellOption`].
31 ///
32 /// IMPORTANT:
33 /// The function *doesn't* changes a [`Table`].
34 /// [`Table`] will be changed only after passing [`Modify`] object to [`Table::with`].
35 ///
36 /// [`Table`]: crate::Table
37 /// [`Table::with`]: crate::Table::with
38 pub fn with<M>(self, next: M) -> ModifyList<O, M> {
39 ModifyList {
40 obj: self.obj,
41 modifiers: next,
42 }
43 }
44}
45
46/// This is a container of [`CellOption`]s which are applied to a set [`Object`].
47#[derive(Debug)]
48pub struct ModifyList<O, S> {
49 obj: O,
50 modifiers: S,
51}
52
53impl<O, M1> ModifyList<O, M1> {
54 /// With a generic function which stores a [`CellOption`].
55 ///
56 /// IMPORTANT:
57 /// The function *doesn't* changes a [`Table`].
58 /// [`Table`] will be changed only after passing [`Modify`] object to [`Table::with`].
59 ///
60 /// [`Table`]: crate::Table
61 /// [`Table::with`]: crate::Table::with
62 pub fn with<M2>(self, next: M2) -> ModifyList<O, Settings<M1, M2>> {
63 ModifyList {
64 obj: self.obj,
65 modifiers: Settings::new(self.modifiers, settings2:next),
66 }
67 }
68}
69
70impl<O, M, R, D, C> TableOption<R, D, C> for ModifyList<O, M>
71where
72 O: Object<R>,
73 M: CellOption<R, C> + Clone,
74 R: Records + ExactRecords,
75{
76 fn change(self, records: &mut R, cfg: &mut C, _: &mut D) {
77 for entity: Entity in self.obj.cells(records) {
78 self.modifiers.clone().change(records, cfg, entity);
79 }
80 }
81}
82