Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
split and remove trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Nov 26, 2024
1 parent 6d898c6 commit 6078e40
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 233 deletions.
1 change: 0 additions & 1 deletion .tarpaulin.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[all]
exclude-files = ["src/types/sargon_types.rs", "src/testing/*", "src/samples/*"]
verbose = false
force-clean = true
timeout = "2m"
Expand Down
18 changes: 14 additions & 4 deletions crates/rules/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
mod matrix_builder;
mod roles_builder;
mod matrices;
mod move_to_sargon;
mod roles;

pub mod prelude {
pub(crate) use sargon::{
FactorSourceID, FactorSourceIDFromHash, FactorSourceKind, Identifiable, IndexSet, RoleKind,
};

#[allow(unused_imports)]
pub(crate) use crate::matrix_builder::*;
pub(crate) use crate::roles_builder::*;
pub(crate) use crate::matrices::*;
pub(crate) use crate::move_to_sargon::*;
pub(crate) use crate::roles::*;

pub(crate) use serde::{Deserialize, Serialize};
pub(crate) use std::collections::HashSet;
pub(crate) use std::marker::PhantomData;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::{collections::HashSet, marker::PhantomData};

use sargon::{FactorSourceID, FactorSourceKind, IndexSet, RoleKind};
use serde::{Deserialize, Serialize};

use crate::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -425,7 +420,6 @@ impl MatrixBuilder {
mod tests {

use super::*;
use sargon::FactorSourceID;

#[allow(clippy::upper_case_acronyms)]
type SUT = MatrixBuilder;
Expand Down
4 changes: 4 additions & 0 deletions crates/rules/src/matrices/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod matrix_builder;

#[allow(unused_imports)]
pub use matrix_builder::*;
21 changes: 21 additions & 0 deletions crates/rules/src/move_to_sargon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::prelude::*;

/// A kind of factor list, either threshold, or override kind.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum FactorListKind {
Threshold,
Override,
}

/// TODO move to Sargon!!!!
pub trait HasFactorSourceKindObjectSafe {
fn get_factor_source_kind(&self) -> FactorSourceKind;
}
impl HasFactorSourceKindObjectSafe for FactorSourceID {
fn get_factor_source_kind(&self) -> FactorSourceKind {
match self {
FactorSourceID::Hash { value } => value.kind,
FactorSourceID::Address { value } => value.kind,
}
}
}
112 changes: 112 additions & 0 deletions crates/rules/src/roles/abstract_role_builder_or_built.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use std::marker::PhantomData;

use serde::{Deserialize, Serialize};

use crate::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AbstractRoleBuilderOrBuilt<F, T> {
#[serde(skip)]
#[doc(hidden)]
built: PhantomData<T>,
role: RoleKind,
threshold: u8,
threshold_factors: Vec<F>,
override_factors: Vec<F>,
}

pub type AbstractBuiltRoleWithFactor<F> = AbstractRoleBuilderOrBuilt<F, ()>;
pub type RoleBuilder = AbstractRoleBuilderOrBuilt<FactorSourceID, RoleWithFactorSourceIds>;

impl<F> AbstractBuiltRoleWithFactor<F> {
pub(crate) fn with_factors(
role: RoleKind,
threshold: u8,
threshold_factors: impl IntoIterator<Item = F>,
override_factors: impl IntoIterator<Item = F>,
) -> Self {
Self {
built: PhantomData,
role,
threshold,
threshold_factors: threshold_factors.into_iter().collect(),
override_factors: override_factors.into_iter().collect(),
}
}
}

impl RoleBuilder {
pub(crate) fn new(role: RoleKind) -> Self {
Self {
built: PhantomData,
role,
threshold: 0,
threshold_factors: Vec::new(),
override_factors: Vec::new(),
}
}

pub(crate) fn role(&self) -> RoleKind {
self.role
}

pub(crate) fn threshold(&self) -> u8 {
self.threshold
}

pub(crate) fn mut_threshold_factors(&mut self) -> &mut Vec<FactorSourceID> {
&mut self.threshold_factors
}

pub(crate) fn mut_override_factors(&mut self) -> &mut Vec<FactorSourceID> {
&mut self.override_factors
}

pub(crate) fn threshold_factors(&self) -> &Vec<FactorSourceID> {
&self.threshold_factors
}

pub(crate) fn override_factors(&self) -> &Vec<FactorSourceID> {
&self.override_factors
}

pub(crate) fn factors(&self) -> Vec<&FactorSourceID> {
self.threshold_factors
.iter()
.chain(self.override_factors.iter())
.collect()
}

pub(crate) fn unchecked_add_factor_source_to_list(
&mut self,
factor_source_id: FactorSourceID,
factor_list_kind: FactorListKind,
) {
match factor_list_kind {
FactorListKind::Threshold => self.threshold_factors.push(factor_source_id),
FactorListKind::Override => self.override_factors.push(factor_source_id),
}
}

pub(crate) fn unchecked_set_threshold(&mut self, threshold: u8) {
self.threshold = threshold;
}
}

impl<F> AbstractBuiltRoleWithFactor<F> {
pub fn role(&self) -> RoleKind {
self.role
}

pub fn threshold(&self) -> u8 {
self.threshold
}

pub fn threshold_factors(&self) -> &Vec<F> {
&self.threshold_factors
}

pub fn override_factors(&self) -> &Vec<F> {
&self.override_factors
}
}
3 changes: 3 additions & 0 deletions crates/rules/src/roles/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod roles_builder;

pub use roles_builder::*;
Loading

0 comments on commit 6078e40

Please sign in to comment.