use crate::types::Px;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Spacing {
pub top: Px,
pub right: Px,
pub bottom: Px,
pub left: Px,
}
impl Spacing {
pub fn all(value: Px) -> Self {
Spacing {
top: value,
right: value,
bottom: value,
left: value,
}
}
pub fn xy(vertical: Px, horizontal: Px) -> Self {
Spacing {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
pub fn new(top: Px, right: Px, bottom: Px, left: Px) -> Self {
Spacing {
top,
right,
bottom,
left,
}
}
pub fn zero() -> Self {
Spacing::all(Px(0))
}
}
impl fmt::Display for Spacing {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.top == self.right && self.right == self.bottom && self.bottom == self.left {
write!(f, "{}", self.top)
} else if self.top == self.bottom && self.left == self.right {
write!(f, "{} {}", self.top, self.right)
} else {
write!(
f,
"{} {} {} {}",
self.top, self.right, self.bottom, self.left
)
}
}
}
impl Default for Spacing {
fn default() -> Self {
Spacing::zero()
}
}