1#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
26pub struct Padding {
27 pub left: u16,
29 pub right: u16,
31 pub top: u16,
33 pub bottom: u16,
35}
36
37impl Padding {
38 pub const ZERO: Self = Self {
40 left: 0,
41 right: 0,
42 top: 0,
43 bottom: 0,
44 };
45
46 pub const fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
50 Self {
51 left,
52 right,
53 top,
54 bottom,
55 }
56 }
57
58 #[deprecated = "use Padding::ZERO"]
60 pub const fn zero() -> Self {
61 Self::ZERO
62 }
63
64 pub const fn horizontal(value: u16) -> Self {
66 Self {
67 left: value,
68 right: value,
69 top: 0,
70 bottom: 0,
71 }
72 }
73
74 pub const fn vertical(value: u16) -> Self {
76 Self {
77 left: 0,
78 right: 0,
79 top: value,
80 bottom: value,
81 }
82 }
83
84 pub const fn uniform(value: u16) -> Self {
86 Self {
87 left: value,
88 right: value,
89 top: value,
90 bottom: value,
91 }
92 }
93
94 pub const fn proportional(value: u16) -> Self {
99 Self {
100 left: 2 * value,
101 right: 2 * value,
102 top: value,
103 bottom: value,
104 }
105 }
106
107 pub const fn symmetric(x: u16, y: u16) -> Self {
112 Self {
113 left: x,
114 right: x,
115 top: y,
116 bottom: y,
117 }
118 }
119
120 pub const fn left(value: u16) -> Self {
122 Self {
123 left: value,
124 right: 0,
125 top: 0,
126 bottom: 0,
127 }
128 }
129
130 pub const fn right(value: u16) -> Self {
132 Self {
133 left: 0,
134 right: value,
135 top: 0,
136 bottom: 0,
137 }
138 }
139
140 pub const fn top(value: u16) -> Self {
142 Self {
143 left: 0,
144 right: 0,
145 top: value,
146 bottom: 0,
147 }
148 }
149
150 pub const fn bottom(value: u16) -> Self {
152 Self {
153 left: 0,
154 right: 0,
155 top: 0,
156 bottom: value,
157 }
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164
165 #[test]
166 fn new() {
167 assert_eq!(
168 Padding::new(1, 2, 3, 4),
169 Padding {
170 left: 1,
171 right: 2,
172 top: 3,
173 bottom: 4
174 }
175 );
176 }
177
178 #[test]
179 fn constructors() {
180 assert_eq!(Padding::horizontal(1), Padding::new(1, 1, 0, 0));
181 assert_eq!(Padding::vertical(1), Padding::new(0, 0, 1, 1));
182 assert_eq!(Padding::uniform(1), Padding::new(1, 1, 1, 1));
183 assert_eq!(Padding::proportional(1), Padding::new(2, 2, 1, 1));
184 assert_eq!(Padding::symmetric(1, 2), Padding::new(1, 1, 2, 2));
185 assert_eq!(Padding::left(1), Padding::new(1, 0, 0, 0));
186 assert_eq!(Padding::right(1), Padding::new(0, 1, 0, 0));
187 assert_eq!(Padding::top(1), Padding::new(0, 0, 1, 0));
188 assert_eq!(Padding::bottom(1), Padding::new(0, 0, 0, 1));
189 }
190
191 #[test]
192 const fn can_be_const() {
193 const _PADDING: Padding = Padding::new(1, 1, 1, 1);
194 const _UNI_PADDING: Padding = Padding::uniform(1);
195 const _HORIZONTAL: Padding = Padding::horizontal(1);
196 const _VERTICAL: Padding = Padding::vertical(1);
197 const _PROPORTIONAL: Padding = Padding::proportional(1);
198 const _SYMMETRIC: Padding = Padding::symmetric(1, 1);
199 const _LEFT: Padding = Padding::left(1);
200 const _RIGHT: Padding = Padding::right(1);
201 const _TOP: Padding = Padding::top(1);
202 const _BOTTOM: Padding = Padding::bottom(1);
203 }
204}