1 | /// Border is a representation of a cells's borders (left, right, top, bottom, and the corners) |
2 | /// |
3 | /// |
4 | /// ```text |
5 | /// top border |
6 | /// | |
7 | /// V |
8 | /// corner top left ------> +_______+ <---- corner top left |
9 | /// | | |
10 | /// left border ----------> | cell | <---- right border |
11 | /// | | |
12 | /// corner bottom right --> +_______+ <---- corner bottom right |
13 | /// ^ |
14 | /// | |
15 | /// bottom border |
16 | /// ``` |
17 | #[derive (Debug, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord)] |
18 | pub struct Border<T> { |
19 | /// A character for a top. |
20 | pub top: Option<T>, |
21 | /// A character for a bottom. |
22 | pub bottom: Option<T>, |
23 | /// A character for a left. |
24 | pub left: Option<T>, |
25 | /// A character for a right. |
26 | pub right: Option<T>, |
27 | /// A character for a left top corner. |
28 | pub left_top_corner: Option<T>, |
29 | /// A character for a left bottom corner. |
30 | pub left_bottom_corner: Option<T>, |
31 | /// A character for a right top corner. |
32 | pub right_top_corner: Option<T>, |
33 | /// A character for a right bottom corner. |
34 | pub right_bottom_corner: Option<T>, |
35 | } |
36 | |
37 | impl<T> Border<T> { |
38 | /// This function constructs a cell borders with all sides set. |
39 | #[allow (clippy::too_many_arguments)] |
40 | pub const fn full( |
41 | top: T, |
42 | bottom: T, |
43 | left: T, |
44 | right: T, |
45 | top_left: T, |
46 | top_right: T, |
47 | bottom_left: T, |
48 | bottom_right: T, |
49 | ) -> Self { |
50 | Self { |
51 | top: Some(top), |
52 | bottom: Some(bottom), |
53 | right: Some(right), |
54 | right_top_corner: Some(top_right), |
55 | right_bottom_corner: Some(bottom_right), |
56 | left: Some(left), |
57 | left_bottom_corner: Some(bottom_left), |
58 | left_top_corner: Some(top_left), |
59 | } |
60 | } |
61 | |
62 | /// Checks whether any side is set. |
63 | pub const fn is_empty(&self) -> bool { |
64 | self.top.is_none() |
65 | && self.left_top_corner.is_none() |
66 | && self.right_top_corner.is_none() |
67 | && self.bottom.is_none() |
68 | && self.left_bottom_corner.is_none() |
69 | && self.left_top_corner.is_none() |
70 | && self.left.is_none() |
71 | && self.right.is_none() |
72 | } |
73 | |
74 | /// Verifies whether anything is set on the top. |
75 | pub const fn has_top(&self) -> bool { |
76 | self.top.is_some() || self.left_top_corner.is_some() || self.right_top_corner.is_some() |
77 | } |
78 | |
79 | /// Verifies whether anything is set on the bottom. |
80 | pub const fn has_bottom(&self) -> bool { |
81 | self.bottom.is_some() |
82 | || self.left_bottom_corner.is_some() |
83 | || self.right_bottom_corner.is_some() |
84 | } |
85 | |
86 | /// Verifies whether anything is set on the left. |
87 | pub const fn has_left(&self) -> bool { |
88 | self.left.is_some() || self.left_top_corner.is_some() || self.left_bottom_corner.is_some() |
89 | } |
90 | |
91 | /// Verifies whether anything is set on the right. |
92 | pub const fn has_right(&self) -> bool { |
93 | self.right.is_some() |
94 | || self.right_top_corner.is_some() |
95 | || self.right_bottom_corner.is_some() |
96 | } |
97 | } |
98 | |
99 | impl<T: Copy> Border<T> { |
100 | /// This function constructs a cell borders with all sides's char set to a given character. |
101 | /// |
102 | /// It behaves like [`Border::full`] with the same character set to each side. |
103 | pub fn filled(c: T) -> Self { |
104 | Self::full(top:c, bottom:c, left:c, right:c, top_left:c, top_right:c, bottom_left:c, bottom_right:c) |
105 | } |
106 | } |
107 | |
108 | impl<T: Copy> Border<&T> { |
109 | /// This function constructs a cell borders with all sides's char set to a given character. |
110 | /// |
111 | /// It behaves like [`Border::full`] with the same character set to each side. |
112 | pub fn copied(&self) -> Border<T> { |
113 | Border { |
114 | top: self.top.copied(), |
115 | bottom: self.bottom.copied(), |
116 | left: self.left.copied(), |
117 | right: self.right.copied(), |
118 | left_bottom_corner: self.left_bottom_corner.copied(), |
119 | left_top_corner: self.left_top_corner.copied(), |
120 | right_bottom_corner: self.right_bottom_corner.copied(), |
121 | right_top_corner: self.right_top_corner.copied(), |
122 | } |
123 | } |
124 | } |
125 | |
126 | impl<T: Clone> Border<&T> { |
127 | /// This function constructs a cell borders with all sides's char set to a given character. |
128 | /// |
129 | /// It behaves like [`Border::full`] with the same character set to each side. |
130 | pub fn cloned(&self) -> Border<T> { |
131 | Border { |
132 | top: self.top.cloned(), |
133 | bottom: self.bottom.cloned(), |
134 | left: self.left.cloned(), |
135 | right: self.right.cloned(), |
136 | left_bottom_corner: self.left_bottom_corner.cloned(), |
137 | left_top_corner: self.left_top_corner.cloned(), |
138 | right_bottom_corner: self.right_bottom_corner.cloned(), |
139 | right_top_corner: self.right_top_corner.cloned(), |
140 | } |
141 | } |
142 | } |
143 | |