From a8bfc1d686f737ec8363499988290cf0a9f3a160 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 19 Feb 2021 17:04:42 +1100 Subject: [PATCH] Make LatestRate::Error require std::error::Error trait bound This allows us to use .context instead of .map_err when calling `latest_rate()`. For the static rate module, we simply fill in `Infallible` which is actually better suited because it describes that we are never using this error. --- swap/src/asb.rs | 2 +- swap/src/asb/fixed_rate.rs | 6 +++--- swap/src/protocol/alice/event_loop.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/swap/src/asb.rs b/swap/src/asb.rs index 2fda0610..77006526 100644 --- a/swap/src/asb.rs +++ b/swap/src/asb.rs @@ -8,7 +8,7 @@ mod amounts; pub use amounts::Rate; pub trait LatestRate { - type Error: std::fmt::Debug; + type Error: std::error::Error + Send + Sync + 'static; fn latest_rate(&mut self) -> Result; } diff --git a/swap/src/asb/fixed_rate.rs b/swap/src/asb/fixed_rate.rs index 53d42308..1bf56364 100644 --- a/swap/src/asb/fixed_rate.rs +++ b/swap/src/asb/fixed_rate.rs @@ -1,5 +1,5 @@ use crate::asb::{LatestRate, Rate}; -use anyhow::Result; +use std::convert::Infallible; pub const RATE: f64 = 0.01; @@ -7,9 +7,9 @@ pub const RATE: f64 = 0.01; pub struct RateService(Rate); impl LatestRate for RateService { - type Error = anyhow::Error; + type Error = Infallible; - fn latest_rate(&mut self) -> Result { + fn latest_rate(&mut self) -> Result { Ok(self.0) } } diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index b0a03d71..57c6f546 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -14,7 +14,7 @@ use crate::{ }, seed::Seed, }; -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use futures::future::RemoteHandle; use libp2p::{ core::Multiaddr, futures::FutureExt, request_response::ResponseChannel, PeerId, Swarm, @@ -208,7 +208,7 @@ where let rate = self .rate_service .latest_rate() - .map_err(|e| anyhow!("Failed to get latest rate: {:?}", e))?; + .context("Failed to get latest rate")?; let btc_amount = quote_request.btc_amount; let xmr_amount = rate.sell_quote(btc_amount)?;