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
179
180
181
182
183
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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::collections::BTreeMap;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use toml::Value;

use error::GPSErrorKind as GPSEK;
use error::GPSError as GPSE;
use error::Result;

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

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

impl GPSValue {

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

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

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

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

}


impl Into<Value> for GPSValue {

    fn into(self) -> Value {
        let mut map = BTreeMap::new();
        let _ = map.insert("degree".to_owned(),  Value::Integer(self.degree as i64));
        let _ = map.insert("minutes".to_owned(), Value::Integer(self.minutes as i64));
        let _ = map.insert("seconds".to_owned(), Value::Integer(self.seconds as i64));
        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(GPSE::from_kind(GPSEK::HeaderTypeError)).and_then(i64_to_i8)
        };

        match *v {
            Value::Table(ref map) => {
                Ok(GPSValue::new(
                    map.get("degree")
                        .ok_or_else(|| GPSE::from_kind(GPSEK::DegreeMissing))
                        .and_then(&int_to_appropriate_width)?,

                    map
                        .get("minutes")
                        .ok_or_else(|| GPSE::from_kind(GPSEK::MinutesMissing))
                        .and_then(&int_to_appropriate_width)?,

                    map
                        .get("seconds")
                        .ok_or_else(|| GPSE::from_kind(GPSEK::SecondsMissing))
                        .and_then(&int_to_appropriate_width)?
                ))
            }
            _ => Err(GPSE::from_kind(GPSEK::TypeError))
        }
    }

}

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 = BTreeMap::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(GPSE::from_kind(GPSEK::TypeError))
            .and_then(|t| {
                let get = |m: &BTreeMap<_, _>, what: &'static str, ek| -> Result<GPSValue> {
                    m.get(what).ok_or(GPSE::from_kind(ek)).and_then(GPSValue::from_value)
                };

                Ok(Coordinates::new(
                    get(t, "longitude", GPSEK::LongitudeMissing)?,
                    get(t, "latitude", GPSEK::LatitudeMissing)?
                ))
            })
    }

}

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

/// Helper to convert a i64 to i8 or return an error if this doesn't work.
fn i64_to_i8(i: i64) -> Result<i8> {
    if i > (<i8>::max_value() as i64) {
        Err(GPSE::from_kind(GPSEK::NumberConversionError))
    } else {
        Ok(i as i8)
    }
}