Skip to main content

digdigdig3_station/
persistence.rs

1//! Persistence configuration for the Station builder.
2//!
3//! Actual disk I/O lives in [`crate::series::DiskStore`], generic over
4//! [`crate::series::DataPoint`]. This module only carries on/off toggles
5//! threaded through the builder.
6
7use serde::{Deserialize, Serialize};
8
9/// Per-kind persistence toggles. Master `enabled` gates everything.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct PersistenceConfig {
12    pub enabled: bool,
13    pub trades: bool,
14    pub agg_trades: bool,
15    pub klines: bool,
16    pub tickers: bool,
17    pub orderbook_snapshots: bool,
18    pub mark_price: bool,
19    pub funding_rate: bool,
20    pub open_interest: bool,
21    pub liquidations: bool,
22}
23
24impl Default for PersistenceConfig {
25    fn default() -> Self {
26        Self {
27            enabled: false,
28            trades: false,
29            agg_trades: false,
30            klines: false,
31            tickers: false,
32            orderbook_snapshots: false,
33            mark_price: false,
34            funding_rate: false,
35            open_interest: false,
36            liquidations: false,
37        }
38    }
39}
40
41impl PersistenceConfig {
42    /// Enable persistence for every supported kind.
43    pub fn on() -> Self {
44        Self {
45            enabled: true,
46            trades: true,
47            agg_trades: true,
48            klines: true,
49            tickers: true,
50            orderbook_snapshots: true,
51            mark_price: true,
52            funding_rate: true,
53            open_interest: true,
54            liquidations: true,
55        }
56    }
57
58    pub fn trades(mut self, on: bool) -> Self { self.trades = on; self }
59    pub fn agg_trades(mut self, on: bool) -> Self { self.agg_trades = on; self }
60    pub fn klines(mut self, on: bool) -> Self { self.klines = on; self }
61    pub fn tickers(mut self, on: bool) -> Self { self.tickers = on; self }
62    pub fn orderbook_snapshots(mut self, on: bool) -> Self { self.orderbook_snapshots = on; self }
63    pub fn mark_price(mut self, on: bool) -> Self { self.mark_price = on; self }
64    pub fn funding_rate(mut self, on: bool) -> Self { self.funding_rate = on; self }
65    pub fn open_interest(mut self, on: bool) -> Self { self.open_interest = on; self }
66    pub fn liquidations(mut self, on: bool) -> Self { self.liquidations = on; self }
67
68    /// Should `kind` be persisted given the current config?
69    pub fn is_enabled_for(&self, kind: &crate::series::Kind) -> bool {
70        if !self.enabled {
71            return false;
72        }
73        use crate::series::Kind::*;
74        match kind {
75            Trade => self.trades,
76            AggTrade => self.agg_trades,
77            Kline(_) => self.klines,
78            Ticker => self.tickers,
79            Orderbook => self.orderbook_snapshots,
80            MarkPrice => self.mark_price,
81            FundingRate => self.funding_rate,
82            OpenInterest => self.open_interest,
83            Liquidation => self.liquidations,
84        }
85    }
86}