11use {
22 super :: { account_state:: AccountState , COption , Initializable , Transmutable } ,
3- pinocchio:: pubkey:: Pubkey ,
3+ pinocchio:: { program_error :: ProgramError , pubkey:: Pubkey } ,
44} ;
55
66/// Incinerator address.
@@ -27,7 +27,7 @@ pub struct Account {
2727 delegate : COption < Pubkey > ,
2828
2929 /// The account's state.
30- pub state : AccountState ,
30+ state : u8 ,
3131
3232 /// Indicates whether this account represents a native token or not.
3333 is_native : [ u8 ; 4 ] ,
@@ -46,6 +46,16 @@ pub struct Account {
4646}
4747
4848impl Account {
49+ #[ inline( always) ]
50+ pub fn set_account_state ( & mut self , state : AccountState ) {
51+ self . state = state as u8 ;
52+ }
53+
54+ #[ inline( always) ]
55+ pub fn account_state ( & self ) -> Result < AccountState , ProgramError > {
56+ AccountState :: try_from ( self . state )
57+ }
58+
4959 #[ inline( always) ]
5060 pub fn set_amount ( & mut self , amount : u64 ) {
5161 self . amount = amount. to_le_bytes ( ) ;
@@ -68,11 +78,11 @@ impl Account {
6878 }
6979
7080 #[ inline( always) ]
71- pub fn delegate ( & self ) -> Option < & Pubkey > {
72- if self . delegate . 0 [ 0 ] == 1 {
73- Some ( & self . delegate . 1 )
74- } else {
75- None
81+ pub fn delegate ( & self ) -> Result < Option < & Pubkey > , ProgramError > {
82+ match self . delegate . 0 {
83+ [ 0 , 0 , 0 , 0 ] => Ok ( None ) ,
84+ [ 1 , 0 , 0 , 0 ] => Ok ( Some ( & self . delegate . 1 ) ) ,
85+ _ => Err ( ProgramError :: InvalidAccountData ) ,
7686 }
7787 }
7888
@@ -122,17 +132,17 @@ impl Account {
122132 }
123133
124134 #[ inline( always) ]
125- pub fn close_authority ( & self ) -> Option < & Pubkey > {
126- if self . close_authority . 0 [ 0 ] == 1 {
127- Some ( & self . close_authority . 1 )
128- } else {
129- None
135+ pub fn close_authority ( & self ) -> Result < Option < & Pubkey > , ProgramError > {
136+ match self . close_authority . 0 {
137+ [ 0 , 0 , 0 , 0 ] => Ok ( None ) ,
138+ [ 1 , 0 , 0 , 0 ] => Ok ( Some ( & self . close_authority . 1 ) ) ,
139+ _ => Err ( ProgramError :: InvalidAccountData ) ,
130140 }
131141 }
132142
133143 #[ inline( always) ]
134- pub fn is_frozen ( & self ) -> bool {
135- self . state == AccountState :: Frozen
144+ pub fn is_frozen ( & self ) -> Result < bool , ProgramError > {
145+ Ok ( AccountState :: try_from ( self . state ) ? == AccountState :: Frozen )
136146 }
137147
138148 #[ inline( always) ]
@@ -147,7 +157,7 @@ impl Transmutable for Account {
147157
148158impl Initializable for Account {
149159 #[ inline( always) ]
150- fn is_initialized ( & self ) -> bool {
151- self . state != AccountState :: Uninitialized
160+ fn is_initialized ( & self ) -> Result < bool , ProgramError > {
161+ Ok ( AccountState :: try_from ( self . state ) ? != AccountState :: Uninitialized )
152162 }
153163}
0 commit comments