Instead of leaking the tokio::sync:⌚:Receiver type in our
return value, we create a newtype that implements the desired
interface. This allows us to get rid of the `RateService` structs
and instead implement `LatestRate` directly on top of this struct.
Given that `LatestRate` is only used within the event_loop module,
we move the definition of this type into there.
pull/287/head
parent
f6ed4d65b5
commit
1822886cd0
@ -1,13 +1,7 @@
|
||||
mod amounts;
|
||||
pub mod command;
|
||||
pub mod config;
|
||||
pub mod fixed_rate;
|
||||
pub mod kraken;
|
||||
mod fixed_rate;
|
||||
mod rate;
|
||||
|
||||
pub use amounts::Rate;
|
||||
|
||||
pub trait LatestRate {
|
||||
type Error: std::error::Error + Send + Sync + 'static;
|
||||
|
||||
fn latest_rate(&mut self) -> Result<Rate, Self::Error>;
|
||||
}
|
||||
pub use self::fixed_rate::FixedRate;
|
||||
pub use self::rate::Rate;
|
||||
|
@ -1,23 +1,20 @@
|
||||
use crate::asb::{LatestRate, Rate};
|
||||
use std::convert::Infallible;
|
||||
use crate::asb::Rate;
|
||||
|
||||
pub const RATE: f64 = 0.01;
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FixedRate(Rate);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RateService(Rate);
|
||||
impl FixedRate {
|
||||
pub const RATE: f64 = 0.01;
|
||||
|
||||
impl LatestRate for RateService {
|
||||
type Error = Infallible;
|
||||
|
||||
fn latest_rate(&mut self) -> Result<Rate, Infallible> {
|
||||
Ok(self.0)
|
||||
pub fn value(&self) -> Rate {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RateService {
|
||||
impl Default for FixedRate {
|
||||
fn default() -> Self {
|
||||
Self(Rate {
|
||||
ask: bitcoin::Amount::from_btc(RATE).expect("Static value should never fail"),
|
||||
ask: bitcoin::Amount::from_btc(Self::RATE).expect("Static value should never fail"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
use crate::asb::{LatestRate, Rate};
|
||||
use crate::kraken;
|
||||
use anyhow::Result;
|
||||
use tokio::sync::watch::Receiver;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RateService {
|
||||
receiver: Receiver<Result<Rate, kraken::Error>>,
|
||||
}
|
||||
|
||||
impl LatestRate for RateService {
|
||||
type Error = kraken::Error;
|
||||
|
||||
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
|
||||
(*self.receiver.borrow()).clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl RateService {
|
||||
pub async fn new() -> Result<Self> {
|
||||
Ok(Self {
|
||||
receiver: kraken::connect().await?,
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue