tabled/settings/
settings_list.rs

1use crate::{grid::config::Entity, settings::TableOption};
2
3#[cfg(feature = "std")]
4use crate::settings::CellOption;
5
6/// Settings is a combinator of [`TableOption`]s.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
8pub struct Settings<A = EmptySettings, B = EmptySettings>(A, B);
9
10impl Default for Settings<EmptySettings, EmptySettings> {
11    fn default() -> Self {
12        Self(EmptySettings, EmptySettings)
13    }
14}
15
16impl Settings<(), ()> {
17    /// Creates an empty list.
18    pub const fn empty() -> Settings<EmptySettings, EmptySettings> {
19        Settings(EmptySettings, EmptySettings)
20    }
21}
22
23impl<A, B> Settings<A, B> {
24    /// Creates a new combinator.
25    pub const fn new(settings1: A, settings2: B) -> Settings<A, B> {
26        Settings(settings1, settings2)
27    }
28
29    /// Add an option to a combinator.
30    pub const fn with<C>(self, settings: C) -> Settings<Self, C> {
31        Settings(self, settings)
32    }
33}
34
35#[cfg(feature = "std")]
36impl<R, C, A, B> CellOption<R, C> for Settings<A, B>
37where
38    A: CellOption<R, C>,
39    B: CellOption<R, C>,
40{
41    fn change(self, records: &mut R, cfg: &mut C, entity: Entity) {
42        self.0.change(records, cfg, entity);
43        self.1.change(records, cfg, entity);
44    }
45
46    fn hint_change(&self) -> Option<Entity> {
47        match (self.0.hint_change(), self.1.hint_change()) {
48            (None, None) => None,
49            (Some(a), Some(b)) => Some(combine_entity(a, b)),
50            (None, value) => value,
51            (value, None) => value,
52        }
53    }
54}
55
56impl<R, D, C, A, B> TableOption<R, C, D> for Settings<A, B>
57where
58    A: TableOption<R, C, D>,
59    B: TableOption<R, C, D>,
60{
61    fn change(self, records: &mut R, cfg: &mut C, dims: &mut D) {
62        self.0.change(records, cfg, dims);
63        self.1.change(records, cfg, dims);
64    }
65
66    fn hint_change(&self) -> Option<Entity> {
67        match (self.0.hint_change(), self.1.hint_change()) {
68            (None, None) => None,
69            (Some(a), Some(b)) => Some(combine_entity(a, b)),
70            (None, value) => value,
71            (value, None) => value,
72        }
73    }
74}
75
76/// A marker structure to be able to create an empty [`Settings`].
77#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
78pub struct EmptySettings;
79
80#[cfg(feature = "std")]
81impl<R, C> CellOption<R, C> for EmptySettings {
82    fn change(self, _: &mut R, _: &mut C, _: Entity) {}
83}
84
85impl<R, D, C> TableOption<R, C, D> for EmptySettings {
86    fn change(self, _: &mut R, _: &mut C, _: &mut D) {}
87}
88
89pub(crate) fn combine_entity(x1: Entity, x2: Entity) -> Entity {
90    use Entity::*;
91
92    match (x1, x2) {
93        (Column(a), Column(b)) if a == b => Column(a),
94        (Column(a), Cell(_, b)) if a == b => Column(a),
95        (Row(a), Row(b)) if a == b => Row(a),
96        (Row(a), Cell(b, _)) if a == b => Row(a),
97        (Cell(_, a), Column(b)) if a == b => Column(a),
98        (Cell(a, _), Row(b)) if a == b => Row(a),
99        (Cell(a, b), Cell(a1, b1)) if a == a1 && b == b1 => Cell(a, b),
100        _ => Global,
101    }
102}