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
use ParseError;

/// 
/// Holds a pair for latitude and longitude coordinates
///
/// # Examples
/// ```
/// use std::error::Error;
///
/// extern crate geomorph;
///
/// fn try_main() -> Result<geomorph::coord::Coord, geomorph::ParseError> {
///     let lat: f64 = -23.0095839;
///     let lon: f64 = -43.4361816;
///     
///     geomorph::coord::Coord::new(lat, lon)
/// }
///
/// fn main() {
///     let coord = try_main().unwrap();
/// }
/// ```
///
pub struct Coord {
    /// Latitude: Must be contained in the interval [-90.0..90.0]
    pub lat: f64,
    /// Longitude: Must be contained in the interval [-180.0..180.0]
    pub lon: f64,
}

impl Coord {
    pub fn new(lat: f64, lon: f64)
    -> Result<Coord, ParseError> {
        if lat < -90.0 ||
        lat > 90.0 ||
        lon < -180.0 ||
        lon > 180.0 {
            return Err(ParseError {});
        }

        Ok(Coord {
            lat,
            lon
        })
    }
}

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

    #[test]
    fn instantiate_coord() {
        let lat: f64 = -23.0095839;
        let lon: f64 = -43.4361816;
        let coord = Coord::new(lat, lon)
            .unwrap();
        assert_eq!(coord.lat, lat);
        assert_eq!(coord.lon, lon);
    }

    #[test]
    #[should_panic]
    fn lat_lower_limit() {
        Coord::new(-91.0, 0.0)
            .unwrap();
    }

    #[test]
    #[should_panic]
    fn lat_upper_limit() {
        Coord::new(91.0, 0.0)
            .unwrap();
    }

    #[test]
    #[should_panic]
    fn lon_lower_limit() {
        Coord::new(0.0, -181.0)
            .unwrap();
    }

    #[test]
    #[should_panic]
    fn lon_upper_limit() {
        Coord::new(0.0, 181.0)
            .unwrap();
    }
}