This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
208 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod matrix_builder; | ||
|
||
#[allow(unused_imports)] | ||
pub use matrix_builder::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
112
crates/rules/src/roles/abstract_role_builder_or_built.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod roles_builder; | ||
|
||
pub use roles_builder::*; |
Oops, something went wrong.