diff --git a/lib/entry/libimagentrygps/src/entry.rs b/lib/entry/libimagentrygps/src/entry.rs index e3072a26..13b73ef5 100644 --- a/lib/entry/libimagentrygps/src/entry.rs +++ b/lib/entry/libimagentrygps/src/entry.rs @@ -26,12 +26,32 @@ use libimagstore::store::Entry; use toml_query::read::TomlValueReadExt; use toml_query::insert::TomlValueInsertExt; +use toml_query::delete::TomlValueDeleteExt; pub trait GPSEntry { fn set_coordinates(&mut self, c: Coordinates) -> Result<()>; fn get_coordinates(&self) -> Result>; + /// Remove the coordinates from the entry + /// + /// # Returns + /// + /// The return type is a bit complicated, but that has a reason: + /// + /// The outer Result<_> is used for notifying a failure during the header read/write action. + /// If the Option<_> is Some(_), the value was deleted. + /// The inner Result<_> is used for parsing failures during the parsing of the deleted value. + /// + /// So: + /// + /// * Ok(Some(Ok(_))) if the coordinates were deleted, returning the deleted value + /// * Ok(Some(Err(_))) if the coordinates were deleted, but the deleted value couldn't be parsed + /// * Ok(None) if there were no coordinates to delete + /// * Err(e) if the deleting failed + /// + fn remove_coordinates(&mut self) -> Result>>; + } impl GPSEntry for Entry { @@ -51,6 +71,12 @@ impl GPSEntry for Entry { } } + fn remove_coordinates(&mut self) -> Result>> { + self.get_header_mut() + .delete("gps.coordinates") + .chain_err(|| GPSEK::HeaderWriteError) + .map(|opt| opt.as_ref().map(Coordinates::from_value)) + } } #[cfg(test)] diff --git a/lib/entry/libimagentrygps/src/types.rs b/lib/entry/libimagentrygps/src/types.rs index 587b17b4..b3e7afc2 100644 --- a/lib/entry/libimagentrygps/src/types.rs +++ b/lib/entry/libimagentrygps/src/types.rs @@ -18,6 +18,9 @@ // use std::collections::BTreeMap; +use std::fmt::Display; +use std::fmt::Formatter; +use std::fmt::Result as FmtResult; use toml::Value; @@ -46,6 +49,19 @@ impl GPSValue { seconds: s } } + + pub fn degree(&self) -> i8 { + self.degree + } + + pub fn minutes(&self) -> i8 { + self.minutes + } + + pub fn seconds(&self) -> i8 { + self.seconds + } + } @@ -96,6 +112,12 @@ impl FromValue for GPSValue { } +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 { @@ -110,6 +132,14 @@ impl Coordinates { latitude: lat, } } + + pub fn longitude(&self) -> &GPSValue { + &self.longitude + } + + pub fn latitude(&self) -> &GPSValue { + &self.latitude + } } impl Into for Coordinates { @@ -145,6 +175,12 @@ impl FromValue for Coordinates { } +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 { if i > (::max_value() as i64) {