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
//! Tools and data for coordinate reference systems from the [EPSG Geodetic Parameter Dataset](https://en.wikipedia.org/wiki/EPSG_Geodetic_Parameter_Dataset).
pub mod references;
use crate::references::get_crs;
use std::convert::TryFrom;

/// A coordinate reference system
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CRS {
    pub coord_ref_sys_code: i32,
    pub coord_ref_sys_name: &'static str,
    pub coord_ref_sys_kind: &'static str,
    pub coord_sys_code: i32,
    pub datum_code: i32,
    pub base_crs_code: i32,
    pub remarks: &'static str,
    pub information_source: &'static str,
    pub data_source: &'static str,
    pub revision_date: &'static str,
    pub deprecated: i16,
}

impl CRS {
    /// Is this CRS deprecated? Convenience function for `deprecated` field.
    pub fn deprecated(&self) -> bool { 
        self.deprecated == 1
    }

}

impl TryFrom<String> for CRS {
    type Error = &'static str;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        get_crs(&value).map(|x| x.to_owned()).ok_or("No such CRS")
    }
}

#[cfg(test)]
mod test {
    use crate::references::get_crs;

    #[test]
    fn test_deprecated() {
        let crs = get_crs("EPSG:2155").unwrap();
        assert!(crs.deprecated());
    }
}