refactor(location): make location on the kml object not optional

This commit is contained in:
dignifiedquire
2019-08-26 21:49:47 +02:00
parent 4ec214b3fc
commit 3f8abd2218
2 changed files with 23 additions and 30 deletions

View File

@@ -938,15 +938,13 @@ unsafe fn save_locations(
let mut send_event = false;
if !mime_parser.message_kml.is_none() && chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint {
if let Some(ref locations) = mime_parser.message_kml.as_ref().unwrap().locations {
let newest_location_id =
location::save(context, chat_id, from_id, locations, 1).unwrap_or_default();
if 0 != newest_location_id && 0 == hidden {
if location::set_msg_location_id(context, insert_msg_id, newest_location_id).is_ok()
{
location_id_written = true;
send_event = true;
}
let locations = &mime_parser.message_kml.as_ref().unwrap().locations;
let newest_location_id =
location::save(context, chat_id, from_id, locations, 1).unwrap_or_default();
if 0 != newest_location_id && 0 == hidden {
if location::set_msg_location_id(context, insert_msg_id, newest_location_id).is_ok() {
location_id_written = true;
send_event = true;
}
}
}
@@ -957,23 +955,19 @@ unsafe fn save_locations(
if !contact.get_addr().is_empty()
&& contact.get_addr().to_lowercase() == addr.to_lowercase()
{
if let Some(ref locations) =
mime_parser.location_kml.as_ref().unwrap().locations
{
let newest_location_id =
location::save(context, chat_id, from_id, locations, 0)
.unwrap_or_default();
if newest_location_id != 0 && hidden == 0 && !location_id_written {
if let Err(err) = location::set_msg_location_id(
context,
insert_msg_id,
newest_location_id,
) {
error!(context, 0, "Failed to set msg_location_id: {:?}", err);
}
let locations = &mime_parser.location_kml.as_ref().unwrap().locations;
let newest_location_id =
location::save(context, chat_id, from_id, locations, 0).unwrap_or_default();
if newest_location_id != 0 && hidden == 0 && !location_id_written {
if let Err(err) = location::set_msg_location_id(
context,
insert_msg_id,
newest_location_id,
) {
error!(context, 0, "Failed to set msg_location_id: {:?}", err);
}
send_event = true;
}
send_event = true;
}
}
}

View File

@@ -39,7 +39,7 @@ impl Location {
#[derive(Debug, Clone, Default)]
pub struct Kml {
pub addr: Option<String>,
pub locations: Option<Vec<Location>>,
pub locations: Vec<Location>,
tag: KmlTag,
pub curr: Location,
}
@@ -72,7 +72,7 @@ impl Kml {
reader.trim_text(true);
let mut kml = Kml::new();
kml.locations = Some(Vec::with_capacity(100));
kml.locations = Vec::with_capacity(100);
let mut buf = Vec::new();
@@ -142,9 +142,8 @@ impl Kml {
&& 0. != self.curr.latitude
&& 0. != self.curr.longitude
{
if let Some(ref mut locations) = self.locations {
locations.push(std::mem::replace(&mut self.curr, Location::new()));
}
self.locations
.push(std::mem::replace(&mut self.curr, Location::new()));
}
self.tag = KmlTag::UNDEFINED;
};
@@ -675,7 +674,7 @@ mod tests {
assert!(kml.addr.is_some());
assert_eq!(kml.addr.as_ref().unwrap(), "user@example.org",);
let locations_ref = &kml.locations.as_ref().unwrap();
let locations_ref = &kml.locations;
assert_eq!(locations_ref.len(), 2);
assert!(locations_ref[0].latitude > 53.6f64);