papergrid/config/
borders.rs

1/// Borders represents a Table frame with horizontal and vertical split lines.
2#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3pub struct Borders<T> {
4    /// A top horizontal on the frame.
5    pub top: Option<T>,
6    /// A top left on the frame.
7    pub top_left: Option<T>,
8    /// A top right on the frame.
9    pub top_right: Option<T>,
10    /// A top horizontal intersection on the frame.
11    pub top_intersection: Option<T>,
12
13    /// A bottom horizontal on the frame.
14    pub bottom: Option<T>,
15    /// A bottom left on the frame.
16    pub bottom_left: Option<T>,
17    /// A bottom right on the frame.
18    pub bottom_right: Option<T>,
19    /// A bottom horizontal intersection on the frame.
20    pub bottom_intersection: Option<T>,
21
22    /// A horizontal split.
23    pub horizontal: Option<T>,
24    /// A vertical split.
25    pub vertical: Option<T>,
26    /// A top left character on the frame.
27    pub intersection: Option<T>,
28
29    /// A vertical split on the left frame line.
30    pub left: Option<T>,
31    /// A horizontal split on the left frame line.
32    pub left_intersection: Option<T>,
33
34    /// A vertical split on the right frame line.
35    pub right: Option<T>,
36    /// A horizontal split on the right frame line.
37    pub right_intersection: Option<T>,
38}
39
40impl<T> Borders<T> {
41    /// Returns empty borders.
42    pub const fn empty() -> Self {
43        Self {
44            top: None,
45            top_left: None,
46            top_right: None,
47            top_intersection: None,
48            bottom: None,
49            bottom_left: None,
50            bottom_right: None,
51            bottom_intersection: None,
52            horizontal: None,
53            left: None,
54            right: None,
55            vertical: None,
56            left_intersection: None,
57            right_intersection: None,
58            intersection: None,
59        }
60    }
61
62    /// Returns Borders filled in with a supplied value.
63    pub const fn filled(val: T) -> Self
64    where
65        T: Copy,
66    {
67        Self {
68            top: Some(val),
69            top_left: Some(val),
70            top_right: Some(val),
71            top_intersection: Some(val),
72            bottom: Some(val),
73            bottom_left: Some(val),
74            bottom_right: Some(val),
75            bottom_intersection: Some(val),
76            horizontal: Some(val),
77            left: Some(val),
78            right: Some(val),
79            vertical: Some(val),
80            left_intersection: Some(val),
81            right_intersection: Some(val),
82            intersection: Some(val),
83        }
84    }
85
86    /// A verification whether any border was set.
87    pub const fn is_empty(&self) -> bool {
88        self.top.is_none()
89            && self.top_left.is_none()
90            && self.top_right.is_none()
91            && self.top_intersection.is_none()
92            && self.bottom.is_none()
93            && self.bottom_left.is_none()
94            && self.bottom_right.is_none()
95            && self.bottom_intersection.is_none()
96            && self.horizontal.is_none()
97            && self.left.is_none()
98            && self.right.is_none()
99            && self.vertical.is_none()
100            && self.left_intersection.is_none()
101            && self.right_intersection.is_none()
102            && self.intersection.is_none()
103    }
104
105    /// Verifies if borders has left line set on the frame.
106    pub const fn has_left(&self) -> bool {
107        self.left.is_some()
108            || self.left_intersection.is_some()
109            || self.top_left.is_some()
110            || self.bottom_left.is_some()
111    }
112
113    /// Verifies if borders has right line set on the frame.
114    pub const fn has_right(&self) -> bool {
115        self.right.is_some()
116            || self.right_intersection.is_some()
117            || self.top_right.is_some()
118            || self.bottom_right.is_some()
119    }
120
121    /// Verifies if borders has top line set on the frame.
122    pub const fn has_top(&self) -> bool {
123        self.top.is_some()
124            || self.top_intersection.is_some()
125            || self.top_left.is_some()
126            || self.top_right.is_some()
127    }
128
129    /// Verifies if borders has bottom line set on the frame.
130    pub const fn has_bottom(&self) -> bool {
131        self.bottom.is_some()
132            || self.bottom_intersection.is_some()
133            || self.bottom_left.is_some()
134            || self.bottom_right.is_some()
135    }
136
137    /// Verifies if borders has horizontal lines set.
138    pub const fn has_horizontal(&self) -> bool {
139        self.horizontal.is_some()
140            || self.left_intersection.is_some()
141            || self.right_intersection.is_some()
142            || self.intersection.is_some()
143    }
144
145    /// Verifies if borders has vertical lines set.
146    pub const fn has_vertical(&self) -> bool {
147        self.intersection.is_some()
148            || self.vertical.is_some()
149            || self.top_intersection.is_some()
150            || self.bottom_intersection.is_some()
151    }
152
153    /// Converts borders type into another one.
154    pub fn convert_into<T1>(self) -> Borders<T1>
155    where
156        T1: From<T>,
157    {
158        Borders {
159            left: self.left.map(Into::into),
160            right: self.right.map(Into::into),
161            top: self.top.map(Into::into),
162            bottom: self.bottom.map(Into::into),
163            bottom_intersection: self.bottom_intersection.map(Into::into),
164            bottom_left: self.bottom_left.map(Into::into),
165            bottom_right: self.bottom_right.map(Into::into),
166            horizontal: self.horizontal.map(Into::into),
167            intersection: self.intersection.map(Into::into),
168            left_intersection: self.left_intersection.map(Into::into),
169            right_intersection: self.right_intersection.map(Into::into),
170            top_intersection: self.top_intersection.map(Into::into),
171            top_left: self.top_left.map(Into::into),
172            top_right: self.top_right.map(Into::into),
173            vertical: self.vertical.map(Into::into),
174        }
175    }
176
177    /// Converts borders with a given function.
178    pub fn map<F, T1>(self, f: F) -> Borders<T1>
179    where
180        F: Fn(T) -> T1,
181    {
182        Borders {
183            left: self.left.map(&f),
184            right: self.right.map(&f),
185            top: self.top.map(&f),
186            bottom: self.bottom.map(&f),
187            bottom_intersection: self.bottom_intersection.map(&f),
188            bottom_left: self.bottom_left.map(&f),
189            bottom_right: self.bottom_right.map(&f),
190            horizontal: self.horizontal.map(&f),
191            intersection: self.intersection.map(&f),
192            left_intersection: self.left_intersection.map(&f),
193            right_intersection: self.right_intersection.map(&f),
194            top_intersection: self.top_intersection.map(&f),
195            top_left: self.top_left.map(&f),
196            top_right: self.top_right.map(&f),
197            vertical: self.vertical.map(&f),
198        }
199    }
200}