Skip to main content

haystack_core/kinds/
coord.rs

1use std::fmt;
2
3/// Geographic coordinate (latitude/longitude in decimal degrees).
4/// Zinc: `C(37.55,-77.45)`
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct Coord {
7    pub lat: f64,
8    pub lng: f64,
9}
10
11impl Coord {
12    pub fn new(lat: f64, lng: f64) -> Self {
13        Self { lat, lng }
14    }
15}
16
17impl fmt::Display for Coord {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "C({},{})", self.lat, self.lng)
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn coord_display() {
29        let c = Coord::new(37.5458266, -77.4491888);
30        assert_eq!(c.to_string(), "C(37.5458266,-77.4491888)");
31    }
32
33    #[test]
34    fn coord_equality() {
35        assert_eq!(Coord::new(37.0, -77.0), Coord::new(37.0, -77.0));
36        assert_ne!(Coord::new(37.0, -77.0), Coord::new(37.0, -78.0));
37    }
38
39    #[test]
40    fn coord_is_copy() {
41        let c = Coord::new(1.0, 2.0);
42        let c2 = c;
43        assert_eq!(c, c2);
44    }
45}