#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Padding {
pub left: u16,
pub right: u16,
pub top: u16,
pub bottom: u16,
}
impl Padding {
pub const ZERO: Self = Self {
left: 0,
right: 0,
top: 0,
bottom: 0,
};
pub const fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
Self {
left,
right,
top,
bottom,
}
}
#[deprecated = "use Padding::ZERO"]
pub const fn zero() -> Self {
Self::ZERO
}
pub const fn horizontal(value: u16) -> Self {
Self {
left: value,
right: value,
top: 0,
bottom: 0,
}
}
pub const fn vertical(value: u16) -> Self {
Self {
left: 0,
right: 0,
top: value,
bottom: value,
}
}
pub const fn uniform(value: u16) -> Self {
Self {
left: value,
right: value,
top: value,
bottom: value,
}
}
pub const fn proportional(value: u16) -> Self {
Self {
left: 2 * value,
right: 2 * value,
top: value,
bottom: value,
}
}
pub const fn symmetric(x: u16, y: u16) -> Self {
Self {
left: x,
right: x,
top: y,
bottom: y,
}
}
pub const fn left(value: u16) -> Self {
Self {
left: value,
right: 0,
top: 0,
bottom: 0,
}
}
pub const fn right(value: u16) -> Self {
Self {
left: 0,
right: value,
top: 0,
bottom: 0,
}
}
pub const fn top(value: u16) -> Self {
Self {
left: 0,
right: 0,
top: value,
bottom: 0,
}
}
pub const fn bottom(value: u16) -> Self {
Self {
left: 0,
right: 0,
top: 0,
bottom: value,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new() {
assert_eq!(
Padding::new(1, 2, 3, 4),
Padding {
left: 1,
right: 2,
top: 3,
bottom: 4
}
);
}
#[test]
fn constructors() {
assert_eq!(Padding::horizontal(1), Padding::new(1, 1, 0, 0));
assert_eq!(Padding::vertical(1), Padding::new(0, 0, 1, 1));
assert_eq!(Padding::uniform(1), Padding::new(1, 1, 1, 1));
assert_eq!(Padding::proportional(1), Padding::new(2, 2, 1, 1));
assert_eq!(Padding::symmetric(1, 2), Padding::new(1, 1, 2, 2));
assert_eq!(Padding::left(1), Padding::new(1, 0, 0, 0));
assert_eq!(Padding::right(1), Padding::new(0, 1, 0, 0));
assert_eq!(Padding::top(1), Padding::new(0, 0, 1, 0));
assert_eq!(Padding::bottom(1), Padding::new(0, 0, 0, 1));
}
#[test]
const fn can_be_const() {
const _PADDING: Padding = Padding::new(1, 1, 1, 1);
const _UNI_PADDING: Padding = Padding::uniform(1);
const _HORIZONTAL: Padding = Padding::horizontal(1);
const _VERTICAL: Padding = Padding::vertical(1);
const _PROPORTIONAL: Padding = Padding::proportional(1);
const _SYMMETRIC: Padding = Padding::symmetric(1, 1);
const _LEFT: Padding = Padding::left(1);
const _RIGHT: Padding = Padding::right(1);
const _TOP: Padding = Padding::top(1);
const _BOTTOM: Padding = Padding::bottom(1);
}
}