Skip to content

Commit

Permalink
split
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Oct 12, 2024
1 parent b2a2f8d commit 2674ee2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 69 deletions.
69 changes: 0 additions & 69 deletions src/factor_instances_provider/provider/factor_instances_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,75 +297,6 @@ impl CachedInstancesWithQuantitiesOutcome {
}
}

pub type InstancesByAgnosticPath = KeyedInstances<IndexAgnosticPath>;
pub type InstancesByDerivationPreset = KeyedInstances<DerivationPreset>;
impl InstancesByAgnosticPath {
pub fn into_derivation_preset(self) -> InstancesByDerivationPreset {
let map = self
.into_iter()
.map(|(k, v)| (DerivationPreset::try_from(k).unwrap(), v))
.collect::<IndexMap<DerivationPreset, FactorInstances>>();
InstancesByDerivationPreset::new(map)
}
}
pub struct KeyedInstances<K: Eq + std::hash::Hash + Clone>(IndexMap<K, FactorInstances>);
impl<K: Eq + std::hash::Hash + Clone> KeyedInstances<K> {
pub fn validate_from_source(
&self,
factor_source_id: impl Borrow<FactorSourceIDFromHash>,
) -> Result<()> {
if self
.all_instances()
.into_iter()
.any(|f| f.factor_source_id != *factor_source_id.borrow())
{
return Err(CommonError::FactorSourceDiscrepancy);
}
Ok(())
}

pub fn remove(&mut self, key: impl Borrow<K>) -> Option<FactorInstances> {
self.0.shift_remove(key.borrow())
}
pub fn all_instances(&self) -> FactorInstances {
self.0
.clone()
.into_iter()
.flat_map(|(_, v)| v.factor_instances())
.collect::<FactorInstances>()
}
pub fn new(map: IndexMap<K, FactorInstances>) -> Self {
Self(map)
}
}
impl<K: Eq + std::hash::Hash + Clone> IntoIterator for KeyedInstances<K> {
type Item = <IndexMap<K, FactorInstances> as IntoIterator>::Item;
type IntoIter = <IndexMap<K, FactorInstances> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<FactorInstances> for InstancesByAgnosticPath {
fn from(value: FactorInstances) -> Self {
let map = value
.factor_instances()
.into_iter()
.into_group_map_by(|f| f.agnostic_path())
.into_iter()
.map(|(k, v)| (k, FactorInstances::from_iter(v)))
.collect::<IndexMap<IndexAgnosticPath, FactorInstances>>();

Self::new(map)
}
}

impl From<FactorInstances> for InstancesByDerivationPreset {
fn from(value: FactorInstances) -> Self {
InstancesByAgnosticPath::from(value).into_derivation_preset()
}
}

impl FactorInstancesCache {
pub fn get_mono_factor(
&self,
Expand Down
76 changes: 76 additions & 0 deletions src/factor_instances_provider/provider/keyed_instances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::borrow::Borrow;

use crate::prelude::*;

pub struct KeyedInstances<K: Eq + std::hash::Hash + Clone>(pub IndexMap<K, FactorInstances>);

impl<K: Eq + std::hash::Hash + Clone> KeyedInstances<K> {
pub fn validate_from_source(
&self,
factor_source_id: impl Borrow<FactorSourceIDFromHash>,
) -> Result<()> {
if self
.all_instances()
.into_iter()
.any(|f| f.factor_source_id != *factor_source_id.borrow())
{
return Err(CommonError::FactorSourceDiscrepancy);
}
Ok(())
}

pub fn remove(&mut self, key: impl Borrow<K>) -> Option<FactorInstances> {
self.0.shift_remove(key.borrow())
}
pub fn all_instances(&self) -> FactorInstances {
self.0
.clone()
.into_iter()
.flat_map(|(_, v)| v.factor_instances())
.collect::<FactorInstances>()
}
pub fn new(map: IndexMap<K, FactorInstances>) -> Self {
Self(map)
}
}

impl<K: Eq + std::hash::Hash + Clone> IntoIterator for KeyedInstances<K> {
type Item = <IndexMap<K, FactorInstances> as IntoIterator>::Item;
type IntoIter = <IndexMap<K, FactorInstances> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

pub type InstancesByAgnosticPath = KeyedInstances<IndexAgnosticPath>;
pub type InstancesByDerivationPreset = KeyedInstances<DerivationPreset>;
impl InstancesByAgnosticPath {
pub fn into_derivation_preset(self) -> InstancesByDerivationPreset {
let map = self
.into_iter()
.map(|(k, v)| (DerivationPreset::try_from(k).unwrap(), v))
.collect::<IndexMap<DerivationPreset, FactorInstances>>();
InstancesByDerivationPreset::new(map)
}
}

impl From<FactorInstances> for InstancesByAgnosticPath {
fn from(value: FactorInstances) -> Self {
let map = value
.factor_instances()
.into_iter()
.into_group_map_by(|f| f.agnostic_path())
.into_iter()
.map(|(k, v)| (k, FactorInstances::from_iter(v)))
.collect::<IndexMap<IndexAgnosticPath, FactorInstances>>();

Self::new(map)
}
}

impl From<FactorInstances> for InstancesByDerivationPreset {
fn from(value: FactorInstances) -> Self {
InstancesByAgnosticPath::from(value).into_derivation_preset()
}
}
2 changes: 2 additions & 0 deletions src/factor_instances_provider/provider/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod factor_instances_cache;
mod factor_instances_provider;
mod keyed_instances;
mod outcome;

#[cfg(test)]
Expand All @@ -9,4 +10,5 @@ mod test_sargon_os;

pub use factor_instances_cache::*;
pub use factor_instances_provider::*;
pub use keyed_instances::*;
pub use outcome::*;

0 comments on commit 2674ee2

Please sign in to comment.