Skip to main content

photon_ui/layout/
spacing.rs

1/** Gap or overlap between layout segments. */
2#[derive(Debug, Clone, Eq, PartialEq, Hash)]
3pub enum Spacing {
4    /// Positive gap between layout segments.
5    Space(u16),
6    /// Negative overlap that causes segments to encroach on each other.
7    Overlap(u16),
8}
9
10impl Default for Spacing {
11    fn default() -> Self {
12        Self::Space(0)
13    }
14}
15
16impl From<u16> for Spacing {
17    fn from(value: u16) -> Self {
18        Self::Space(value)
19    }
20}
21
22impl From<i16> for Spacing {
23    fn from(value: i16) -> Self {
24        if value < 0 {
25            Self::Overlap(value.unsigned_abs())
26        } else {
27            Self::Space(value as u16)
28        }
29    }
30}
31
32impl From<i32> for Spacing {
33    fn from(value: i32) -> Self {
34        Self::from(value.clamp(i32::from(i16::MIN), i32::from(i16::MAX)) as i16)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn spacing_default() {
44        assert_eq!(Spacing::default(), Spacing::Space(0));
45    }
46
47    #[test]
48    fn spacing_from_u16() {
49        assert_eq!(Spacing::from(5_u16), Spacing::Space(5));
50    }
51
52    #[test]
53    fn spacing_from_i16_positive() {
54        assert_eq!(Spacing::from(3_i16), Spacing::Space(3));
55    }
56
57    #[test]
58    fn spacing_from_i16_negative() {
59        assert_eq!(Spacing::from(-2_i16), Spacing::Overlap(2));
60    }
61
62    #[test]
63    fn spacing_from_i32_clamped() {
64        assert_eq!(Spacing::from(i32::MAX), Spacing::Space(i16::MAX as u16));
65        assert_eq!(
66            Spacing::from(i32::MIN),
67            Spacing::Overlap(i16::MAX as u16 + 1)
68        );
69    }
70
71    #[test]
72    fn spacing_from_i32_zero() {
73        assert_eq!(Spacing::from(0_i32), Spacing::Space(0));
74    }
75}