Skip to main content

kcl_api/point/
only.rs

1use super::Point2d;
2use super::Point3d;
3use super::Point4d;
4
5macro_rules! impl_only {
6    ($typ:ident, $method:ident, $component:ident, $($i:ident),*) => {
7        impl<T> $typ<T>
8        where
9            T: Default,
10        {
11            #[doc = concat!("Set the `", stringify!($component), "` component to the given value, and all other components to their default.\n")]
12            #[doc = "```\n"]
13            #[doc = concat!("use kcl_api::point::", stringify!($typ), ";")]
14            #[doc = concat!("let expected = ", stringify!($typ), "{")]
15            #[doc = concat!("\t", stringify!($component), ": 8,")]
16                    $(
17            #[doc = concat!("\t", stringify!($i), ": 0,")]
18                    )*
19            #[doc = "};"]
20            #[doc = concat!("let actual = ", stringify!($typ), "::only_", stringify!($component), "(8);")]
21            #[doc = "assert_eq!(actual, expected);"]
22            #[doc = "```\n"]
23            pub fn $method($component: T) -> Self {
24                Self {
25                    $component,
26                    $(
27                        $i: Default::default(),
28                    )*
29                }
30            }
31        }
32    };
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_all() {
41        assert_eq!(Point2d::only_x(1), Point2d { x: 1, y: 0 });
42        assert_eq!(Point2d::only_y(1), Point2d { x: 0, y: 1 });
43
44        assert_eq!(Point3d::only_x(1), Point3d { x: 1, y: 0, z: 0 });
45        assert_eq!(Point3d::only_y(1), Point3d { x: 0, y: 1, z: 0 });
46        assert_eq!(Point3d::only_z(1), Point3d { x: 0, y: 0, z: 1 });
47
48        assert_eq!(Point4d::only_x(1), Point4d { x: 1, y: 0, z: 0, w: 0 });
49        assert_eq!(Point4d::only_y(1), Point4d { x: 0, y: 1, z: 0, w: 0 });
50        assert_eq!(Point4d::only_z(1), Point4d { x: 0, y: 0, z: 1, w: 0 });
51        assert_eq!(Point4d::only_w(1), Point4d { x: 0, y: 0, z: 0, w: 1 });
52    }
53}
54
55impl_only!(Point2d, only_x, x, y);
56impl_only!(Point2d, only_y, y, x);
57impl_only!(Point3d, only_x, x, y, z);
58impl_only!(Point3d, only_y, y, x, z);
59impl_only!(Point3d, only_z, z, x, y);
60impl_only!(Point4d, only_x, x, y, z, w);
61impl_only!(Point4d, only_y, y, x, z, w);
62impl_only!(Point4d, only_z, z, x, y, w);
63impl_only!(Point4d, only_w, w, x, y, z);