From 5870fa078593811effe46ed31de93ed98d4e0a82 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 28 Apr 2018 23:00:32 +0200 Subject: [PATCH 1/4] Fix: Use 16 bit variables for GPS degree/minute/second --- lib/entry/libimagentrygps/src/types.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/entry/libimagentrygps/src/types.rs b/lib/entry/libimagentrygps/src/types.rs index 54ce6e62..a8da0124 100644 --- a/lib/entry/libimagentrygps/src/types.rs +++ b/lib/entry/libimagentrygps/src/types.rs @@ -34,14 +34,14 @@ pub trait FromValue : Sized { #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct GPSValue { - pub degree: i8, - pub minutes: i8, - pub seconds: i8 + pub degree: i32, + pub minutes: i32, + pub seconds: i32, } impl GPSValue { - pub fn new(d: i8, m: i8, s: i8) -> GPSValue { + pub fn new(d: i32, m: i32, s: i32) -> GPSValue { GPSValue { degree: d, minutes: m, @@ -49,15 +49,15 @@ impl GPSValue { } } - pub fn degree(&self) -> i8 { + pub fn degree(&self) -> i32 { self.degree } - pub fn minutes(&self) -> i8 { + pub fn minutes(&self) -> i32 { self.minutes } - pub fn seconds(&self) -> i8 { + pub fn seconds(&self) -> i32 { self.seconds } @@ -80,7 +80,7 @@ impl FromValue for GPSValue { fn from_value(v: &Value) -> Result { let int_to_appropriate_width = |v: &Value| { v.as_integer() - .ok_or(GPSE::from_kind(GPSEK::HeaderTypeError)).and_then(i64_to_i8) + .ok_or(GPSE::from_kind(GPSEK::HeaderTypeError)).and_then(i64_to_i32) }; match *v { @@ -172,12 +172,12 @@ impl Display for Coordinates { } } -/// 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) { +/// Helper to convert a i64 to i32 or return an error if this doesn't work. +fn i64_to_i32(i: i64) -> Result { + if i > (::max_value() as i64) { Err(GPSE::from_kind(GPSEK::NumberConversionError)) } else { - Ok(i as i8) + Ok(i as i32) } } From f493b4b8c35ba812174d793e5b1f83a43f3db378 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 28 Apr 2018 23:16:10 +0200 Subject: [PATCH 2/4] Fix: Allow second to be missing --- bin/core/imag-gps/src/main.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bin/core/imag-gps/src/main.rs b/bin/core/imag-gps/src/main.rs index 502497a2..5378fac6 100644 --- a/bin/core/imag-gps/src/main.rs +++ b/bin/core/imag-gps/src/main.rs @@ -94,21 +94,35 @@ fn add(rt: &Runtime) { .map_err_trace_exit_unwrap(1); let c = { - let parse = |value: &str| -> Vec { - value.split(".") + let parse = |value: &str| -> (i32, i32, i32) { + debug!("Parsing '{}' into degree, minute and second", value); + let ary = value.split(".") + .map(|v| {debug!("Parsing = {}", v); v}) .map(FromStr::from_str) .map(|elem| { elem.or_else(|_| Err(GE::from(GEK::NumberConversionError))) .map_err_trace_exit_unwrap(1) }) - .collect::>() + .collect::>(); + + let degree = ary.get(0).unwrap_or_else(|| { + error!("Degree missing. This value is required."); + exit(1) + }); + let minute = ary.get(1).unwrap_or_else(|| { + error!("Degree missing. This value is required."); + exit(1) + }); + let second = ary.get(2).unwrap_or(&0); + + (*degree, *minute, *second) }; let long = parse(scmd.value_of("longitude").unwrap()); // unwrap safed by clap let lati = parse(scmd.value_of("latitude").unwrap()); // unwrap safed by clap - let long = GPSValue::new(long[0], long[1], long[2]); - let lati = GPSValue::new(lati[0], lati[1], lati[2]); + let long = GPSValue::new(long.0, long.1, long.2); + let lati = GPSValue::new(lati.0, lati.1, lati.2); Coordinates::new(long, lati) }; From 93c3bb5862b566c975f42a3b103357fef1a7f7a6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 28 Apr 2018 23:24:27 +0200 Subject: [PATCH 3/4] Upgrade variables to 64 bit --- lib/entry/libimagentrygps/src/types.rs | 30 ++++++++++---------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/entry/libimagentrygps/src/types.rs b/lib/entry/libimagentrygps/src/types.rs index a8da0124..265539e7 100644 --- a/lib/entry/libimagentrygps/src/types.rs +++ b/lib/entry/libimagentrygps/src/types.rs @@ -34,14 +34,14 @@ pub trait FromValue : Sized { #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct GPSValue { - pub degree: i32, - pub minutes: i32, - pub seconds: i32, + pub degree: i64, + pub minutes: i64, + pub seconds: i64, } impl GPSValue { - pub fn new(d: i32, m: i32, s: i32) -> GPSValue { + pub fn new(d: i64, m: i64, s: i64) -> GPSValue { GPSValue { degree: d, minutes: m, @@ -49,15 +49,15 @@ impl GPSValue { } } - pub fn degree(&self) -> i32 { + pub fn degree(&self) -> i64 { self.degree } - pub fn minutes(&self) -> i32 { + pub fn minutes(&self) -> i64 { self.minutes } - pub fn seconds(&self) -> i32 { + pub fn seconds(&self) -> i64 { self.seconds } @@ -68,9 +68,9 @@ impl Into 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)); + 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) } @@ -80,7 +80,7 @@ impl FromValue for GPSValue { fn from_value(v: &Value) -> Result { let int_to_appropriate_width = |v: &Value| { v.as_integer() - .ok_or(GPSE::from_kind(GPSEK::HeaderTypeError)).and_then(i64_to_i32) + .ok_or(GPSE::from_kind(GPSEK::HeaderTypeError)) }; match *v { @@ -172,12 +172,4 @@ impl Display for Coordinates { } } -/// Helper to convert a i64 to i32 or return an error if this doesn't work. -fn i64_to_i32(i: i64) -> Result { - if i > (::max_value() as i64) { - Err(GPSE::from_kind(GPSEK::NumberConversionError)) - } else { - Ok(i as i32) - } -} From dbb39475d982e8126fdbdb3e075f538a04ed460b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 28 Apr 2018 23:24:49 +0200 Subject: [PATCH 4/4] Upgrade variables to 64 bit --- bin/core/imag-gps/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/core/imag-gps/src/main.rs b/bin/core/imag-gps/src/main.rs index 5378fac6..a4415594 100644 --- a/bin/core/imag-gps/src/main.rs +++ b/bin/core/imag-gps/src/main.rs @@ -94,7 +94,7 @@ fn add(rt: &Runtime) { .map_err_trace_exit_unwrap(1); let c = { - let parse = |value: &str| -> (i32, i32, i32) { + let parse = |value: &str| -> (i64, i64, i64) { debug!("Parsing '{}' into degree, minute and second", value); let ary = value.split(".") .map(|v| {debug!("Parsing = {}", v); v}) @@ -103,7 +103,7 @@ fn add(rt: &Runtime) { elem.or_else(|_| Err(GE::from(GEK::NumberConversionError))) .map_err_trace_exit_unwrap(1) }) - .collect::>(); + .collect::>(); let degree = ary.get(0).unwrap_or_else(|| { error!("Degree missing. This value is required.");