Skip to content

Commit

Permalink
Create SupportedCurves and OtherGateways through macro (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
micbakos-rdx authored Apr 15, 2024
1 parent 93bd334 commit 83a225b
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 70 deletions.
3 changes: 3 additions & 0 deletions src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ pub enum CommonError {

#[error("Submitted transaction was duplicate.")]
GatewaySubmitDuplicateTX { intent_hash: String } = 10119,

#[error("SupportedCurves must not be empty.")]
SupportedCurvesMustNotBeEmpty = 10120,
}

#[uniffi::export]
Expand Down
11 changes: 11 additions & 0 deletions src/core/types/keys/slip10_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::prelude::*;
PartialOrd,
Ord,
uniffi::Enum,
derive_more::Display,
)]
#[serde(rename_all = "camelCase")]
pub enum SLIP10Curve {
Expand All @@ -40,6 +41,16 @@ impl Identifiable for SLIP10Curve {
}
}

impl HasSampleValues for SLIP10Curve {
fn sample() -> Self {
Self::Curve25519
}

fn sample_other() -> Self {
Self::Secp256k1
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down
10 changes: 10 additions & 0 deletions src/profile/v100/app_preferences/gateways/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ impl Gateway {
}
}

impl HasSampleValues for Gateway {
fn sample() -> Self {
Gateway::mainnet()
}

fn sample_other() -> Self {
Gateway::stokenet()
}
}

#[uniffi::export]
pub fn gateway_mainnet() -> Gateway {
Gateway::mainnet()
Expand Down
18 changes: 14 additions & 4 deletions src/profile/v100/app_preferences/gateways/gateways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Gateways {
/// Other by user added or predefined Gateways the user can switch to.
/// It might be Gateways with different URLs on the SAME network, or
/// other networks, the identifier of a Gateway is the URL.
pub other: IdentifiedVecVia<Gateway>,
pub other: OtherGateways,
}

/// Constructs `Gateways` with `current` set as active Gateway.
Expand Down Expand Up @@ -103,15 +103,15 @@ impl Gateways {
pub fn new(current: Gateway) -> Self {
Self {
current,
other: IdentifiedVecVia::new(),
other: OtherGateways::default(),
}
}

pub fn new_with_other<I>(current: Gateway, other: I) -> Result<Self>
where
I: IntoIterator<Item = Gateway>,
{
let other = IdentifiedVecVia::from_iter(other);
let other = OtherGateways::from_iter(other);
if other.contains(&current) {
return Err(
CommonError::GatewaysDiscrepancyOtherShouldNotContainCurrent,
Expand Down Expand Up @@ -175,6 +175,16 @@ impl HasSampleValues for Gateways {
}
}

impl HasSampleValues for OtherGateways {
fn sample() -> Self {
OtherGateways::from_iter([Gateway::stokenet()])
}

fn sample_other() -> Self {
OtherGateways::from_iter([Gateway::stokenet(), Gateway::hammunet()])
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down Expand Up @@ -212,7 +222,7 @@ mod tests {
fn change_throw_gateways_discrepancy_other_should_not_contain_current() {
let mut impossible = Gateways {
current: Gateway::mainnet(),
other: IdentifiedVecVia::from_iter([Gateway::mainnet()]),
other: OtherGateways::from_iter([Gateway::mainnet()]),
};
assert_eq!(
impossible.change_current(Gateway::stokenet()),
Expand Down
14 changes: 14 additions & 0 deletions src/profile/v100/factors/factor_source_crypto_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ impl Default for FactorSourceCryptoParameters {
}
}

impl HasSampleValues for SupportedCurves {
fn sample() -> Self {
SupportedCurves::just(SLIP10Curve::Curve25519)
}

fn sample_other() -> Self {
SupportedCurves::from_iter([
SLIP10Curve::Curve25519,
SLIP10Curve::Secp256k1,
])
.unwrap()
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion src/profile/v100/factors/factor_sources/factor_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl FactorSources {
/// AND marked "main".
pub fn with_bdfs(device_factor_source: DeviceFactorSource) -> Self {
assert!(device_factor_source.is_main_bdfs());
Self::with_factor_source(device_factor_source.into())
Self::just(device_factor_source.into())
}
}

Expand Down
77 changes: 42 additions & 35 deletions src/profile/v100/identified_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ macro_rules! decl_identified_array_of {
$(
#[doc = $expr: expr]
)*
$element_type: ty,
$struct_type: ident,
$element_type: ty,
$collection_type: ty
) => {
paste! {
Expand Down Expand Up @@ -39,7 +39,7 @@ macro_rules! decl_identified_array_of {
}
}

impl [< $element_type s >] {
impl $struct_type {
/// Returns a reference to the element identified by `id`, if it exists.
pub fn [< get_ $element_type:snake _by_id>](
&self,
Expand Down Expand Up @@ -123,14 +123,14 @@ macro_rules! decl_identified_array_of {
}

/// Impl rules for identified_array_of implementations which can be empty
macro_rules! dec_can_be_empty_impl {
macro_rules! decl_can_be_empty_impl {
(
$element_type: ty,
$struct_type: ty,
$element_type: ty,
$secret_magic: ty
) => {
paste! {
impl [< $element_type s >] {
impl $struct_type {

#[allow(clippy::should_implement_trait)]
pub fn from_iter<I>([< $struct_type:lower >]: I) -> Self
Expand All @@ -142,23 +142,16 @@ macro_rules! dec_can_be_empty_impl {
}
}

pub fn [< with_ $struct_type:snake >]<I>([< $struct_type:snake >]: I) -> Self
where
I: IntoIterator<Item = $element_type>,
{
Self::from_iter([< $struct_type:snake >])
}

pub fn [< with_ $element_type:snake >]([< $element_type:snake >]: $element_type) -> Self {
Self::[< with_ $struct_type:snake >]([[< $element_type:snake >]])
pub fn just([< $element_type:snake >]: $element_type) -> Self {
Self::from_iter([[< $element_type:snake >]])
}
}

// Trait: Default
impl Default for $struct_type {
/// Instantiates a new empty collection.
fn default() -> Self {
Self::[< with_ $struct_type:snake >]([])
Self::from_iter([])
}
}

Expand Down Expand Up @@ -196,14 +189,14 @@ macro_rules! dec_can_be_empty_impl {
}

/// Impl rules for identified_array_of implementations which must not be empty
macro_rules! dec_never_empty_impl {
macro_rules! decl_never_empty_impl {
(
$element_type: ty,
$struct_type: ty,
$element_type: ty,
$secret_magic: ty
) => {
paste! {
impl [< $element_type s >] {
impl $struct_type {

#[allow(clippy::should_implement_trait)]
pub fn from_iter<I>([< $struct_type:snake >]: I) -> Result<Self>
Expand All @@ -220,15 +213,8 @@ macro_rules! dec_never_empty_impl {
}
}

pub fn [< with_ $struct_type:snake >]<I>([< $struct_type:snake >]: I) -> Result<Self>
where
I: IntoIterator<Item = $element_type>,
{
Self::from_iter([< $struct_type:snake >])
}

pub fn [< with_ $element_type:snake >]([< $element_type:snake >]: $element_type) -> Self {
Self::[< with_ $struct_type:snake >]([[< $element_type:snake >]]).unwrap()
pub fn just([< $element_type:snake >]: $element_type) -> Self {
Self::from_iter([[< $element_type:snake >]]).unwrap()
}
}

Expand Down Expand Up @@ -267,22 +253,23 @@ macro_rules! decl_can_be_empty_identified_array_of {
$(
#[doc = $expr: expr]
)*
$struct_type: ty,
$element_type: ty
) => {
paste! {
decl_identified_array_of!(
$(
#[doc = $expr]
)*
$struct_type,
$element_type,
[< $element_type s >],
IdentifiedVecVia<$element_type>
);

dec_can_be_empty_impl!(
decl_can_be_empty_impl!(
$struct_type,
$element_type,
[< $element_type s >],
[< $element_type s SecretMagic >]
[< $struct_type SecretMagic >]
);
}
};
Expand All @@ -293,22 +280,23 @@ macro_rules! decl_never_empty_identified_array_of {
$(
#[doc = $expr: expr]
)*
$struct_type: ty,
$element_type: ty
) => {
paste! {
decl_identified_array_of!(
$(
#[doc = $expr]
)*
$struct_type,
$element_type,
[< $element_type s >],
IdentifiedVecVia<$element_type>
);

dec_never_empty_impl!(
decl_never_empty_impl!(
$struct_type,
$element_type,
[< $element_type s >],
[< $element_type s SecretMagic >]
[< $struct_type SecretMagic >]
);
}
};
Expand All @@ -318,21 +306,40 @@ decl_can_be_empty_identified_array_of!(
/// An ordered set of [`Account`]s on a specific network, most commonly
/// the set is non-empty, since wallets guide user to create a first
/// Account.
Accounts,
Account
);

decl_can_be_empty_identified_array_of!(
/// An ordered set of [`Persona`]s on a specific network.
Personas,
Persona
);

decl_can_be_empty_identified_array_of!(
/// An ordered set of ['AuthorizedDapp`]s on a specific network.
AuthorizedDapps,
AuthorizedDapp
);

decl_never_empty_identified_array_of!(
/// A collection of [`FactorSource`]s generated by a wallet or manually added by user.
/// MUST never be empty.
FactorSources,
FactorSource
);

decl_can_be_empty_identified_array_of!(
/// Other by user added or predefined Gateways the user can switch to.
/// It might be Gateways with different URLs on the SAME network, or
/// other networks, the identifier of a Gateway is the URL.
OtherGateways,
Gateway
);

decl_never_empty_identified_array_of!(
/// A collection of [`SLIP10Curve`]s that a factor source supports.
/// MUST never be empty.
SupportedCurves,
SLIP10Curve
);
14 changes: 6 additions & 8 deletions src/profile/v100/networks/network/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ impl HasSampleValues for Accounts {
impl Accounts {
/// A sample used to facilitate unit tests.
pub fn sample_mainnet() -> Self {
Self::with_accounts([
Self::from_iter([
Account::sample_mainnet(),
Account::sample_mainnet_other(),
])
}

/// A sample used to facilitate unit tests.
pub fn sample_stokenet() -> Self {
Self::with_accounts([
Self::from_iter([
Account::sample_stokenet_nadia(),
Account::sample_stokenet_olivia(),
])
Expand Down Expand Up @@ -58,17 +58,15 @@ mod tests {
#[test]
fn duplicates_are_prevented() {
assert_eq!(
SUT::with_accounts(
[Account::sample(), Account::sample()].into_iter()
)
.len(),
SUT::from_iter([Account::sample(), Account::sample()].into_iter())
.len(),
1
)
}

#[test]
fn with_one() {
assert_eq!(SUT::with_account(Account::sample()).len(), 1)
assert_eq!(SUT::just(Account::sample()).len(), 1)
}

#[test]
Expand All @@ -84,7 +82,7 @@ mod tests {
DisplayName::default(),
AppearanceID::default(),
);
let accounts = SUT::with_account(account.clone());
let accounts = SUT::just(account.clone());
assert_eq!(accounts.get_account_by_id(&address), Some(&account));
}

Expand Down
Loading

0 comments on commit 83a225b

Please sign in to comment.