1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*!
A collection of useful inovcation macros for structs.

These provide a less-strict typed struct versions which lightens the syntax.
*/

/**
  initalize Coordinates using a range of parameters.


  ## Examples

  These are all equal.

  ```
  # #![macro_use] use pathfinder::*;
  # fn main() {
  coordinate!();
  coordinate!(0);
  coordinate!(0, 0);
  # }
  ```

  Type conversion is performed to make invocation easier.

  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.

  ```
  # #![macro_use] use pathfinder::*;
  # fn main() {
  coordinate!(0.0);
  coordinate!(0u8, 0i128);
  # }
  ```

*/
#[macro_export]
macro_rules! coordinate {
    () => {
        coordinate!(0, 0);
    };

    ($c:expr) => {
        coordinate!($c, $c);
    };

    ($x:expr, $y:expr) => {
        Coordinate::new($x as i16, $y as i16);
    };
}

/**
  initalize Nodes using a range of parameters.

  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.

  ## Examples

  These are all equal.

  ```
  # #![macro_use] use pathfinder::*;
  # fn main() {

  node!();
  node!(coordinate!());
  node!(0, 0);
  node!("0,0", 0, 0);

  # }
  ```
*/
#[macro_export]
macro_rules! node {
    () => {
        node!(0, 0);
    };

    ($c:expr) => {
        node!($c.x, $c.y);
    };

    ($x:expr, $y:expr) => {
        node!(&format!("{},{}", $x, $y), $x, $y);
    };

    ($name:expr, $x:expr, $y:expr) => {
        Node::new($name, coordinate!($x, $y));
    };
}

/**
  initalize Groups using a range of parameters.

  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.


  ## Examples

  These are all equal.

  ```
  # #![macro_use] use pathfinder::*;
  # fn main() {

  cluster!();
  cluster!(coordinate!());
  cluster!(0, 0);
  cluster!("0,0", 0, 0);

  # }
  ```
*/
#[macro_export]
macro_rules! cluster {
    () => {
        cluster!(0, 0);
    };

    ($c:expr) => {
        cluster!($c.x, $c.y);
    };

    ($x:expr, $y:expr) => {
        cluster!(&format!("{},{}", $x, $y), $x, $y);
    };

    ($name:expr, $x:expr, $y:expr) => {
        Group::new($name, coordinate!($x, $y));
    };
}

#[cfg(test)]
mod tests {
    use super::super::*;

    #[test]
    fn node() {
        let a = node!(0, 0);
        let b = node!("0,0", 0, 0);
        let c = node!();
        let d = node!(Coordinate::new(0, 0));

        assert_eq!(a, b);
        assert_eq!(b, c);
        assert_eq!(c, d);

        let e = node!("not the same!", 0, 0);
        let f = node!(1, 0);
        let g = node!(Coordinate::new(1, 0));

        assert_ne!(a, e);
        assert_ne!(a, f);
        assert_ne!(a, g);
    }

    #[test]
    fn node_any_type() {
        let _ = node!(0u64, 0.5 as f64);
        let _ = node!(0u32, 4000);
        let _ = node!(0u16, 9u8);
        let _ = node!(0u8, 0i32);
        let _ = node!(0f64, 100);
    }

    #[test]
    fn cluster() {
        let a = cluster!(0, 0);
        let b = cluster!("0,0", 0, 0);
        let c = cluster!();
        let d = cluster!(Coordinate::new(0, 0));

        assert_eq!(a, b);
        assert_eq!(b, c);
        assert_eq!(c, d);

        let e = cluster!("not the same!", 0, 0);
        let f = cluster!(1, 0);
        let g = cluster!(Coordinate::new(1, 0));

        assert_ne!(a, e);
        assert_ne!(a, f);
        assert_ne!(a, g);
    }

    #[test]
    fn cluster_any_type() {
        let _ = cluster!(0u64, 0.5 as f64);
        let _ = cluster!(0u32, 4000);
        let _ = cluster!(0u16, 9u8);
        let _ = cluster!(0u8, 0i32);
        let _ = cluster!(0f64, 100);
    }

    #[test]
    fn coordinate() {
        let a = coordinate!(0, 0);
        let b = coordinate!(0);
        let c = coordinate!();

        assert_eq!(a, b);
        assert_eq!(b, c);

        let c = coordinate!(1, 0);

        assert_ne!(a, c);
    }

    #[test]
    fn coordinate_any_type() {
        let _ = coordinate!(0u64, 0.5 as f64);
        let _ = coordinate!(0u32, 4000);
        let _ = coordinate!(0u16, 9u8);
        let _ = coordinate!(0u8, 0i32);
        let _ = coordinate!(0f64, 100);
    }

}