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
1 parent
654995a
commit 3b5adb3
Showing
6 changed files
with
182 additions
and
159 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
crates/rules/src/matrices/abstract_matrix_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,12 @@ | ||
use crate::prelude::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
pub(crate) struct AbstractMatrixBuilderOrBuilt<F, T, U> { | ||
#[serde(skip)] | ||
#[doc(hidden)] | ||
pub(crate) built: PhantomData<T>, | ||
|
||
pub(crate) primary_role: AbstractRoleBuilderOrBuilt<F, U>, | ||
pub(crate) recovery_role: AbstractRoleBuilderOrBuilt<F, U>, | ||
pub(crate) confirmation_role: AbstractRoleBuilderOrBuilt<F, U>, | ||
} |
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,52 @@ | ||
use crate::prelude::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)] | ||
pub enum MatrixRolesInCombinationViolation { | ||
#[error("Basic violation: {0}")] | ||
Basic(#[from] MatrixRolesInCombinationBasicViolation), | ||
|
||
#[error("Forever invalid: {0}")] | ||
ForeverInvalid(#[from] MatrixRolesInCombinationForeverInvalid), | ||
|
||
#[error("Not yet valid: {0}")] | ||
NotYetValid(#[from] MatrixRolesInCombinationNotYetValid), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)] | ||
pub enum MatrixRolesInCombinationBasicViolation { | ||
#[error("The factor source was not found in any role")] | ||
FactorSourceNotFoundInAnyRole, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)] | ||
pub enum MatrixRolesInCombinationForeverInvalid { | ||
#[error("Recovery and confirmation factors overlap. No factor may be used in both the recovery and confirmation roles")] | ||
RecoveryAndConfirmationFactorsOverlap, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)] | ||
pub enum MatrixRolesInCombinationNotYetValid { | ||
#[error("The single factor used in the primary role must not be used in any other role")] | ||
SingleFactorUsedInPrimaryMustNotBeUsedInAnyOtherRole, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)] | ||
pub enum MatrixBuilderValidation { | ||
#[error("Role {role:?} in isolation violation: {violation}")] | ||
RoleInIsolation { | ||
role: RoleKind, | ||
violation: RoleBuilderValidation, | ||
}, | ||
#[error("Roles in combination violation: {0}")] | ||
CombinationViolation(#[from] MatrixRolesInCombinationViolation), | ||
} | ||
|
||
pub(crate) trait IntoMatrixErr<T> { | ||
fn into_matrix_err(self, role: RoleKind) -> Result<T, MatrixBuilderValidation>; | ||
} | ||
|
||
impl<T> IntoMatrixErr<T> for Result<T, RoleBuilderValidation> { | ||
fn into_matrix_err(self, role: RoleKind) -> Result<T, MatrixBuilderValidation> { | ||
self.map_err(|violation| MatrixBuilderValidation::RoleInIsolation { role, violation }) | ||
} | ||
} |
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,6 @@ | ||
mod error; | ||
mod matrix_builder; | ||
|
||
pub use error::*; | ||
#[allow(unused_imports)] | ||
pub use matrix_builder::*; |
36 changes: 36 additions & 0 deletions
36
crates/rules/src/matrices/matrix_with_factor_source_ids.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,36 @@ | ||
use crate::prelude::*; | ||
|
||
pub type MatrixWithFactorSourceIds = AbstractMatrixBuilderOrBuilt<FactorSourceID, (), ()>; | ||
|
||
#[cfg(test)] | ||
impl MatrixWithFactorSourceIds { | ||
pub(crate) fn with_roles( | ||
primary: RoleWithFactorSourceIds, | ||
recovery: RoleWithFactorSourceIds, | ||
confirmation: RoleWithFactorSourceIds, | ||
) -> Self { | ||
assert_eq!(primary.role(), sargon::RoleKind::Primary); | ||
assert_eq!(recovery.role(), sargon::RoleKind::Recovery); | ||
assert_eq!(confirmation.role(), sargon::RoleKind::Confirmation); | ||
Self { | ||
built: PhantomData, | ||
primary_role: primary, | ||
recovery_role: recovery, | ||
confirmation_role: confirmation, | ||
} | ||
} | ||
} | ||
|
||
impl MatrixWithFactorSourceIds { | ||
pub fn primary(&self) -> &RoleWithFactorSourceIds { | ||
&self.primary_role | ||
} | ||
|
||
pub fn recovery(&self) -> &RoleWithFactorSourceIds { | ||
&self.recovery_role | ||
} | ||
|
||
pub fn confirmation(&self) -> &RoleWithFactorSourceIds { | ||
&self.confirmation_role | ||
} | ||
} |
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,4 +1,8 @@ | ||
mod matrix_builder; | ||
mod abstract_matrix_builder_or_built; | ||
mod builder; | ||
mod matrix_with_factor_source_ids; | ||
|
||
pub(crate) use abstract_matrix_builder_or_built::*; | ||
#[allow(unused_imports)] | ||
pub use matrix_builder::*; | ||
pub use builder::*; | ||
pub use matrix_with_factor_source_ids::*; |