From 38d5743c06993452e542332aaf16e9a98d502651 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 28 Sep 2023 15:19:33 +0000 Subject: [PATCH] refactor: do not ignore errors in get_kml() This removes unnecessary warning "mimefactory: could not send location: No locations processed" when there are no locations to send. --- src/location.rs | 10 ++++++---- src/mimefactory.rs | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/location.rs b/src/location.rs index dc07181ec..9084e752f 100644 --- a/src/location.rs +++ b/src/location.rs @@ -460,7 +460,7 @@ pub async fn delete_all(context: &Context) -> Result<()> { } /// Returns `location.kml` contents. -pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<(String, u32)> { +pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result> { let mut last_added_location_id = 0; let self_addr = context.get_primary_self_addr().await?; @@ -530,9 +530,11 @@ pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<(String, u32) ret += "\n"; } - ensure!(location_count > 0, "No locations processed"); - - Ok((ret, last_added_location_id)) + if location_count > 0 { + Ok(Some((ret, last_added_location_id))) + } else { + Ok(None) + } } fn get_kml_timestamp(utc: i64) -> String { diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 996ea3fd7..0f55423cc 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -860,9 +860,13 @@ impl<'a> MimeFactory<'a> { } /// Returns MIME part with a `location.kml` attachment. - async fn get_location_kml_part(&mut self, context: &Context) -> Result { - let (kml_content, last_added_location_id) = - location::get_kml(context, self.msg.chat_id).await?; + async fn get_location_kml_part(&mut self, context: &Context) -> Result> { + let Some((kml_content, last_added_location_id)) = + location::get_kml(context, self.msg.chat_id).await? + else { + return Ok(None); + }; + let part = PartBuilder::new() .content_type( &"application/vnd.google-earth.kml+xml" @@ -878,7 +882,7 @@ impl<'a> MimeFactory<'a> { // otherwise, the independent location is already filed self.last_added_location_id = last_added_location_id; } - Ok(part) + Ok(Some(part)) } #[allow(clippy::cognitive_complexity)] @@ -1230,11 +1234,8 @@ impl<'a> MimeFactory<'a> { } if location::is_sending_locations_to_chat(context, Some(self.msg.chat_id)).await? { - match self.get_location_kml_part(context).await { - Ok(part) => parts.push(part), - Err(err) => { - warn!(context, "mimefactory: could not send location: {}", err); - } + if let Some(part) = self.get_location_kml_part(context).await? { + parts.push(part); } }