papergrid/config/
border.rs

1/// Border is a representation of a cells's borders (left, right, top, bottom, and the corners)
2#[derive(Debug, Clone, Default, Eq, PartialEq)]
3pub struct Border<T = char> {
4    /// A character for a top.
5    pub top: Option<T>,
6    /// A character for a bottom.
7    pub bottom: Option<T>,
8    /// A character for a left.
9    pub left: Option<T>,
10    /// A character for a right.
11    pub right: Option<T>,
12    /// A character for a left top corner.
13    pub left_top_corner: Option<T>,
14    /// A character for a left bottom corner.
15    pub left_bottom_corner: Option<T>,
16    /// A character for a right top corner.
17    pub right_top_corner: Option<T>,
18    /// A character for a right bottom corner.
19    pub right_bottom_corner: Option<T>,
20}
21
22impl<T> Border<T> {
23    /// This function constructs a cell borders with all sides set.
24    #[allow(clippy::too_many_arguments)]
25    pub fn full(
26        top: T,
27        bottom: T,
28        left: T,
29        right: T,
30        top_left: T,
31        top_right: T,
32        bottom_left: T,
33        bottom_right: T,
34    ) -> Self {
35        Self {
36            top: Some(top),
37            bottom: Some(bottom),
38            right: Some(right),
39            right_top_corner: Some(top_right),
40            right_bottom_corner: Some(bottom_right),
41            left: Some(left),
42            left_bottom_corner: Some(bottom_left),
43            left_top_corner: Some(top_left),
44        }
45    }
46
47    /// Checks whether any side is set.
48    pub fn is_empty(&self) -> bool {
49        self.top.is_none()
50            && self.left_top_corner.is_none()
51            && self.right_top_corner.is_none()
52            && self.bottom.is_none()
53            && self.left_bottom_corner.is_none()
54            && self.left_top_corner.is_none()
55            && self.left.is_none()
56            && self.right.is_none()
57    }
58}
59
60impl<T: Copy> Border<T> {
61    /// This function constructs a cell borders with all sides's char set to a given character.
62    ///
63    /// It behaives like [`Border::full`] with the same character set to each side.
64    pub fn filled(c: T) -> Self {
65        Self::full(c, c, c, c, c, c, c, c)
66    }
67}
68
69impl<T: Copy> Border<&T> {
70    /// This function constructs a cell borders with all sides's char set to a given character.
71    ///
72    /// It behaives like [`Border::full`] with the same character set to each side.
73    pub fn copied(&self) -> Border<T> {
74        Border {
75            top: self.top.copied(),
76            bottom: self.bottom.copied(),
77            left: self.left.copied(),
78            right: self.right.copied(),
79            left_bottom_corner: self.left_bottom_corner.copied(),
80            left_top_corner: self.left_top_corner.copied(),
81            right_bottom_corner: self.right_bottom_corner.copied(),
82            right_top_corner: self.right_top_corner.copied(),
83        }
84    }
85}
86
87impl<T: Clone> Border<&T> {
88    /// This function constructs a cell borders with all sides's char set to a given character.
89    ///
90    /// It behaives like [`Border::full`] with the same character set to each side.
91    pub fn cloned(&self) -> Border<T> {
92        Border {
93            top: self.top.cloned(),
94            bottom: self.bottom.cloned(),
95            left: self.left.cloned(),
96            right: self.right.cloned(),
97            left_bottom_corner: self.left_bottom_corner.cloned(),
98            left_top_corner: self.left_top_corner.cloned(),
99            right_bottom_corner: self.right_bottom_corner.cloned(),
100            right_top_corner: self.right_top_corner.cloned(),
101        }
102    }
103}