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
// Copyright 2016 Mark Sta Ana.
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0>, at your option.
// This file may not be copied, modified, or distributed except
// according to those terms.

//! A collection of Geodetical functions
//!
//! # Usage
//!
//! This crate is [on crates.io](https://crates.io/crates/geezy) and can be
//! used by adding `wordsworth` to the dependencies in your project's `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! geezy = "0.1.*"
//! ```
//!
//! and this to your crate root:
//!
//! ```rust
//! extern crate geezy;
//! ```
//!
//! # Example
//!
//! ```rust
//! use geezy;
//! assert_eq!(true, geezy::Coords::is_valid(-90_f64, -180_f64));
//! ```
//!

#[derive(Debug,PartialEq)]
#[allow(dead_code)]
pub struct Coords {
    latitude: f64,
    longtitude: f64,
}


impl Coords {
    /// Returns a Result<Coords,String> that has been validated
    pub fn new(latitude: f64, longtitude: f64) -> Result<Coords, String> {
        if !Self::is_valid(latitude, longtitude) {
            return Err("Invalid coordinates!".to_string());
        }

        Ok(Coords {
            latitude: latitude,
            longtitude: longtitude,
        })
    }

    // Returns a bool based on the validality of the latitude and longtitude coordinates give.
    pub fn is_valid(latitude: f64, longtitude: f64) -> bool {
        (latitude >= -90f64 && latitude <= 90f64) && (longtitude >= -180f64 && longtitude <= 180f64)
    }
}

#[test]
fn is_valid_coords() {
    let result = Coords::new(-90_f64, -180_f64).unwrap();
    let expected = Coords {
        latitude: -90_f64,
        longtitude: -180_f64,
    };
    assert_eq!(expected, result);

}
#[test]
fn boundary_tests() {
    // FIXME: Expand this, better still quickcheck it
    assert_eq!(true, Coords::is_valid(-90_f64, -180_f64));
    assert_eq!(true, Coords::is_valid(-90_f64, 180_f64));
    assert_eq!(true, Coords::is_valid(90_f64, 180_f64));
    assert_eq!(true, Coords::is_valid(90_f64, -180_f64));


    assert_eq!(false, Coords::is_valid(-91_f64, -180_f64));
    assert_eq!(false, Coords::is_valid(91_f64, 180_f64));

    assert_eq!(false, Coords::is_valid(-90.99, -180f64));
}