use brk_cohort::Filter; use brk_error::Result; use brk_types::{BasisPoints16, BasisPoints32, BasisPointsSigned32, Cents, Height, Version}; use schemars::JsonSchema; use vecdb::{BytesVec, BytesVecValue, Database, ImportableVec}; use crate::{ indexes, internal::{ AmountPerBlock, AmountPerBlockCumulative, AmountPerBlockCumulativeRolling, CentsType, FiatPerBlock, FiatPerBlockCumulativeWithSums, NumericValue, PerBlock, PerBlockCumulativeRolling, PercentPerBlock, PercentRollingWindows, Price, PriceWithRatioExtendedPerBlock, PriceWithRatioPerBlock, RatioPerBlock, RollingWindow24hPerBlock, RollingWindows, RollingWindowsFrom1w, WindowStartVec, Windows, }, }; /// Trait for types importable via `ImportConfig::import`. pub(crate) trait ConfigImport: Sized { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result; } /// Implement `ConfigImport` for types whose `forced_import` takes `(db, name, version, indexes)`. macro_rules! impl_config_import { ($($type:ty),+ $(,)?) => { $( impl ConfigImport for $type { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } )+ }; } // Non-generic types impl_config_import!( AmountPerBlock, AmountPerBlockCumulative, PriceWithRatioPerBlock, PriceWithRatioExtendedPerBlock, RatioPerBlock, RatioPerBlock, PercentPerBlock, PercentPerBlock, PercentPerBlock, PercentRollingWindows, Price>, ); // Generic types (macro_rules can't parse generic bounds, so written out) impl ConfigImport for PerBlock { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } impl ConfigImport for PerBlockCumulativeRolling where T: NumericValue + JsonSchema + Into, C: NumericValue + JsonSchema, { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import( cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes, cfg.cached_starts, ) } } impl ConfigImport for RollingWindows { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } impl ConfigImport for RollingWindow24hPerBlock { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } impl ConfigImport for AmountPerBlockCumulativeRolling { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import( cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes, cfg.cached_starts, ) } } impl ConfigImport for FiatPerBlockCumulativeWithSums { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import( cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes, cfg.cached_starts, ) } } impl ConfigImport for RollingWindowsFrom1w { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } impl ConfigImport for FiatPerBlock { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes) } } impl ConfigImport for BytesVec { fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result { Ok(Self::forced_import( cfg.db, &cfg.name(suffix), cfg.version + offset, )?) } } #[derive(Clone, Copy)] pub struct ImportConfig<'a> { pub db: &'a Database, pub filter: &'a Filter, pub full_name: &'a str, pub version: Version, pub indexes: &'a indexes::Vecs, pub cached_starts: &'a Windows<&'a WindowStartVec>, } impl<'a> ImportConfig<'a> { pub(crate) fn name(&self, suffix: &str) -> String { if self.full_name.is_empty() { suffix.to_string() } else if suffix.is_empty() { self.full_name.to_string() } else { format!("{}_{suffix}", self.full_name) } } pub(crate) fn import(&self, suffix: &str, offset: Version) -> Result { T::config_import(self, suffix, offset) } }