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