summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/settings/modify.rs
blob: c712a255e6f76537e58fbea376cbcf6f87325d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::{
    grid::records::{ExactRecords, Records},
    settings::{object::Object, CellOption, Settings, TableOption},
};

/// Modify structure provide an abstraction, to be able to apply
/// a set of [`CellOption`]s to the same object.
///
/// Be aware that the settings are applied all to a cell at a time.
/// So sometimes you may need to make a several calls of [`Modify`] in order to achieve the desired affect.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Modify<O> {
    obj: O,
}

impl<O> Modify<O> {
    /// Creates a new [`Modify`] without any options.
    pub const fn new(obj: O) -> Self {
        Self { obj }
    }

    /// A function which combines together [`Modify::new`] and [`Modify::with`] calls.
    pub const fn list<M>(obj: O, next: M) -> ModifyList<O, M> {
        ModifyList {
            obj,
            modifiers: next,
        }
    }

    /// It's a generic function which stores a [`CellOption`].
    ///
    /// IMPORTANT:
    ///     The function *doesn't* changes a [`Table`].
    ///     [`Table`] will be changed only after passing [`Modify`] object to [`Table::with`].
    ///
    /// [`Table`]: crate::Table
    /// [`Table::with`]: crate::Table::with
    pub fn with<M>(self, next: M) -> ModifyList<O, M> {
        ModifyList {
            obj: self.obj,
            modifiers: next,
        }
    }
}

/// This is a container of [`CellOption`]s which are applied to a set [`Object`].
#[derive(Debug)]
pub struct ModifyList<O, S> {
    obj: O,
    modifiers: S,
}

impl<O, M1> ModifyList<O, M1> {
    /// With a generic function which stores a [`CellOption`].
    ///
    /// IMPORTANT:
    ///     The function *doesn't* changes a [`Table`].
    ///     [`Table`] will be changed only after passing [`Modify`] object to [`Table::with`].
    ///
    /// [`Table`]: crate::Table
    /// [`Table::with`]: crate::Table::with
    pub fn with<M2>(self, next: M2) -> ModifyList<O, Settings<M1, M2>> {
        ModifyList {
            obj: self.obj,
            modifiers: Settings::new(self.modifiers, next),
        }
    }
}

impl<O, M, R, D, C> TableOption<R, D, C> for ModifyList<O, M>
where
    O: Object<R>,
    M: CellOption<R, C> + Clone,
    R: Records + ExactRecords,
{
    fn change(self, records: &mut R, cfg: &mut C, _: &mut D) {
        for entity in self.obj.cells(records) {
            self.modifiers.clone().change(records, cfg, entity);
        }
    }
}