~matthiasbeyer/imag

ref: 6bc08b60e6e3a99cc9a263713db33061d63dc964 imag/lib/entry/libimagentrygps/src/types.rs -rw-r--r-- 4.7 KiB
6bc08b60Matthias Beyer Merge branch 'update-deps' into master 11 months ago
                                                                                                    
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use toml::Value;
use toml::map::Map;
use anyhow::Result;

use anyhow::Error;

use libimagerror::errors::Error as EM;

pub trait FromValue : Sized {
    fn from_value(v: &Value) -> Result<Self>;
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct GPSValue {
    pub degree:  i64,
    pub minutes: i64,
    pub seconds: i64,
}

impl GPSValue {

    pub fn new(d: i64, m: i64, s: i64) -> GPSValue {
        GPSValue {
            degree:  d,
            minutes: m,
            seconds: s
        }
    }

    pub fn degree(&self) -> i64 {
        self.degree
    }

    pub fn minutes(&self) -> i64 {
        self.minutes
    }

    pub fn seconds(&self) -> i64 {
        self.seconds
    }

}


impl Into<Value> for GPSValue {

    fn into(self) -> Value {
        let mut map = Map::new();
        let _ = map.insert("degree".to_owned(),  Value::Integer(self.degree));
        let _ = map.insert("minutes".to_owned(), Value::Integer(self.minutes));
        let _ = map.insert("seconds".to_owned(), Value::Integer(self.seconds));
        Value::Table(map)
    }

}

impl FromValue for GPSValue {
    fn from_value(v: &Value) -> Result<Self> {
        let int_to_appropriate_width = |v: &Value| {
            v.as_integer()
             .ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
        };

        match *v {
            Value::Table(ref map) => {
                Ok(GPSValue::new(
                    map.get("degree")
                        .ok_or_else(|| anyhow!("Degree missing"))
                        .and_then(&int_to_appropriate_width)?,

                    map
                        .get("minutes")
                        .ok_or_else(|| anyhow!("Minutes missing"))
                        .and_then(&int_to_appropriate_width)?,

                    map
                        .get("seconds")
                        .ok_or_else(|| anyhow!("Seconds missing"))
                        .and_then(&int_to_appropriate_width)?
                ))
            }
            _ => Err(Error::from(EM::EntryHeaderTypeError))
        }
    }

}

impl Display for GPSValue {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{}° {}\" {}'", self.degree, self.minutes, self.seconds)
    }
}

/// Data-transfer type for transfering longitude-latitude-pairs
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Coordinates {
    pub longitude: GPSValue,
    pub latitude:  GPSValue,
}

impl Coordinates {
    pub fn new(long: GPSValue, lat: GPSValue) -> Coordinates {
        Coordinates {
            longitude: long,
            latitude:  lat,
        }
    }

    pub fn longitude(&self) -> &GPSValue {
        &self.longitude
    }

    pub fn latitude(&self) -> &GPSValue {
        &self.latitude
    }
}

impl Into<Value> for Coordinates {

    fn into(self) -> Value {
        let mut map = Map::new();
        let _ = map.insert("longitude".to_owned(), self.longitude.into());
        let _ = map.insert("latitude".to_owned(), self.latitude.into());
        Value::Table(map)
    }

}

impl FromValue for Coordinates {
    fn from_value(v: &Value) -> Result<Self> {
        v.as_table()
            .ok_or_else(|| Error::from(EM::EntryHeaderTypeError))
            .and_then(|t| {
                let get = |m: &Map<_, _>, what: &'static str, ek: &'static str| -> Result<GPSValue> {
                    m.get(what)
                        .ok_or_else(|| anyhow!(ek))
                        .and_then(GPSValue::from_value)
                };

                Ok(Coordinates::new(
                    get(t, "longitude", "Longitude missing")?,
                    get(t, "latitude", "Latitude missing")?
                ))
            })
    }

}

impl Display for Coordinates {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "longitude = {}\nlatitude = {}", self.longitude, self.latitude)
    }
}