papergrid/config/
sides.rs

1/// A structure which represents 4 box sides.
2#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3pub struct Sides<T> {
4    /// Top side.
5    pub top: T,
6    /// Bottom side.
7    pub bottom: T,
8    /// Left side.
9    pub left: T,
10    /// Right side.
11    pub right: T,
12}
13
14impl<T> Sides<T> {
15    /// Creates a new object.
16    pub const fn new(left: T, right: T, top: T, bottom: T) -> Self {
17        Self {
18            top,
19            bottom,
20            left,
21            right,
22        }
23    }
24
25    /// Creates a new object.
26    pub const fn filled(value: T) -> Self
27    where
28        T: Copy,
29    {
30        Self::new(value, value, value, value)
31    }
32
33    /// Creates a new object.
34    pub fn convert_into<T1>(self) -> Sides<T1>
35    where
36        T: Into<T1>,
37    {
38        Sides::new(
39            self.left.into(),
40            self.right.into(),
41            self.top.into(),
42            self.bottom.into(),
43        )
44    }
45
46    /// Converts all sides with a given function.
47    pub fn map<F, T1>(self, f: F) -> Sides<T1>
48    where
49        F: Fn(T) -> T1,
50    {
51        Sides::new(
52            (f)(self.left),
53            (f)(self.right),
54            (f)(self.top),
55            (f)(self.bottom),
56        )
57    }
58
59    /// Converts all sides with a given function.
60    pub fn fold<B, F>(self, acc: B, f: F) -> B
61    where
62        F: FnMut(B, T) -> B,
63    {
64        let mut f = f;
65        let mut acc = acc;
66
67        acc = (f)(acc, self.left);
68        acc = (f)(acc, self.right);
69        acc = (f)(acc, self.top);
70        acc = (f)(acc, self.bottom);
71
72        acc
73    }
74}
75
76impl<T> Sides<Option<T>> {
77    /// Checkes whether any option was set
78    pub const fn is_empty(&self) -> bool {
79        self.left.is_none() && self.right.is_none() && self.top.is_none() && self.bottom.is_none()
80    }
81}