Skip to main content

telar_geometry_core/
border_radius.rs

1#[derive(Debug, Clone, Copy, PartialEq)]
2pub struct BorderRadius {
3    pub top_left: f32,
4    pub top_right: f32,
5    pub bottom_right: f32,
6    pub bottom_left: f32,
7}
8
9impl BorderRadius {
10    pub fn all(radius: f32) -> Self {
11        Self {
12            top_left: radius,
13            top_right: radius,
14            bottom_right: radius,
15            bottom_left: radius,
16        }
17    }
18
19    pub fn zero() -> Self {
20        Self::all(0.0)
21    }
22
23    pub fn is_zero(&self) -> bool {
24        self.top_left == 0.0
25            && self.top_right == 0.0
26            && self.bottom_right == 0.0
27            && self.bottom_left == 0.0
28    }
29}
30
31impl Default for BorderRadius {
32    fn default() -> Self {
33        Self::zero()
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn border_radius_all_sets_all_corners_equal() {
43        let br = BorderRadius::all(8.0);
44        assert_eq!(br.top_left, 8.0);
45        assert_eq!(br.top_right, 8.0);
46        assert_eq!(br.bottom_right, 8.0);
47        assert_eq!(br.bottom_left, 8.0);
48    }
49
50    #[test]
51    fn border_radius_zero_all_corners_are_zero() {
52        let br = BorderRadius::zero();
53        assert_eq!(br.top_left, 0.0);
54        assert_eq!(br.top_right, 0.0);
55        assert_eq!(br.bottom_right, 0.0);
56        assert_eq!(br.bottom_left, 0.0);
57    }
58
59    #[test]
60    fn border_radius_zero_is_zero() {
61        assert!(BorderRadius::zero().is_zero());
62    }
63
64    #[test]
65    fn border_radius_non_zero_is_not_zero() {
66        assert!(!BorderRadius::all(1.0).is_zero());
67    }
68
69    #[test]
70    fn border_radius_default_is_zero() {
71        assert!(BorderRadius::default().is_zero());
72    }
73
74    #[test]
75    fn border_radius_partial_non_zero_is_not_zero() {
76        let br = BorderRadius {
77            top_left: 5.0,
78            top_right: 0.0,
79            bottom_right: 0.0,
80            bottom_left: 0.0,
81        };
82        assert!(!br.is_zero());
83    }
84}