Skip to main content

bulk_client/common/
instrument_config.rs

1use std::collections::HashMap;
2use serde::{Deserialize, Serialize};
3
4/// A map between instrument and value
5#[derive(Clone, Debug, Default, Serialize, Deserialize)]
6pub struct InstrumentConfig<T> {
7    pub default: T,
8    pub instruments: HashMap<String, T>,
9}
10
11impl<T> InstrumentConfig<T> {
12    pub fn new(default: T, instruments: HashMap<String, T>) -> Self {
13        Self {
14            default,
15            instruments,
16        }
17    }
18
19    /// Get appropriate value for instrument
20    ///
21    /// # Arguments
22    /// - `id`: instrument ID
23    ///
24    /// # Returns
25    /// - returns value associated with instrument or default
26    pub fn value_for(&self, id: &str) -> &T {
27        if let Some(instrument) = self.instruments.get(id) {
28            instrument
29        } else {
30            &self.default
31        }
32    }
33}