Skip to main content

photon_ui/layout/
margin.rs

1use std::fmt;
2
3/// Spacing around a rectangular area.
4#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
5pub struct Margin {
6    /// Horizontal margin (left and right) in columns.
7    pub horizontal: u16,
8    /// Vertical margin (top and bottom) in rows.
9    pub vertical: u16,
10}
11
12impl Margin {
13    /// Create a new margin with the given horizontal and vertical values.
14    pub const fn new(horizontal: u16, vertical: u16) -> Self {
15        Self {
16            horizontal,
17            vertical,
18        }
19    }
20}
21
22impl From<u16> for Margin {
23    fn from(value: u16) -> Self {
24        Self::new(value, value)
25    }
26}
27
28impl fmt::Display for Margin {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(f, "{}x{}", self.horizontal, self.vertical)
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn margin_new() {
40        let m = Margin::new(2, 1);
41        assert_eq!(m.horizontal, 2);
42        assert_eq!(m.vertical, 1);
43    }
44
45    #[test]
46    fn margin_from_u16() {
47        let m: Margin = 5.into();
48        assert_eq!(m, Margin::new(5, 5));
49    }
50
51    #[test]
52    fn margin_display() {
53        assert_eq!(Margin::new(2, 1).to_string(), "2x1");
54    }
55}