Skip to main content

haystack_core/kinds/
coord.rs

1use std::fmt;
2
3/// A geographic coordinate represented as latitude and longitude in decimal degrees.
4///
5/// This is the Haystack `Coord` scalar kind. Latitude must be in the range
6/// −90 to 90 and longitude in −180 to 180. Displayed in Zinc as `C(lat,lng)`.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct Coord {
9    /// Latitude in decimal degrees (−90 … 90).
10    pub lat: f64,
11    /// Longitude in decimal degrees (−180 … 180).
12    pub lng: f64,
13}
14
15impl Coord {
16    /// Creates a new `Coord` from the given latitude and longitude.
17    ///
18    /// Both values are stored as-is; callers are responsible for ensuring
19    /// that `lat` is within −90..=90 and `lng` is within −180..=180.
20    pub fn new(lat: f64, lng: f64) -> Self {
21        Self { lat, lng }
22    }
23}
24
25impl fmt::Display for Coord {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(f, "C({},{})", self.lat, self.lng)
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn coord_display() {
37        let c = Coord::new(37.5458266, -77.4491888);
38        assert_eq!(c.to_string(), "C(37.5458266,-77.4491888)");
39    }
40
41    #[test]
42    fn coord_equality() {
43        assert_eq!(Coord::new(37.0, -77.0), Coord::new(37.0, -77.0));
44        assert_ne!(Coord::new(37.0, -77.0), Coord::new(37.0, -78.0));
45    }
46
47    #[test]
48    fn coord_is_copy() {
49        let c = Coord::new(1.0, 2.0);
50        let c2 = c;
51        assert_eq!(c, c2);
52    }
53}