From 3a4fabc292a9c6c1405e78312b24ea67a945ecc7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 10 Dec 2021 18:03:21 +0100 Subject: Refactor: Add helper fn for sending gossip reply object Signed-off-by: Matthias Beyer --- lib/src/reactor/gossip/mod.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/src/reactor/gossip/mod.rs b/lib/src/reactor/gossip/mod.rs index 884f42a..52bff51 100644 --- a/lib/src/reactor/gossip/mod.rs +++ b/lib/src/reactor/gossip/mod.rs @@ -46,6 +46,14 @@ impl GossipReactor { self.inner.receive_next_message().await } + fn send_gossip_reply(channel: ReplyChannel, reply: GossipReply) -> Result<()> { + if let Err(_) = channel.send(ReactorReply::Custom(reply)) { + anyhow::bail!("Failed to send GossipReply::NoHead)") + } + + Ok(()) + } + pub(super) async fn process_reactor_message(&mut self, request: (ReactorRequest, ReplyChannel)) -> Result<()> { match self.inner.process_reactor_message(request).await? { None => Ok(()), @@ -63,17 +71,15 @@ impl GossipReactor { let head = profile.head(); if head.is_none() { - if let Err(_) = reply_channel.send(ReactorReply::Custom(GossipReply::NoHead)) { - anyhow::bail!("Failed to send GossipReply::NoHead)") - } + Self::send_gossip_reply(reply_channel, GossipReply::NoHead)?; + return Ok(()) } let head = head.unwrap().to_bytes(); let own_id = match profile.client().own_id().await { Ok(id) => id, - Err(e) => if let Err(_) = reply_channel.send(ReactorReply::Custom(GossipReply::PublishMeResult(Err(e)))) { - anyhow::bail!("Failed to send GossipReply::PublishMeResult(Err(_))") - } else { + Err(e) => { + Self::send_gossip_reply(reply_channel, GossipReply::PublishMeResult(Err(e)))?; return Ok(()) // TODO: abort operation here for now, maybe not the best idea } }; @@ -88,18 +94,8 @@ impl GossipReactor { .await; match publish_res { - Ok(()) => if let Err(_) = reply_channel.send(ReactorReply::Custom(GossipReply::PublishMeResult(Ok(())))) { - anyhow::bail!("Failed to send GossipReply::PublishMeResult(Ok(()))") - } else { - Ok(()) - }, - - Err(e) => if let Err(_) = reply_channel.send(ReactorReply::Custom(GossipReply::PublishMeResult(Err(e)))) { - anyhow::bail!("Failed to send GossipReply::PublishMeResult(Err(_))") - } else { - Ok(()) - } - + Ok(()) => Self::send_gossip_reply(reply_channel, GossipReply::PublishMeResult(Ok(()))), + Err(e) => Self::send_gossip_reply(reply_channel, GossipReply::PublishMeResult(Err(e))), } }, } -- cgit v1.2.3