From ca2bd5e2910a99e3b7910b1ac3db4fbf6de64a97 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 15 Nov 2021 18:21:06 +1100 Subject: [PATCH] Statically ensure that our only failure mode is `NoConnection` By creating a dedicated error type, we can communicate that the only failure mode of this function is a missing connection. --- daemon/src/maker_inc_connections.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/daemon/src/maker_inc_connections.rs b/daemon/src/maker_inc_connections.rs index 3dbb69b..7cd5e0a 100644 --- a/daemon/src/maker_inc_connections.rs +++ b/daemon/src/maker_inc_connections.rs @@ -2,7 +2,7 @@ use crate::maker_cfd::{FromTaker, NewTakerOnline}; use crate::model::cfd::{Order, OrderId}; use crate::model::{BitMexPriceEventId, TakerId}; use crate::{forward_only_ok, maker_cfd, noise, send_to_socket, wire, HEARTBEAT_INTERVAL}; -use anyhow::{Context as AnyhowContext, Result}; +use anyhow::Result; use futures::{StreamExt, TryStreamExt}; use std::collections::HashMap; use std::io; @@ -84,11 +84,15 @@ impl Actor { } } - async fn send_to_taker(&mut self, taker_id: &TakerId, msg: wire::MakerToTaker) -> Result<()> { + async fn send_to_taker( + &mut self, + taker_id: &TakerId, + msg: wire::MakerToTaker, + ) -> Result<(), NoConnection> { let conn = self .write_connections .get(taker_id) - .context("no connection to taker_id")?; + .ok_or_else(|| NoConnection(*taker_id))?; let msg_str = msg.to_string(); @@ -156,6 +160,10 @@ impl Actor { } } +#[derive(Debug, thiserror::Error)] +#[error("No connection to taker {0}")] +pub struct NoConnection(TakerId); + #[xtra_productivity] impl Actor { async fn handle_broadcast_order(&mut self, msg: BroadcastOrder) { @@ -166,7 +174,7 @@ impl Actor { } } - async fn handle_taker_message(&mut self, msg: TakerMessage) -> Result<()> { + async fn handle_taker_message(&mut self, msg: TakerMessage) -> Result<(), NoConnection> { match msg.command { TakerCommand::SendOrder { order } => { self.send_to_taker(&msg.taker_id, wire::MakerToTaker::CurrentOrder(order)) @@ -221,6 +229,7 @@ impl Actor { .await?; } } + Ok(()) }