From 7b83c1dccffe02de9fe9cf21991d5d48a643601d Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Mon, 25 Nov 2024 10:21:57 +0100 Subject: [PATCH] remove other wip crates, rename wip -> rules --- Cargo.lock | 16 +- Cargo.toml | 6 +- crates/actions/Cargo.toml | 7 - crates/actions/src/lib.rs | 777 ------------------ crates/generics/Cargo.toml | 8 - crates/generics/src/lib.rs | 14 - .../src/rules/factor_rules_violation.rs | 47 -- .../src/rules/has_rule_set_for_role.rs | 150 ---- crates/generics/src/rules/is_role.rs | 15 - crates/generics/src/rules/matrix.rs | 35 - crates/generics/src/rules/mod.rs | 21 - ...rimary_role_with_factor_sources_builder.rs | 147 ---- .../src/rules/role_builder_or_built.rs | 241 ------ crates/generics/src/rules/role_kind.rs | 6 - crates/generics/src/rules/role_tags.rs | 48 -- crates/generics/src/rules/sargon_types.rs | 130 --- crates/{wip => rules}/Cargo.toml | 2 +- crates/{wip => rules}/src/lib.rs | 0 crates/{wip => rules}/src/roles_builder.rs | 0 19 files changed, 10 insertions(+), 1660 deletions(-) delete mode 100644 crates/actions/Cargo.toml delete mode 100644 crates/actions/src/lib.rs delete mode 100644 crates/generics/Cargo.toml delete mode 100644 crates/generics/src/lib.rs delete mode 100644 crates/generics/src/rules/factor_rules_violation.rs delete mode 100644 crates/generics/src/rules/has_rule_set_for_role.rs delete mode 100644 crates/generics/src/rules/is_role.rs delete mode 100644 crates/generics/src/rules/matrix.rs delete mode 100644 crates/generics/src/rules/mod.rs delete mode 100644 crates/generics/src/rules/primary_role_with_factor_sources_builder.rs delete mode 100644 crates/generics/src/rules/role_builder_or_built.rs delete mode 100644 crates/generics/src/rules/role_kind.rs delete mode 100644 crates/generics/src/rules/role_tags.rs delete mode 100644 crates/generics/src/rules/sargon_types.rs rename crates/{wip => rules}/Cargo.toml (91%) rename crates/{wip => rules}/src/lib.rs (100%) rename crates/{wip => rules}/src/roles_builder.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 1b337f74..466b9ac1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2509,6 +2509,14 @@ dependencies = [ "subtle", ] +[[package]] +name = "rules" +version = "0.1.0" +dependencies = [ + "sargon", + "thiserror 2.0.3", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -3869,14 +3877,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "wip" -version = "0.1.0" -dependencies = [ - "sargon", - "thiserror 2.0.3", -] - [[package]] name = "write16" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 183b194c..1e18afce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,6 @@ [workspace] resolver = "2" -members = [ - # "crates/generics", - # "crates/actions", - "crates/wip", -] +members = ["crates/rules"] [profile.release] incremental = false diff --git a/crates/actions/Cargo.toml b/crates/actions/Cargo.toml deleted file mode 100644 index 61f6eb88..00000000 --- a/crates/actions/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "actions" -version = "0.1.0" -edition = "2021" - -[dependencies] -thiserror = { workspace = true } diff --git a/crates/actions/src/lib.rs b/crates/actions/src/lib.rs deleted file mode 100644 index 3d673850..00000000 --- a/crates/actions/src/lib.rs +++ /dev/null @@ -1,777 +0,0 @@ -use std::collections::HashSet; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum FactorSourceKind { - Device, - Ledger, - Password, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum RoleKind { - Primary, - Recovery, - Confirmation, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum FactorListKind { - Override, - Threshold, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct FactorSource { - kind: FactorSourceKind, - name: String, -} - -impl FactorSource { - fn new(kind: FactorSourceKind, name: impl AsRef) -> Self { - Self { - kind, - name: name.as_ref().to_owned(), - } - } - - fn new_sample(kind: FactorSourceKind) -> Self { - Self::new(kind, "Sample") - } - - fn new_other_sample(kind: FactorSourceKind) -> Self { - Self::new(kind, "Other Sample") - } - - fn sample_device() -> Self { - Self::new_sample(FactorSourceKind::Device) - } - - fn sample_other_device() -> Self { - Self::new_other_sample(FactorSourceKind::Device) - } - - fn sample_ledger() -> Self { - Self::new_sample(FactorSourceKind::Ledger) - } - - fn sample_other_ledger() -> Self { - Self::new_other_sample(FactorSourceKind::Ledger) - } - - fn sample_password() -> Self { - Self::new_sample(FactorSourceKind::Password) - } - - fn sample_other_password() -> Self { - Self::new_other_sample(FactorSourceKind::Password) - } -} - -pub trait FactorListBuilder: Clone { - fn factors(&self) -> Vec<&FactorSource>; - fn contains(&self, factor_source: &FactorSource) -> bool { - self.factors().contains(&factor_source) - } - fn perform_action(&mut self, action: Action) -> Outcome { - match action { - Action::SetThreshold(action) => self.set_threshold(action), - Action::Add(action) => self.add(action), - Action::Remove(action) => self.remove(action), - Action::CheckSupport(action) => self.check_support(action), - } - } - fn add(&mut self, action: AddFactorSourceAction) -> Outcome; - fn remove(&mut self, action: RemoveFactorSourceAction) -> Outcome; - fn set_threshold(&mut self, action: SetThresholdAction) -> Outcome; - fn check_support(&self, action: CheckSupportForFactorSourceOfKindAction) -> Outcome; - - fn validate(&self) -> Outcome; - - fn simulate_outcome_of(&self, action: Action) -> Outcome { - let mut simulation = self.clone(); - simulation.perform_action(action) - } - - fn can_user_perform(&self, action: Action) -> bool { - match self.simulate_outcome_of(action) { - Ok(_) => true, - Err(e) => match e { - FactorValidation::Basic(_) => false, - FactorValidation::Action(a) => match a { - FactorActionValidation::NotYetValid(_) => true, // Action might have made state "more valid". - FactorActionValidation::ForeverInvalid(_) => false, - }, - }, - } - } - - fn can_user_add_factor_source_of_kind( - &self, - kind: FactorSourceKind, - to_role: RoleKind, - in_list: FactorListKind, - ) -> bool { - self.can_user_perform(Action::CheckSupport( - CheckSupportForFactorSourceOfKindAction { - role_kind: to_role, - factor_source_kind: kind, - list_kind: in_list, - }, - )) - } - - fn can_user_add_factor_source( - &self, - factor_source: FactorSource, - to_role: RoleKind, - in_list: FactorListKind, - ) -> bool { - self.can_user_perform(Action::Add(AddFactorSourceAction { - role_kind: to_role, - factor_source, - list_kind: in_list, - })) - } -} - -pub trait FactorListSingleRoleBuilder: FactorListBuilder { - fn role_kind(&self) -> RoleKind; -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ThresholdListBuilder { - role_kind: RoleKind, - threshold: u8, - factors: Vec, -} -impl ThresholdListBuilder { - fn list_kind() -> FactorListKind { - FactorListKind::Threshold - } -} -impl FactorListSingleRoleBuilder for ThresholdListBuilder { - fn role_kind(&self) -> RoleKind { - self.role_kind - } -} -impl FactorListBuilder for ThresholdListBuilder { - fn factors(&self) -> Vec<&FactorSource> { - self.factors.iter().collect() - } - fn add(&mut self, action: AddFactorSourceAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - assert_eq!(action.list_kind, Self::list_kind()); - self.factors.push(action.factor_source); - Ok(()) - } - - /// Automatically lowers threshold if threshold > threshold_factors.len() - /// after deletion. - fn remove(&mut self, action: RemoveFactorSourceAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - if let Some(index) = self.factors.iter().position(|f| *f == action.factor_source) { - self.factors.remove(index); - let len = self.factors.len(); - if self.threshold as usize > len { - self.threshold = len as u8; - } - return Ok(()); - } else { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::UnableToRemoveFactorNotFound, - ), - )); - } - } - - fn set_threshold(&mut self, action: SetThresholdAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - let new_threshold = action.to; - if new_threshold as usize > self.factors.len() { - return Outcome::Err(FactorValidation::Action( - FactorActionValidation::NotYetValid(ValidIf::TriedToChangeThresholdToTooHighValue), - )); - } else { - self.threshold = new_threshold; - return Ok(()); - } - } - - fn check_support(&self, action: CheckSupportForFactorSourceOfKindAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - assert_eq!(action.list_kind, Self::list_kind()); - todo!() - } - - fn validate(&self) -> Outcome { - self.role_kind - .validate_threshold_in_isolation(self.threshold, self.factors()) - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct OverrideListBuilder { - role_kind: RoleKind, - factors: Vec, -} -impl OverrideListBuilder { - fn list_kind() -> FactorListKind { - FactorListKind::Override - } -} -impl FactorListSingleRoleBuilder for OverrideListBuilder { - fn role_kind(&self) -> RoleKind { - self.role_kind - } -} -impl FactorListBuilder for OverrideListBuilder { - fn factors(&self) -> Vec<&FactorSource> { - self.factors.iter().collect() - } - - fn add(&mut self, action: AddFactorSourceAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - assert_eq!(action.list_kind, Self::list_kind()); - self.factors.push(action.factor_source); - self.validate() - } - - fn remove(&mut self, action: RemoveFactorSourceAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - if !self.contains(&action.factor_source) { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::UnableToRemoveFactorNotFound, - ), - )); - } - self.factors - .retain(|factor| factor != &action.factor_source); - - self.validate() - } - - fn set_threshold(&mut self, action: SetThresholdAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::ShouldNotHaveCalledSetThresholdOnOverrideList, - ), - )) - } - - fn check_support(&self, action: CheckSupportForFactorSourceOfKindAction) -> Outcome { - assert_eq!(action.role_kind, self.role_kind()); - assert_eq!(action.list_kind, Self::list_kind()); - todo!() - } - - fn validate(&self) -> Outcome { - self.role_kind - .validate_override_in_isolation(self.factors()) - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct RoleBuilder { - role_kind: RoleKind, - threshold_list_builder: ThresholdListBuilder, - override_list_builder: OverrideListBuilder, -} -impl RoleBuilder { - pub fn new(role_kind: RoleKind) -> Self { - Self { - role_kind, - threshold_list_builder: ThresholdListBuilder { - role_kind, - threshold: 0, - factors: Vec::new(), - }, - override_list_builder: OverrideListBuilder { - role_kind, - factors: Vec::new(), - }, - } - } - - fn build(self) -> BuildRoleOutcome { - self.validate()?; - let validated = RoleWithFactors { - __hidden: (), - threshold: self.threshold_list_builder.threshold, - threshold_factors: self.threshold_list_builder.factors, - override_factors: self.override_list_builder.factors, - }; - Ok(validated) - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct SetThresholdAction { - role_kind: RoleKind, - to: u8, - role: RoleKind, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct AddFactorSourceAction { - role_kind: RoleKind, - factor_source: FactorSource, - list_kind: FactorListKind, -} -impl AddFactorSourceAction { - pub fn new(role: RoleKind, factor: FactorSource, list: FactorListKind) -> Self { - Self { - role_kind: role, - factor_source: factor, - list_kind: list, - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct RemoveFactorSourceAction { - role_kind: RoleKind, - factor_source: FactorSource, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct CheckSupportForFactorSourceOfKindAction { - role_kind: RoleKind, - factor_source_kind: FactorSourceKind, - list_kind: FactorListKind, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum Action { - SetThreshold(SetThresholdAction), - Add(AddFactorSourceAction), - Remove(RemoveFactorSourceAction), - CheckSupport(CheckSupportForFactorSourceOfKindAction), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum FactorValidation { - Basic(BasicFactorInteractionValidation), - Action(FactorActionValidation), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum BasicFactorInteractionValidation { - UserInterfaceShouldHavePrevented(InvalidStateFailure), - InternalStateError(InternalStateFailure), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum InvalidStateFailure { - ThresholdFactorsNotSupportedForRecovery, - ThresholdFactorsNotSupportedForConfirmation, - ThresholdNotSupportedForRecovery, - ThresholdNotSupportedForConfirmation, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum InternalStateFailure { - UnableToRemoveFactorNotFound, - ShouldNotHaveCalledSetThresholdOnOverrideList, - FactorCannotBePresentInBothOverrideAndThreshold, - DuplicateFactorsFoundInThresholdList, - DuplicateFactorsFoundInOverrideList, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum FactorActionValidation { - NotYetValid(ValidIf), - ForeverInvalid(ForeverInvalid), -} - -/// The validation state is not yet valid, but can be if more factors were added -/// or if threshold was lowered. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ValidIf { - /// Threshold higher than threshold factor count - ThresholdDecreased, - - /// Threshold higher than threshold factor count - TriedToChangeThresholdToTooHighValue, -} - -/// The validation state is forever invalid, no matter what factors are added the -/// state can never be valid, you have to at least REMOVE factors to make it valid, -/// possibly also add other factors. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ForeverInvalid { - ThresholdCannotBeLargerThanFactorList, -} - -pub type Outcome = std::result::Result<(), FactorValidation>; -pub type BuildRoleOutcome = std::result::Result; -pub type BuildMatrixOutcome = std::result::Result; - -impl RoleBuilder { - fn list_of_factor_source(&self, factor_source: &FactorSource) -> Option { - if self.override_list_builder.contains(factor_source) { - return Some(FactorListKind::Override); - } - if self.threshold_list_builder.contains(factor_source) { - return Some(FactorListKind::Threshold); - } - None - } -} - -impl FactorListSingleRoleBuilder for RoleBuilder { - fn role_kind(&self) -> RoleKind { - self.role_kind - } -} -impl FactorListBuilder for RoleBuilder { - fn factors(&self) -> Vec<&FactorSource> { - self.threshold_list_builder - .factors - .iter() - .chain(self.override_list_builder.factors.iter()) - .collect() - } - - fn add(&mut self, action: AddFactorSourceAction) -> Outcome { - match action.list_kind { - FactorListKind::Threshold => self.threshold_list_builder.add(action), - FactorListKind::Override => self.override_list_builder.add(action), - } - } - - fn remove(&mut self, action: RemoveFactorSourceAction) -> Outcome { - let Some(list) = self.list_of_factor_source(&action.factor_source) else { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::UnableToRemoveFactorNotFound, - ), - )); - }; - match list { - FactorListKind::Threshold => self.threshold_list_builder.remove(action), - FactorListKind::Override => self.override_list_builder.remove(action), - } - } - - fn set_threshold(&mut self, action: SetThresholdAction) -> Outcome { - self.threshold_list_builder.set_threshold(action) - } - - fn check_support(&self, action: CheckSupportForFactorSourceOfKindAction) -> Outcome { - match action.list_kind { - FactorListKind::Override => self.override_list_builder.check_support(action), - FactorListKind::Threshold => self.threshold_list_builder.check_support(action), - } - } - - fn validate(&self) -> Outcome { - self.validate_threshold_in_isolation()?; - self.validate_override_in_isolation()?; - self.validate_combination_of_lists() - } -} - -impl RoleBuilder { - fn validate_threshold_in_isolation(&self) -> Outcome { - self.threshold_list_builder.validate() - } - - fn validate_override_in_isolation(&self) -> Outcome { - self.override_list_builder.validate() - } - - fn validate_combination_of_lists(&self) -> Outcome { - self.role_kind.validate_combination_of_lists( - self.threshold_list_builder.threshold, - self.threshold_list_builder.factors(), - self.override_list_builder.factors(), - ) - } -} - -impl RoleKind { - fn validate_combination_of_lists( - &self, - _threshold: u8, - threshold_factors: Vec<&FactorSource>, - override_factors: Vec<&FactorSource>, - ) -> Outcome { - let threshold_set = HashSet::<&FactorSource>::from_iter(threshold_factors.iter().cloned()); - if threshold_set.len() != threshold_factors.len() { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::DuplicateFactorsFoundInThresholdList, - ), - )); - } - let override_set = HashSet::<&FactorSource>::from_iter(override_factors.iter().cloned()); - if override_set.len() != override_factors.len() { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::DuplicateFactorsFoundInOverrideList, - ), - )); - } - let intersection = threshold_set - .intersection(&override_set) - .collect::>(); - if !intersection.is_empty() { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::InternalStateError( - InternalStateFailure::FactorCannotBePresentInBothOverrideAndThreshold, - ), - )); - } - Ok(()) - } - - fn validate_primary_threshold_in_isolation( - &self, - threshold: u8, - threshold_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Primary); - - if threshold as usize > threshold_factors.len() { - return Outcome::Err(FactorValidation::Action( - FactorActionValidation::NotYetValid(ValidIf::ThresholdDecreased), - )); - } - - Ok(()) - } - - fn validate_recovery_threshold_in_isolation( - &self, - threshold: u8, - threshold_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Recovery); - if threshold > 0 { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::UserInterfaceShouldHavePrevented( - InvalidStateFailure::ThresholdNotSupportedForRecovery, - ), - )); - } - if !threshold_factors.is_empty() { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::UserInterfaceShouldHavePrevented( - InvalidStateFailure::ThresholdFactorsNotSupportedForRecovery, - ), - )); - } - todo!() - } - - fn validate_confirmation_threshold_in_isolation( - &self, - threshold: u8, - threshold_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Confirmation); - if threshold > 0 { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::UserInterfaceShouldHavePrevented( - InvalidStateFailure::ThresholdNotSupportedForConfirmation, - ), - )); - } - if !threshold_factors.is_empty() { - return Outcome::Err(FactorValidation::Basic( - BasicFactorInteractionValidation::UserInterfaceShouldHavePrevented( - InvalidStateFailure::ThresholdFactorsNotSupportedForConfirmation, - ), - )); - } - Ok(()) - } - - fn validate_threshold_in_isolation( - &self, - threshold: u8, - threshold_factors: Vec<&FactorSource>, - ) -> Outcome { - match self { - Self::Primary => { - self.validate_primary_threshold_in_isolation(threshold, threshold_factors) - } - Self::Recovery => { - self.validate_recovery_threshold_in_isolation(threshold, threshold_factors) - } - Self::Confirmation => { - self.validate_confirmation_threshold_in_isolation(threshold, threshold_factors) - } - } - } - - fn validate_primary_override_in_isolation( - &self, - override_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Confirmation); - todo!() - } - - fn validate_recovery_override_in_isolation( - &self, - override_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Confirmation); - todo!() - } - - fn validate_confirmation_override_in_isolation( - &self, - override_factors: Vec<&FactorSource>, - ) -> Outcome { - assert_eq!(*self, Self::Confirmation); - todo!() - } - - fn validate_override_in_isolation(&self, override_factors: Vec<&FactorSource>) -> Outcome { - match self { - Self::Primary => self.validate_primary_override_in_isolation(override_factors), - Self::Recovery => self.validate_recovery_override_in_isolation(override_factors), - Self::Confirmation => { - self.validate_confirmation_override_in_isolation(override_factors) - } - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct RoleWithFactors { - __hidden: (), - pub threshold: u8, - pub threshold_factors: Vec, - pub override_factors: Vec, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -struct MatrixOfFactorSources { - __hidden: (), - pub primary: RoleWithFactors, - pub recovery: RoleWithFactors, - pub confirmation: RoleWithFactors, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -struct MatrixBuilder { - primary_builder: RoleBuilder, - recovery_builder: RoleBuilder, - confirmation_builder: RoleBuilder, -} -impl MatrixBuilder { - fn new() -> Self { - Self { - primary_builder: RoleBuilder::new(RoleKind::Primary), - recovery_builder: RoleBuilder::new(RoleKind::Recovery), - confirmation_builder: RoleBuilder::new(RoleKind::Confirmation), - } - } - - pub fn build(self) -> BuildMatrixOutcome { - self.validate()?; - let primary = self.primary_builder.build()?; - let recovery = self.recovery_builder.build()?; - let confirmation = self.confirmation_builder.build()?; - let validated = MatrixOfFactorSources { - __hidden: (), - primary, - recovery, - confirmation, - }; - Ok(validated) - } -} - -impl FactorListBuilder for MatrixBuilder { - fn factors(&self) -> Vec<&FactorSource> { - self.primary_builder - .factors() - .iter() - .chain(self.recovery_builder.factors().iter()) - .chain(self.confirmation_builder.factors().iter()) - .cloned() - .collect() - } - - fn add(&mut self, action: AddFactorSourceAction) -> Outcome { - match action.role_kind { - RoleKind::Primary => self.primary_builder.add(action), - RoleKind::Recovery => self.recovery_builder.add(action), - RoleKind::Confirmation => self.confirmation_builder.add(action), - } - } - - fn remove(&mut self, action: RemoveFactorSourceAction) -> Outcome { - match action.role_kind { - RoleKind::Primary => self.primary_builder.remove(action), - RoleKind::Recovery => self.recovery_builder.remove(action), - RoleKind::Confirmation => self.confirmation_builder.remove(action), - } - } - - fn set_threshold(&mut self, action: SetThresholdAction) -> Outcome { - match action.role_kind { - RoleKind::Primary => self.primary_builder.set_threshold(action), - RoleKind::Recovery => self.recovery_builder.set_threshold(action), - RoleKind::Confirmation => self.confirmation_builder.set_threshold(action), - } - } - - fn check_support(&self, action: CheckSupportForFactorSourceOfKindAction) -> Outcome { - match action.role_kind { - RoleKind::Primary => self.primary_builder.check_support(action), - RoleKind::Recovery => self.recovery_builder.check_support(action), - RoleKind::Confirmation => self.confirmation_builder.check_support(action), - } - } - - fn validate(&self) -> Outcome { - self.validate_primary_in_isolation()?; - self.validate_recovery_in_isolation()?; - self.validate_confirmation_in_isolation()?; - - self.validate_combination_of_roles() - } -} - -impl MatrixBuilder { - fn validate_primary_in_isolation(&self) -> Outcome { - self.primary_builder.validate() - } - - fn validate_recovery_in_isolation(&self) -> Outcome { - self.recovery_builder.validate() - } - - fn validate_confirmation_in_isolation(&self) -> Outcome { - self.confirmation_builder.validate() - } - - fn validate_combination_of_roles(&self) -> Outcome { - todo!() - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn primary_device_in_threshold_once_ok_twice_not() { - let mut sut = MatrixBuilder::new(); - let action = AddFactorSourceAction::new( - RoleKind::Primary, - FactorSource::sample_device(), - FactorListKind::Threshold, - ); - assert!(matches!(sut.add(action.clone()), Ok(()))); - assert!(matches!(sut.add(action.clone()), Err(_))); - } -} diff --git a/crates/generics/Cargo.toml b/crates/generics/Cargo.toml deleted file mode 100644 index 006155aa..00000000 --- a/crates/generics/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "generics" -version = "0.1.0" -edition = "2021" - -[dependencies] -enum-as-inner = "0.6.1" -thiserror = { workspace = true } diff --git a/crates/generics/src/lib.rs b/crates/generics/src/lib.rs deleted file mode 100644 index 7a222667..00000000 --- a/crates/generics/src/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![feature(associated_type_defaults)] -#![allow(incomplete_features)] -#![feature(inherent_associated_types)] - -mod rules; - -pub mod prelude { - - pub(crate) use crate::rules::*; - pub(crate) use std::{borrow::Borrow, marker::PhantomData}; - pub(crate) use thiserror::Error as ThisError; -} -#[allow(unused_imports)] -pub use prelude::*; diff --git a/crates/generics/src/rules/factor_rules_violation.rs b/crates/generics/src/rules/factor_rules_violation.rs deleted file mode 100644 index 114ab337..00000000 --- a/crates/generics/src/rules/factor_rules_violation.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![allow(dead_code)] - -use crate::prelude::*; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum FactorRulesViolation { - NotYetValid(E), - ForeverInvalid(E), -} - -pub trait CanBeNotYetValid { - fn not_yet_valid(violation: E) -> Self; -} - -pub trait CanBeForeverInvalid { - fn forever_invalid(violation: E) -> Self; -} - -pub type FactorBuilderResult = std::result::Result<(), FactorRulesViolation>; - -impl CanBeNotYetValid for std::result::Result> { - fn not_yet_valid(violation: E) -> Self { - std::result::Result::Err(FactorRulesViolation::NotYetValid(violation)) - } -} -impl CanBeForeverInvalid for std::result::Result> { - fn forever_invalid(violation: E) -> Self { - std::result::Result::Err(FactorRulesViolation::ForeverInvalid(violation)) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, ThisError)] -pub enum PrimaryRoleViolation { - // ====== FOREVER INVALID ======= - #[error("More than one device factor is not supported")] - MoreThanOneDeviceFactorIsNotSupported, - - // ====== NOT YET VALID ======= - #[error("Password not allowed in override list since it would be alone")] - PasswordNotAllowedInOverrideListSinceItWouldBeAlone, - - #[error("Password must not be alone")] - PasswordMustNotBeAlone, - - #[error("Password requires a threshold of at at least two")] - PasswordRequiresThresholdOfAtLeastTwo, -} diff --git a/crates/generics/src/rules/has_rule_set_for_role.rs b/crates/generics/src/rules/has_rule_set_for_role.rs deleted file mode 100644 index f50cf46a..00000000 --- a/crates/generics/src/rules/has_rule_set_for_role.rs +++ /dev/null @@ -1,150 +0,0 @@ -use crate::prelude::*; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum FactorRulesFailure { - Specific(E), - - Basic(FactorRulesFailureBasic), -} - -#[derive(Clone, Debug, PartialEq, Eq, ThisError)] -pub enum FactorRulesFailureBasic { - #[error("Threshold may not be larger than the number of factors in the threshold list")] - ThresholdTooLarge, - - #[error("Threshold factors not allowed")] - ThresholdFactorsNotAllowed, -} - -pub trait IntoRulesViolation { - fn into_rules_violation(self) -> std::result::Result<(), FactorRulesFailure>; -} - -impl IntoRulesViolation for FactorBuilderResult { - fn into_rules_violation(self) -> std::result::Result<(), FactorRulesFailure> { - match self { - Ok(()) => Ok(()), - Err(FactorRulesViolation::NotYetValid(e)) => Err(FactorRulesFailure::Specific(e)), - Err(FactorRulesViolation::ForeverInvalid(e)) => Err(FactorRulesFailure::Specific(e)), - } - } -} - -pub trait IntoRulesFailure { - fn into_rules_failure( - self, - ) -> std::result::Result<(), FactorRulesFailure>>; -} - -impl IntoRulesFailure for FactorBuilderResult { - fn into_rules_failure( - self, - ) -> std::result::Result<(), FactorRulesFailure>> { - self.map_err(FactorRulesFailure::Specific) - } -} - -pub trait BasicFailure { - fn basic_failure(failure: FactorRulesFailureBasic) -> Self; -} -impl BasicFailure - for std::result::Result<(), FactorRulesFailure>> -{ - fn basic_failure(failure: FactorRulesFailureBasic) -> Self { - Self::Err(FactorRulesFailure::Basic(failure)) - } -} - -impl CanBeNotYetValid - for std::result::Result<(), FactorRulesFailure>> -{ - fn not_yet_valid(violation: N) -> Self { - Self::Err(FactorRulesFailure::Specific( - FactorRulesViolation::NotYetValid(violation), - )) - } -} -impl CanBeForeverInvalid - for std::result::Result<(), FactorRulesFailure>> -{ - fn forever_invalid(violation: F) -> Self { - Self::Err(FactorRulesFailure::Specific( - FactorRulesViolation::ForeverInvalid(violation), - )) - } -} - -pub trait HasRuleSetForRole: Sized + IsRole + Clone { - type Violation: std::fmt::Debug; - - /// Neither Recovery nor Confirmation roles can have factors added to their threshold - /// factor list. Only Primary supports it. - fn supports_threshold_factors() -> bool { - false - } - - fn validate_device_in_factors_list_of_kind( - context: &RoleWithFactorsBuilt, - list_kind: FactorListKind, - ) -> FactorBuilderResult; - - fn validate_password_in_factors_list_of_kind( - context: &RoleWithFactorsBuilt, - list_kind: FactorListKind, - ) -> FactorBuilderResult; - - fn validate_ledger_in_factors_list_of_kind( - context: &RoleWithFactorsBuilt, - list_kind: FactorListKind, - ) -> FactorBuilderResult; - - fn _do_validate( - context: &RoleWithFactorsBuilt, - validation: impl Fn( - &RoleWithFactorsBuilt, - FactorListKind, - ) -> FactorBuilderResult, - ) -> FactorBuilderResult { - validation(context, FactorListKind::Override)?; - if Self::supports_threshold_factors() { - validation(context, FactorListKind::Threshold)?; - } - Ok(()) - } - - fn validate_device( - context: &RoleWithFactorsBuilt, - ) -> FactorBuilderResult { - Self::_do_validate(context, Self::validate_device_in_factors_list_of_kind) - } - - fn validate_ledger( - context: &RoleWithFactorsBuilt, - ) -> FactorBuilderResult { - Self::_do_validate(context, Self::validate_ledger_in_factors_list_of_kind) - } - - fn validate_password( - context: &RoleWithFactorsBuilt, - ) -> FactorBuilderResult { - Self::_do_validate(context, Self::validate_password_in_factors_list_of_kind) - } - - fn validate( - context: &RoleWithFactorsBuilt, - ) -> std::result::Result<(), FactorRulesFailure>> { - if context.threshold > context.threshold_factors.len() as u8 { - return std::result::Result::< - (), - FactorRulesFailure>, - >::Err(FactorRulesFailure::Basic( - FactorRulesFailureBasic::ThresholdTooLarge, - )); - } - - Self::validate_device(context).into_rules_failure()?; - Self::validate_ledger(context).into_rules_failure()?; - Self::validate_password(context).into_rules_failure()?; - Ok(()) - } -} diff --git a/crates/generics/src/rules/is_role.rs b/crates/generics/src/rules/is_role.rs deleted file mode 100644 index 10d77620..00000000 --- a/crates/generics/src/rules/is_role.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::prelude::*; - -pub trait IsRole { - fn role_kind() -> RoleKind; -} - -pub trait IsRoleObjectSafe { - fn get_role_kind(&self) -> RoleKind; -} - -impl IsRoleObjectSafe for T { - fn get_role_kind(&self) -> RoleKind { - T::role_kind() - } -} diff --git a/crates/generics/src/rules/matrix.rs b/crates/generics/src/rules/matrix.rs deleted file mode 100644 index c5968e6f..00000000 --- a/crates/generics/src/rules/matrix.rs +++ /dev/null @@ -1,35 +0,0 @@ -#![allow(dead_code)] - -use crate::prelude::*; - -pub trait RoleWithFactors: IsRole {} - -pub struct MatrixBuilderOrBuilt -where - P: RoleWithFactors, - R: RoleWithFactors, - C: RoleWithFactors, -{ - /// This will be `()` for types `Matrix` which have been built - /// from a MatrixBuilder - built_type: PhantomData, - pub primary_role: P, - pub recovery_role: R, - pub confirmation_role: C, -} - -pub type Matrix = MatrixBuilderOrBuilt; -pub type MatrixBuilder = MatrixBuilderOrBuilt>; - - -impl MatrixBuilderOrBuilt -where - P: RoleWithFactors, - R: RoleWithFactors, - C: RoleWithFactors, - B: TyEq>, -{ - pub fn build(self) -> Result { - todo!() - } -} diff --git a/crates/generics/src/rules/mod.rs b/crates/generics/src/rules/mod.rs deleted file mode 100644 index da259995..00000000 --- a/crates/generics/src/rules/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -mod factor_rules_violation; -mod has_rule_set_for_role; -mod is_role; -// mod matrix; -mod primary_role_with_factor_sources_builder; -mod role_builder_or_built; -mod role_kind; -mod role_tags; -mod sargon_types; - -pub use factor_rules_violation::*; -pub use has_rule_set_for_role::*; -pub use is_role::*; -// pub use matrix::*; -#[allow(unused_imports)] -pub use primary_role_with_factor_sources_builder::*; -pub use role_builder_or_built::*; -pub use role_kind::*; -#[allow(unused_imports)] -pub use role_tags::*; -pub use sargon_types::*; diff --git a/crates/generics/src/rules/primary_role_with_factor_sources_builder.rs b/crates/generics/src/rules/primary_role_with_factor_sources_builder.rs deleted file mode 100644 index f335a942..00000000 --- a/crates/generics/src/rules/primary_role_with_factor_sources_builder.rs +++ /dev/null @@ -1,147 +0,0 @@ -#![allow(dead_code)] - -use crate::prelude::*; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct PrimaryRoleWithFactorSourceRuleSet; -impl IsRole for PrimaryRoleWithFactorSourceRuleSet { - fn role_kind() -> RoleKind { - RoleKind::Primary - } -} - -impl HasRuleSetForRole for PrimaryRoleWithFactorSourceRuleSet { - type Violation = PrimaryRoleViolation; - - fn supports_threshold_factors() -> bool { - true - } - - fn validate_device_in_factors_list_of_kind( - context: &RoleWithFactorsBuilt, - _list_kind: FactorListKind, - ) -> FactorBuilderResult { - let kind = FactorSourceKind::Device; - if context.count_factors_of_kind_in_any_list(kind) > 1 { - return Err(FactorRulesViolation::ForeverInvalid( - Self::Violation::MoreThanOneDeviceFactorIsNotSupported, - )); - } - Ok(()) - } - - fn validate_password_in_factors_list_of_kind( - context: &RoleWithFactorsBuilt, - list_kind: FactorListKind, - ) -> FactorBuilderResult { - let kind = FactorSourceKind::Password; - - if context.has_factor_of_kind_in_list_of_kind(kind, list_kind) { - if list_kind == FactorListKind::Override { - return FactorBuilderResult::forever_invalid( - PrimaryRoleViolation::PasswordNotAllowedInOverrideListSinceItWouldBeAlone, - ); - } - - if !context.has_factor_not_of_kind_in_list_of_kind(kind, list_kind) { - return FactorBuilderResult::not_yet_valid( - PrimaryRoleViolation::PasswordMustNotBeAlone, - ); - } - - if list_kind == FactorListKind::Threshold && context.threshold < 2 { - return FactorBuilderResult::not_yet_valid( - PrimaryRoleViolation::PasswordRequiresThresholdOfAtLeastTwo, - ); - } - } - Ok(()) - } - - fn validate_ledger_in_factors_list_of_kind( - _context: &RoleWithFactorsBuilt, - _list_kind: FactorListKind, - ) -> FactorBuilderResult { - Ok(()) // No restrictions - } -} - -pub type PrimaryRoleWithFactorSourcesBuilder = - RoleWithFactorsBuilder; - -#[cfg(test)] -mod tests { - use super::*; - - type F = FactorSource; - type SUT = PrimaryRoleWithFactorSourcesBuilder; - type MutResult = SUT::MutateResult; - type E = PrimaryRoleViolation; - - fn forever_invalid(violation: E) -> MutResult { - MutResult::forever_invalid(violation) - } - - fn basic_failure(e: FactorRulesFailureBasic) -> MutResult { - MutResult::basic_failure(e) - } - - #[cfg(test)] - mod threshold_tests { - use super::*; - - fn test(threshold: u8, arrange: impl Fn(&mut SUT)) { - let mut sut = SUT::new(); - arrange(&mut sut); - assert_eq!( - sut.set_threshold(threshold), - basic_failure(FactorRulesFailureBasic::ThresholdTooLarge) - ) - } - - #[test] - fn threshold_cannot_be_larger_than_threshold_factor_count_1_gt_0() { - test(1, |_| {}); - } - - #[test] - fn threshold_cannot_be_larger_than_threshold_factor_count_2_gt_1() { - test(2, |sut| { - sut.add_to_threshold(&F::sample_device()).unwrap(); - }) - } - - #[test] - fn threshold_cannot_be_larger_than_threshold_factor_count_9_gt_2() { - test(9, |sut| { - sut.add_to_threshold(&F::sample_device()).unwrap(); - sut.add_to_threshold(&F::sample_ledger()).unwrap(); - }) - } - } - - #[cfg(test)] - mod general { - use super::*; - - #[test] - fn single_device_in_threshold_is_ok() { - let mut sut = SUT::new(); - - let f = F::sample_device(); - sut.add_to_threshold(&f).unwrap(); - let built = sut.build().unwrap(); - assert_eq!(built.threshold_factors, vec![f]) - } - - #[test] - fn add_password_to_override_then_build() { - let mut sut = SUT::new(); - let f = F::sample_password(); - assert_eq!( - sut.add_to_override(&f), - forever_invalid(E::PasswordNotAllowedInOverrideListSinceItWouldBeAlone) - ); - } - } -} diff --git a/crates/generics/src/rules/role_builder_or_built.rs b/crates/generics/src/rules/role_builder_or_built.rs deleted file mode 100644 index 4f559375..00000000 --- a/crates/generics/src/rules/role_builder_or_built.rs +++ /dev/null @@ -1,241 +0,0 @@ -#![allow(dead_code)] - -use crate::prelude::*; - -pub trait TyEq {} -impl TyEq for A {} - -pub type RoleWithFactorsBuilder = RoleBuilderOrBuilt>; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct RoleBuilderOrBuilt { - role: PhantomData, - built_type: PhantomData, - pub threshold: u8, - pub threshold_factors: Vec, - pub override_factors: Vec, -} - -impl RoleWithFactorsBuilder -where - F: IsFactor, - R: HasRuleSetForRole + Clone, -{ - pub fn new() -> Self { - Self { - role: PhantomData, - built_type: PhantomData, - threshold: 0, - threshold_factors: Vec::new(), - override_factors: Vec::new(), - } - } - - pub type Built = RoleWithFactorsBuilt; - pub type MutateResult = - std::result::Result<(), FactorRulesFailure>>; - pub type BuildResult = - std::result::Result>>; - - pub fn build(self) -> Self::BuildResult { - self.validate()?; - Ok(self.snapshot()) - } - - fn snapshot(&self) -> Self::Built { - RoleWithFactorsBuilt::new( - self.threshold, - self.threshold_factors.clone(), - self.override_factors.clone(), - ) - } - - pub fn add_to_override(&mut self, factor: impl Borrow) -> Self::MutateResult { - self.add_to_list(factor, FactorListKind::Override) - } - - pub fn add_to_threshold(&mut self, factor: impl Borrow) -> Self::MutateResult { - self.add_to_list(factor, FactorListKind::Threshold) - } - - fn assert_supports_threshold(&self) -> Self::MutateResult { - if !R::supports_threshold_factors() { - return Self::MutateResult::Err( - FactorRulesFailure::>::Basic( - FactorRulesFailureBasic::ThresholdFactorsNotAllowed, - ), - ); - } - Ok(()) - } - - pub fn set_threshold(&mut self, threshold: u8) -> Self::MutateResult { - self.assert_supports_threshold()?; - - let mut simulation = self.clone(); - simulation.threshold = threshold; - simulation.validate() - } - - fn validate(&self) -> Self::MutateResult { - R::validate(&self.snapshot()) - } - - fn violation_if_add_factor_to_list_of_kind( - &self, - factor: &F, - list_kind: FactorListKind, - ) -> Self::MutateResult { - if list_kind == FactorListKind::Threshold { - self.assert_supports_threshold()?; - } - - let factor = factor.clone(); - let mut simulation = self.clone(); - match list_kind { - FactorListKind::Override => simulation.override_factors.push(factor), - FactorListKind::Threshold => simulation.threshold_factors.push(factor), - } - - simulation.validate() - } - - fn add_to_list( - &mut self, - factor: impl Borrow, - list_kind: FactorListKind, - ) -> Self::MutateResult { - let factor = factor.borrow(); - let outcome = self.violation_if_add_factor_to_list_of_kind(factor, list_kind); - match outcome.as_ref() { - Err(FactorRulesFailure::>::Basic(_)) => { - return outcome - } - Err(FactorRulesFailure::>::Specific( - FactorRulesViolation::ForeverInvalid(_), - )) => return outcome, - Err(FactorRulesFailure::>::Specific( - FactorRulesViolation::NotYetValid(_), - )) => { - // lets try to make it valid. - } - Ok(_) => {} - } - let factor = factor.clone(); - match list_kind { - FactorListKind::Override => self.override_factors.push(factor), - FactorListKind::Threshold => self.threshold_factors.push(factor), - } - Self::MutateResult::Ok(()) - } -} - -pub type RoleWithFactorsBuilt = RoleBuilderOrBuilt; - -impl RoleWithFactorsBuilt -where - F: IsFactor, - R: IsRoleObjectSafe + Clone, -{ - pub(super) fn new(threshold: u8, threshold_factors: Vec, override_factors: Vec) -> Self { - Self { - role: PhantomData, - built_type: PhantomData, - threshold, - threshold_factors, - override_factors, - } - } - - pub(crate) fn has_factor_of_kind_in_any_list( - &self, - factor_source_kind: FactorSourceKind, - ) -> bool { - self.has_factor_of_kind_in_list_of_kind(factor_source_kind, None) - } - - pub(crate) fn factors_in_list_of_kind( - &self, - list_kind: impl Into>, - ) -> Vec<&F> { - match list_kind.into() { - Some(FactorListKind::Threshold) => self.threshold_factors.iter().collect(), - Some(FactorListKind::Override) => self.override_factors.iter().collect(), - None => self - .threshold_factors - .iter() - .chain(self.override_factors.iter()) - .collect(), - } - } - - pub(crate) fn has_factor_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> bool { - !self - .factors_of_kind_in_list_of_kind(factor_source_kind, list_kind) - .is_empty() - } - - pub(crate) fn has_factor_not_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> bool { - !self - .factors_not_of_kind_in_list_of_kind(factor_source_kind, list_kind) - .is_empty() - } - - pub(crate) fn count_factors_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> usize { - self.factors_of_kind_in_list_of_kind(factor_source_kind, list_kind) - .len() - } - - pub(crate) fn count_factors_of_kind_in_any_list( - &self, - factor_source_kind: FactorSourceKind, - ) -> usize { - self.factors_of_kind_in_list_of_kind(factor_source_kind, None) - .len() - } - - pub(crate) fn count_factors_not_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> usize { - self.factors_not_of_kind_in_list_of_kind(factor_source_kind, list_kind) - .len() - } - - pub(crate) fn factors_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> Vec<&F> { - let factors = self.factors_in_list_of_kind(list_kind); - factors - .into_iter() - .filter(|f| f.get_factor_source_kind() == factor_source_kind) - .collect() - } - - pub(crate) fn factors_not_of_kind_in_list_of_kind( - &self, - factor_source_kind: FactorSourceKind, - list_kind: impl Into>, - ) -> Vec<&F> { - let factors = self.factors_in_list_of_kind(list_kind); - factors - .into_iter() - .filter(|f| f.get_factor_source_kind() != factor_source_kind) - .collect() - } -} diff --git a/crates/generics/src/rules/role_kind.rs b/crates/generics/src/rules/role_kind.rs deleted file mode 100644 index ce44acc3..00000000 --- a/crates/generics/src/rules/role_kind.rs +++ /dev/null @@ -1,6 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum RoleKind { - Primary, - Recovery, - Confirmation, -} diff --git a/crates/generics/src/rules/role_tags.rs b/crates/generics/src/rules/role_tags.rs deleted file mode 100644 index 5ba4d7c1..00000000 --- a/crates/generics/src/rules/role_tags.rs +++ /dev/null @@ -1,48 +0,0 @@ -use crate::prelude::*; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct TagPrimaryRole; - -impl IsRole for TagPrimaryRole { - fn role_kind() -> RoleKind { - RoleKind::Primary - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct TagConfirmationRole; - -impl IsRole for TagConfirmationRole { - fn role_kind() -> RoleKind { - RoleKind::Confirmation - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct TagRecoveryRole; - -impl IsRole for TagRecoveryRole { - fn role_kind() -> RoleKind { - RoleKind::Recovery - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn is_primary_role() { - assert_eq!(TagPrimaryRole::role_kind(), RoleKind::Primary); - } - - #[test] - fn is_recovery_role() { - assert_eq!(TagRecoveryRole::role_kind(), RoleKind::Recovery); - } - - #[test] - fn is_confirmation_role() { - assert_eq!(TagConfirmationRole::role_kind(), RoleKind::Confirmation); - } -} diff --git a/crates/generics/src/rules/sargon_types.rs b/crates/generics/src/rules/sargon_types.rs deleted file mode 100644 index 908477ad..00000000 --- a/crates/generics/src/rules/sargon_types.rs +++ /dev/null @@ -1,130 +0,0 @@ -#![allow(dead_code)] -#![allow(unused_imports)] - -use crate::prelude::*; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum FactorSourceKind { - Device, - Ledger, - Password, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FactorSource { - label: String, - id: FactorSourceID, -} - -impl HasFactorSourceKindObjectSafe for FactorSourceID { - fn get_factor_source_kind(&self) -> FactorSourceKind { - self.kind - } -} - -impl HasFactorSourceKindObjectSafe for FactorInstance { - fn get_factor_source_kind(&self) -> FactorSourceKind { - self.factor_source_id.get_factor_source_kind() - } -} - -impl HasFactorSourceKindObjectSafe for FactorSource { - fn get_factor_source_kind(&self) -> FactorSourceKind { - self.id.get_factor_source_kind() - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FactorInstance { - factor_source_id: FactorSourceID, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct FactorSourceID { - hash: [u8; 32], - kind: FactorSourceKind, -} - -pub trait HasFactorSourceKind { - fn factor_source_kind() -> FactorSourceKind; -} - -pub trait HasFactorSourceKindObjectSafe { - fn get_factor_source_kind(&self) -> FactorSourceKind; -} - -impl HasFactorSourceKindObjectSafe for T { - fn get_factor_source_kind(&self) -> FactorSourceKind { - T::factor_source_kind() - } -} - -pub trait IsFactor: - HasFactorSourceKindObjectSafe + Clone + std::fmt::Debug + PartialEq + Eq -{ -} -impl IsFactor for T {} - -impl HasFactorSourceKindObjectSafe for FactorSourceKind { - fn get_factor_source_kind(&self) -> FactorSourceKind { - *self - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum FactorListKind { - Threshold, - Override, -} - -impl FactorSource { - fn new(kind: FactorSourceKind) -> Self { - Self { - label: "sample".to_owned(), - id: FactorSourceID { - hash: [0xaa; 32], - kind, - }, - } - } - fn other(kind: FactorSourceKind) -> Self { - Self { - label: "sample other".to_owned(), - id: FactorSourceID { - hash: [0xbb; 32], - kind, - }, - } - } - - pub fn sample_device() -> Self { - Self::new(FactorSourceKind::Device) - } - - pub fn sample_device_other() -> Self { - Self::other(FactorSourceKind::Device) - } - - pub fn sample_ledger() -> Self { - Self::new(FactorSourceKind::Ledger) - } - - pub fn sample_ledger_other() -> Self { - Self::other(FactorSourceKind::Ledger) - } - - pub fn sample_password() -> Self { - Self::new(FactorSourceKind::Password) - } - pub fn sample_password_other() -> Self { - Self::other(FactorSourceKind::Password) - } - - // pub fn sample_off_device_mnemonic() -> Self { - // Self::new(FactorSourceKind::OffDeviceMnemonic) - // } - - // pub fn sample_off_device_mnemonic_other() -> Self { - // Self::other(FactorSourceKind::OffDeviceMnemonic) - // } -} diff --git a/crates/wip/Cargo.toml b/crates/rules/Cargo.toml similarity index 91% rename from crates/wip/Cargo.toml rename to crates/rules/Cargo.toml index 9966ebbf..494ce873 100644 --- a/crates/wip/Cargo.toml +++ b/crates/rules/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "wip" +name = "rules" version = "0.1.0" edition = "2021" diff --git a/crates/wip/src/lib.rs b/crates/rules/src/lib.rs similarity index 100% rename from crates/wip/src/lib.rs rename to crates/rules/src/lib.rs diff --git a/crates/wip/src/roles_builder.rs b/crates/rules/src/roles_builder.rs similarity index 100% rename from crates/wip/src/roles_builder.rs rename to crates/rules/src/roles_builder.rs