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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::{
    grid::{
        config::Entity,
        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.
    ///
    /// ```
    /// use tabled::{Table, settings::{Modify, Padding, object::Rows}};
    ///
    /// let table = Table::new(&["Year", "2021"])
    ///     .with(Modify::list(Rows::first(), Padding::new(1, 1, 1, 1)))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+------+\n\
    ///      |      |\n\
    ///      | &str |\n\
    ///      |      |\n\
    ///      +------+\n\
    ///      | Year |\n\
    ///      +------+\n\
    ///      | 2021 |\n\
    ///      +------+"
    /// )
    /// ```
    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`].
    ///
    /// ```
    /// use tabled::{Table, settings::{Modify, Padding, object::Rows}};
    ///
    /// let table = Table::new(&["Year", "2021"])
    ///     .with(Modify::new(Rows::first()).with(Padding::new(1, 1, 1, 1)))
    ///     .to_string();
    ///
    /// assert_eq!(
    ///     table,
    ///     "+------+\n\
    ///      |      |\n\
    ///      | &str |\n\
    ///      |      |\n\
    ///      +------+\n\
    ///      | Year |\n\
    ///      +------+\n\
    ///      | 2021 |\n\
    ///      +------+"
    /// )
    /// ```
    ///
    /// [`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, C, D> 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);
        }
    }

    fn hint_change(&self) -> Option<Entity> {
        self.modifiers.hint_change()
    }
}