Skip to main content

pathtracer/macros/
mod.rs

1/*!
2A collection of useful inovcation macros for structs.
3
4These provide a less-strict typed struct versions which lightens the syntax.
5*/
6
7/**
8  initalize Coordinates using a range of parameters.
9
10
11  ## Examples
12
13  These are all equal.
14
15  ```
16  # #![macro_use] use pathtracer::*;
17  # fn main() {
18  coordinate!();
19  coordinate!(0);
20  coordinate!(0, 0);
21  # }
22  ```
23
24  Type conversion is performed to make invocation easier.
25
26  If you are not sure if the value you pass can be converted to a i16, do not use this macro as values could overflow.
27
28  ```
29  # #![macro_use] use pathtracer::*;
30  # fn main() {
31  coordinate!(0.0);
32  coordinate!(0u8, 0i128);
33  # }
34  ```
35
36*/
37#[macro_export]
38macro_rules! coordinate {
39    () => {
40        coordinate!(0, 0);
41    };
42
43    ($c:expr) => {
44        coordinate!($c, $c);
45    };
46
47    ($x:expr, $y:expr) => {
48        Coordinate::new($x as i16, $y as i16);
49    };
50}
51
52/**
53  initalize Nodes using a range of parameters.
54
55  Since the macro calls coordinate! it allows for type conversion, be aware that if your values can not be cast to i16. it is better to avoid these macro invocations.
56
57  ## Examples
58
59  These are all equal.
60
61  ```
62  # #![macro_use] use pathtracer::*;
63  # fn main() {
64
65  node!();
66  node!(coordinate!());
67  node!(0, 0);
68  node!("0,0", 0, 0);
69
70  # }
71  ```
72*/
73#[macro_export]
74macro_rules! node {
75    () => {
76        node!(0, 0);
77    };
78
79    ($c:expr) => {
80        node!($c.x, $c.y);
81    };
82
83    ($x:expr, $y:expr) => {
84        node!(&format!("{},{}", $x, $y), $x, $y);
85    };
86
87    ($name:expr, $x:expr, $y:expr) => {
88        Node::new($name, coordinate!($x, $y));
89    };
90}
91
92/**
93  initalize Groups using a range of parameters.
94
95  Since the macro calls coordinate! it allows for type conversion, be aware that if your values can not be cast to i16. it is better to avoid these macro invocations.
96
97
98  ## Examples
99
100  These are all equal.
101
102  ```
103  # #![macro_use] use pathtracer::*;
104  # fn main() {
105
106  cluster!();
107  cluster!(coordinate!());
108  cluster!(0, 0);
109  cluster!("0,0", 0, 0);
110
111  # }
112  ```
113*/
114#[macro_export]
115macro_rules! cluster {
116    () => {
117        cluster!(0, 0);
118    };
119
120    ($c:expr) => {
121        cluster!($c.x, $c.y);
122    };
123
124    ($x:expr, $y:expr) => {
125        cluster!(&format!("{},{}", $x, $y), $x, $y);
126    };
127
128    ($name:expr, $x:expr, $y:expr) => {
129        Group::new($name, coordinate!($x, $y));
130    };
131}
132
133#[cfg(test)]
134mod tests {
135    use super::super::*;
136
137    #[test]
138    fn node() {
139        let a = node!(0, 0);
140        let b = node!("0,0", 0, 0);
141        let c = node!();
142        let d = node!(Coordinate::new(0, 0));
143
144        assert_eq!(a, b);
145        assert_eq!(b, c);
146        assert_eq!(c, d);
147
148        let e = node!("not the same!", 0, 0);
149        let f = node!(1, 0);
150        let g = node!(Coordinate::new(1, 0));
151
152        assert_ne!(a, e);
153        assert_ne!(a, f);
154        assert_ne!(a, g);
155    }
156
157    #[test]
158    fn node_any_type() {
159        let _ = node!(0u64, 0.5 as f64);
160        let _ = node!(0u32, 4000);
161        let _ = node!(0u16, 9u8);
162        let _ = node!(0u8, 0i32);
163        let _ = node!(0f64, 100);
164    }
165
166    #[test]
167    fn cluster() {
168        let a = cluster!(0, 0);
169        let b = cluster!("0,0", 0, 0);
170        let c = cluster!();
171        let d = cluster!(Coordinate::new(0, 0));
172
173        assert_eq!(a, b);
174        assert_eq!(b, c);
175        assert_eq!(c, d);
176
177        let e = cluster!("not the same!", 0, 0);
178        let f = cluster!(1, 0);
179        let g = cluster!(Coordinate::new(1, 0));
180
181        assert_ne!(a, e);
182        assert_ne!(a, f);
183        assert_ne!(a, g);
184    }
185
186    #[test]
187    fn cluster_any_type() {
188        let _ = cluster!(0u64, 0.5 as f64);
189        let _ = cluster!(0u32, 4000);
190        let _ = cluster!(0u16, 9u8);
191        let _ = cluster!(0u8, 0i32);
192        let _ = cluster!(0f64, 100);
193    }
194
195    #[test]
196    fn coordinate() {
197        let a = coordinate!(0, 0);
198        let b = coordinate!(0);
199        let c = coordinate!();
200
201        assert_eq!(a, b);
202        assert_eq!(b, c);
203
204        let c = coordinate!(1, 0);
205
206        assert_ne!(a, c);
207    }
208
209    #[test]
210    fn coordinate_any_type() {
211        let _ = coordinate!(0u64, 0.5 as f64);
212        let _ = coordinate!(0u32, 4000);
213        let _ = coordinate!(0u16, 9u8);
214        let _ = coordinate!(0u8, 0i32);
215        let _ = coordinate!(0f64, 100);
216    }
217}