1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::enums::Border;
/// Defines padding struct
#[derive(Debug, PartialEq, Clone, Copy, Default, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Padding {
pub top: usize,
pub right: usize,
pub bottom: usize,
pub left: usize,
}
impl Padding {
/// Creates a [`Padding`] by specifying every field
pub const fn new(
top: usize,
right: usize,
bottom: usize,
left: usize,
) -> Self {
Self {
top,
right,
bottom,
left,
}
}
/// Creates a [`Padding`] with same the value for all fields
pub const fn uniform(value: usize) -> Self {
Self {
top: value,
right: value,
bottom: value,
left: value,
}
}
/// Creates a [`Padding`] with `vertical` value for `top` and `bottom`
/// and `horizontal` value for `left` and `right`
pub const fn symmetric(vertical: usize, horizontal: usize) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
/// Creates a [`Padding`] with the same value for `top` and `bottom` fields
pub const fn vertical(value: usize) -> Self {
Self {
top: value,
right: 0,
bottom: value,
left: 0,
}
}
/// Creates a [`Padding`] with the same value for `left` and `right` fields
pub const fn horizontal(value: usize) -> Self {
Self {
top: 0,
right: value,
bottom: 0,
left: value,
}
}
/// Creates a [`Padding`] that only sets the `top` padding
pub const fn top(value: usize) -> Self {
Self {
top: value,
right: 0,
bottom: 0,
left: 0,
}
}
/// Creates a [`Padding`] that only sets the `right` padding
pub const fn right(value: usize) -> Self {
Self {
top: 0,
right: value,
bottom: 0,
left: 0,
}
}
/// Creates a [`Padding`] that only sets the `bottom` padding
pub const fn bottom(value: usize) -> Self {
Self {
top: 0,
right: 0,
bottom: value,
left: 0,
}
}
/// Creates a [`Padding`] that only sets the `left` padding
pub const fn left(value: usize) -> Self {
Self {
top: 0,
right: 0,
bottom: 0,
left: value,
}
}
/// Gets total padding in vertical axis
pub const fn get_vertical(&self) -> usize {
self.top + self.bottom
}
/// Gets total padding in horizontal axis
pub const fn get_horizontal(&self) -> usize {
self.left + self.right
}
}
impl From<usize> for Padding {
/// Uses the value for all four sides
fn from(value: usize) -> Self {
Self {
top: value,
right: value,
bottom: value,
left: value,
}
}
}
impl From<(usize, usize)> for Padding {
/// Uses the first value for the top and bottom side,
/// second for right and left
fn from(value: (usize, usize)) -> Self {
Self {
top: value.0,
right: value.1,
bottom: value.0,
left: value.1,
}
}
}
impl From<(usize, usize, usize, usize)> for Padding {
/// Each value represent one side, starting from the top and continuing
/// clockwise
fn from(value: (usize, usize, usize, usize)) -> Self {
Self {
top: value.0,
right: value.1,
bottom: value.2,
left: value.3,
}
}
}
impl From<Border> for Padding {
fn from(value: Border) -> Self {
Self {
top: value.contains(Border::TOP) as usize,
right: value.contains(Border::RIGHT) as usize,
bottom: value.contains(Border::BOTTOM) as usize,
left: value.contains(Border::LEFT) as usize,
}
}
}