Refactoring: Use function chaining rather than matching
This commit is contained in:
parent
c92e459e3a
commit
1e2ac14d3b
2 changed files with 21 additions and 30 deletions
|
@ -64,10 +64,9 @@ impl GPSEntry for Entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_coordinates(&self) -> Result<Option<Coordinates>> {
|
fn get_coordinates(&self) -> Result<Option<Coordinates>> {
|
||||||
match self.get_header().read("gps.coordinates").chain_err(|| GPSEK::HeaderWriteError) {
|
match self.get_header().read("gps.coordinates").chain_err(|| GPSEK::HeaderWriteError)? {
|
||||||
Ok(Some(hdr)) => Coordinates::from_value(hdr).map(Some),
|
Some(hdr) => Coordinates::from_value(hdr).map(Some),
|
||||||
Ok(None) => Ok(None),
|
None => Ok(None),
|
||||||
Err(e) => Err(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,31 +78,27 @@ impl Into<Value> for GPSValue {
|
||||||
|
|
||||||
impl FromValue for GPSValue {
|
impl FromValue for GPSValue {
|
||||||
fn from_value(v: &Value) -> Result<Self> {
|
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 {
|
match *v {
|
||||||
Value::Table(ref map) => {
|
Value::Table(ref map) => {
|
||||||
Ok(GPSValue::new(
|
Ok(GPSValue::new(
|
||||||
map.get("degree")
|
map.get("degree")
|
||||||
.ok_or_else(|| GPSE::from_kind(GPSEK::DegreeMissing))
|
.ok_or_else(|| GPSE::from_kind(GPSEK::DegreeMissing))
|
||||||
.and_then(|v| match *v {
|
.and_then(&int_to_appropriate_width)?,
|
||||||
Value::Integer(i) => i64_to_i8(i),
|
|
||||||
_ => Err(GPSE::from_kind(GPSEK::HeaderTypeError)),
|
|
||||||
})?,
|
|
||||||
|
|
||||||
map
|
map
|
||||||
.get("minutes")
|
.get("minutes")
|
||||||
.ok_or_else(|| GPSE::from_kind(GPSEK::MinutesMissing))
|
.ok_or_else(|| GPSE::from_kind(GPSEK::MinutesMissing))
|
||||||
.and_then(|v| match *v {
|
.and_then(&int_to_appropriate_width)?,
|
||||||
Value::Integer(i) => i64_to_i8(i),
|
|
||||||
_ => Err(GPSE::from_kind(GPSEK::HeaderTypeError)),
|
|
||||||
})?,
|
|
||||||
|
|
||||||
map
|
map
|
||||||
.get("seconds")
|
.get("seconds")
|
||||||
.ok_or_else(|| GPSE::from_kind(GPSEK::SecondsMissing))
|
.ok_or_else(|| GPSE::from_kind(GPSEK::SecondsMissing))
|
||||||
.and_then(|v| match *v {
|
.and_then(&int_to_appropriate_width)?
|
||||||
Value::Integer(i) => i64_to_i8(i),
|
|
||||||
_ => Err(GPSE::from_kind(GPSEK::HeaderTypeError)),
|
|
||||||
})?
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
_ => Err(GPSE::from_kind(GPSEK::TypeError))
|
_ => Err(GPSE::from_kind(GPSEK::TypeError))
|
||||||
|
@ -154,22 +150,18 @@ impl Into<Value> for Coordinates {
|
||||||
|
|
||||||
impl FromValue for Coordinates {
|
impl FromValue for Coordinates {
|
||||||
fn from_value(v: &Value) -> Result<Self> {
|
fn from_value(v: &Value) -> Result<Self> {
|
||||||
match *v {
|
v.as_table()
|
||||||
Value::Table(ref map) => {
|
.ok_or(GPSE::from_kind(GPSEK::TypeError))
|
||||||
Ok(Coordinates::new(
|
.and_then(|t| {
|
||||||
match map.get("longitude") {
|
let get = |m: &BTreeMap<_, _>, what: &'static str, ek| -> Result<GPSValue> {
|
||||||
Some(v) => GPSValue::from_value(v),
|
m.get(what).ok_or(GPSE::from_kind(ek)).and_then(GPSValue::from_value)
|
||||||
None => Err(GPSE::from_kind(GPSEK::LongitudeMissing)),
|
};
|
||||||
}?,
|
|
||||||
|
|
||||||
match map.get("latitude") {
|
Ok(Coordinates::new(
|
||||||
Some(v) => GPSValue::from_value(v),
|
get(t, "longitude", GPSEK::LongitudeMissing)?,
|
||||||
None => Err(GPSE::from_kind(GPSEK::LongitudeMissing)),
|
get(t, "latitude", GPSEK::LatitudeMissing)?
|
||||||
}?
|
|
||||||
))
|
))
|
||||||
}
|
})
|
||||||
_ => Err(GPSE::from_kind(GPSEK::TypeError))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue