@@ -21,6 +21,87 @@ use alloc::vec::Vec;
2121#[ cfg( feature = "serde" ) ]
2222use serde:: { Deserialize , Serialize } ;
2323
24+ /// Macro to look up an account by CoreAccountTypeMatch, parameterized by accessor methods
25+ macro_rules! get_by_account_type_match_impl {
26+ ( $self: expr, $match: expr, $get: ident, $as_opt: ident, $values: ident) => {
27+ match $match {
28+ CoreAccountTypeMatch :: StandardBIP44 {
29+ account_index,
30+ ..
31+ } => $self. standard_bip44_accounts. $get( account_index) ,
32+ CoreAccountTypeMatch :: StandardBIP32 {
33+ account_index,
34+ ..
35+ } => $self. standard_bip32_accounts. $get( account_index) ,
36+ CoreAccountTypeMatch :: CoinJoin {
37+ account_index,
38+ ..
39+ } => $self. coinjoin_accounts. $get( account_index) ,
40+ CoreAccountTypeMatch :: IdentityRegistration {
41+ ..
42+ } => $self. identity_registration. $as_opt( ) ,
43+ CoreAccountTypeMatch :: IdentityTopUp {
44+ account_index,
45+ ..
46+ } => $self. identity_topup. $get( account_index) ,
47+ CoreAccountTypeMatch :: IdentityTopUpNotBound {
48+ ..
49+ } => $self. identity_topup_not_bound. $as_opt( ) ,
50+ CoreAccountTypeMatch :: IdentityInvitation {
51+ ..
52+ } => $self. identity_invitation. $as_opt( ) ,
53+ CoreAccountTypeMatch :: ProviderVotingKeys {
54+ ..
55+ } => $self. provider_voting_keys. $as_opt( ) ,
56+ CoreAccountTypeMatch :: ProviderOwnerKeys {
57+ ..
58+ } => $self. provider_owner_keys. $as_opt( ) ,
59+ CoreAccountTypeMatch :: ProviderOperatorKeys {
60+ ..
61+ } => $self. provider_operator_keys. $as_opt( ) ,
62+ CoreAccountTypeMatch :: ProviderPlatformKeys {
63+ ..
64+ } => $self. provider_platform_keys. $as_opt( ) ,
65+ CoreAccountTypeMatch :: DashpayReceivingFunds {
66+ account_index,
67+ involved_addresses,
68+ } => $self. dashpay_receival_accounts. $values( ) . find( |account| {
69+ match & account. account_type {
70+ ManagedAccountType :: DashpayReceivingFunds {
71+ index,
72+ addresses,
73+ ..
74+ } => {
75+ * index == * account_index
76+ && involved_addresses
77+ . iter( )
78+ . any( |addr| addresses. contains_address( & addr. address) )
79+ }
80+ _ => false ,
81+ }
82+ } ) ,
83+ CoreAccountTypeMatch :: DashpayExternalAccount {
84+ account_index,
85+ involved_addresses,
86+ } => $self. dashpay_external_accounts. $values( ) . find( |account| {
87+ match & account. account_type {
88+ ManagedAccountType :: DashpayExternalAccount {
89+ index,
90+ addresses,
91+ ..
92+ } => {
93+ * index == * account_index
94+ && involved_addresses
95+ . iter( )
96+ . any( |addr| addresses. contains_address( & addr. address) )
97+ }
98+ _ => false ,
99+ }
100+ } ) ,
101+ }
102+ } ;
103+ }
104+
24105/// Collection of managed accounts organized by type
25106#[ derive( Debug , Clone , Default ) ]
26107#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
@@ -728,87 +809,15 @@ impl ManagedAccountCollection {
728809 & self ,
729810 account_type_match : & CoreAccountTypeMatch ,
730811 ) -> Option < & ManagedCoreAccount > {
731- match account_type_match {
732- CoreAccountTypeMatch :: StandardBIP44 {
733- account_index,
734- ..
735- } => self . standard_bip44_accounts . get ( account_index) ,
736- CoreAccountTypeMatch :: StandardBIP32 {
737- account_index,
738- ..
739- } => self . standard_bip32_accounts . get ( account_index) ,
740- CoreAccountTypeMatch :: CoinJoin {
741- account_index,
742- ..
743- } => self . coinjoin_accounts . get ( account_index) ,
744- CoreAccountTypeMatch :: IdentityRegistration {
745- ..
746- } => self . identity_registration . as_ref ( ) ,
747- CoreAccountTypeMatch :: IdentityTopUp {
748- account_index,
749- ..
750- } => self . identity_topup . get ( account_index) ,
751- CoreAccountTypeMatch :: IdentityTopUpNotBound {
752- ..
753- } => self . identity_topup_not_bound . as_ref ( ) ,
754- CoreAccountTypeMatch :: IdentityInvitation {
755- ..
756- } => self . identity_invitation . as_ref ( ) ,
757- CoreAccountTypeMatch :: ProviderVotingKeys {
758- ..
759- } => self . provider_voting_keys . as_ref ( ) ,
760- CoreAccountTypeMatch :: ProviderOwnerKeys {
761- ..
762- } => self . provider_owner_keys . as_ref ( ) ,
763- CoreAccountTypeMatch :: ProviderOperatorKeys {
764- ..
765- } => self . provider_operator_keys . as_ref ( ) ,
766- CoreAccountTypeMatch :: ProviderPlatformKeys {
767- ..
768- } => self . provider_platform_keys . as_ref ( ) ,
769- CoreAccountTypeMatch :: DashpayReceivingFunds {
770- account_index,
771- involved_addresses,
772- } => {
773- // Match by index and addresses since multiple accounts can share the same index
774- self . dashpay_receival_accounts . values ( ) . find ( |account| {
775- match & account. account_type {
776- ManagedAccountType :: DashpayReceivingFunds {
777- index,
778- addresses,
779- ..
780- } => {
781- * index == * account_index
782- && involved_addresses
783- . iter ( )
784- . any ( |addr| addresses. contains_address ( & addr. address ) )
785- }
786- _ => false ,
787- }
788- } )
789- }
790- CoreAccountTypeMatch :: DashpayExternalAccount {
791- account_index,
792- involved_addresses,
793- } => {
794- // Match by index and addresses since multiple accounts can share the same index
795- self . dashpay_external_accounts . values ( ) . find ( |account| {
796- match & account. account_type {
797- ManagedAccountType :: DashpayExternalAccount {
798- index,
799- addresses,
800- ..
801- } => {
802- * index == * account_index
803- && involved_addresses
804- . iter ( )
805- . any ( |addr| addresses. contains_address ( & addr. address ) )
806- }
807- _ => false ,
808- }
809- } )
810- }
811- }
812+ get_by_account_type_match_impl ! ( self , account_type_match, get, as_ref, values)
813+ }
814+
815+ /// Get a mutable account reference by AccountTypeMatch
816+ pub fn get_by_account_type_match_mut (
817+ & mut self ,
818+ account_type_match : & CoreAccountTypeMatch ,
819+ ) -> Option < & mut ManagedCoreAccount > {
820+ get_by_account_type_match_impl ! ( self , account_type_match, get_mut, as_mut, values_mut)
812821 }
813822
814823 /// Remove an account from the collection
0 commit comments