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    pub horizontal: u16,
7    pub vertical: u16,
8}
9
10impl Margin {
11    pub const fn new(horizontal: u16, vertical: u16) -> Self {
12        Self {
13            horizontal,
14            vertical,
15        }
16    }
17}
18
19impl From<u16> for Margin {
20    fn from(value: u16) -> Self {
21        Self::new(value, value)
22    }
23}
24
25impl fmt::Display for Margin {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(f, "{}x{}", self.horizontal, self.vertical)
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn margin_new() {
37        let m = Margin::new(2, 1);
38        assert_eq!(m.horizontal, 2);
39        assert_eq!(m.vertical, 1);
40    }
41
42    #[test]
43    fn margin_from_u16() {
44        let m: Margin = 5.into();
45        assert_eq!(m, Margin::new(5, 5));
46    }
47
48    #[test]
49    fn margin_display() {
50        assert_eq!(Margin::new(2, 1).to_string(), "2x1");
51    }
52}