Browse Source

lang: Add fmt::Debug to structs (#1043)

Kirill Fomichev 3 years ago
parent
commit
632aefa250

+ 12 - 0
lang/src/account.rs

@@ -5,6 +5,7 @@ use solana_program::entrypoint::ProgramResult;
 use solana_program::instruction::AccountMeta;
 use solana_program::program_error::ProgramError;
 use solana_program::pubkey::Pubkey;
+use std::fmt;
 use std::ops::{Deref, DerefMut};
 
 /// Account container that checks ownership on deserialization.
@@ -14,6 +15,17 @@ pub struct Account<'info, T: AccountSerialize + AccountDeserialize + Owner + Clo
     info: AccountInfo<'info>,
 }
 
+impl<'info, T: AccountSerialize + AccountDeserialize + Owner + Clone + fmt::Debug> fmt::Debug
+    for Account<'info, T>
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Account")
+            .field("account", &self.account)
+            .field("info", &self.info)
+            .finish()
+    }
+}
+
 impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T> {
     fn new(info: AccountInfo<'a>, account: T) -> Account<'a, T> {
         Self { info, account }

+ 11 - 0
lang/src/context.rs

@@ -2,6 +2,7 @@ use crate::{Accounts, ToAccountInfos, ToAccountMetas};
 use solana_program::account_info::AccountInfo;
 use solana_program::instruction::AccountMeta;
 use solana_program::pubkey::Pubkey;
+use std::fmt;
 
 /// Provides non-argument inputs to the program.
 pub struct Context<'a, 'b, 'c, 'info, T> {
@@ -14,6 +15,16 @@ pub struct Context<'a, 'b, 'c, 'info, T> {
     pub remaining_accounts: &'c [AccountInfo<'info>],
 }
 
+impl<'a, 'b, 'c, 'info, T: fmt::Debug> fmt::Debug for Context<'a, 'b, 'c, 'info, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Context")
+            .field("program_id", &self.program_id)
+            .field("accounts", &self.accounts)
+            .field("remaining_accounts", &self.remaining_accounts)
+            .finish()
+    }
+}
+
 impl<'a, 'b, 'c, 'info, T: Accounts<'info>> Context<'a, 'b, 'c, 'info, T> {
     pub fn new(
         program_id: &'a Pubkey,

+ 10 - 0
lang/src/loader.rs

@@ -9,6 +9,7 @@ use solana_program::instruction::AccountMeta;
 use solana_program::program_error::ProgramError;
 use solana_program::pubkey::Pubkey;
 use std::cell::{Ref, RefMut};
+use std::fmt;
 use std::io::Write;
 use std::marker::PhantomData;
 use std::ops::DerefMut;
@@ -29,6 +30,15 @@ pub struct Loader<'info, T: ZeroCopy> {
     phantom: PhantomData<&'info T>,
 }
 
+impl<'info, T: ZeroCopy + fmt::Debug> fmt::Debug for Loader<'info, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Loader")
+            .field("acc_info", &self.acc_info)
+            .field("phantom", &self.phantom)
+            .finish()
+    }
+}
+
 impl<'info, T: ZeroCopy> Loader<'info, T> {
     fn new(acc_info: AccountInfo<'info>) -> Loader<'info, T> {
         Self {

+ 10 - 0
lang/src/loader_account.rs

@@ -9,6 +9,7 @@ use solana_program::instruction::AccountMeta;
 use solana_program::program_error::ProgramError;
 use solana_program::pubkey::Pubkey;
 use std::cell::{Ref, RefMut};
+use std::fmt;
 use std::io::Write;
 use std::marker::PhantomData;
 use std::ops::DerefMut;
@@ -30,6 +31,15 @@ pub struct AccountLoader<'info, T: ZeroCopy + Owner> {
     phantom: PhantomData<&'info T>,
 }
 
+impl<'info, T: ZeroCopy + Owner + fmt::Debug> fmt::Debug for AccountLoader<'info, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("AccountLoader")
+            .field("acc_info", &self.acc_info)
+            .field("phantom", &self.phantom)
+            .finish()
+    }
+}
+
 impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
     fn new(acc_info: AccountInfo<'info>) -> AccountLoader<'info, T> {
         Self {

+ 10 - 0
lang/src/program.rs

@@ -4,6 +4,7 @@ use solana_program::account_info::AccountInfo;
 use solana_program::instruction::AccountMeta;
 use solana_program::program_error::ProgramError;
 use solana_program::pubkey::Pubkey;
+use std::fmt;
 use std::ops::Deref;
 
 /// Account container that checks ownership on deserialization.
@@ -13,6 +14,15 @@ pub struct Program<'info, T: Id + AccountDeserialize + Clone> {
     info: AccountInfo<'info>,
 }
 
+impl<'info, T: Id + AccountDeserialize + Clone + fmt::Debug> fmt::Debug for Program<'info, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Program")
+            .field("account", &self._account)
+            .field("info", &self.info)
+            .finish()
+    }
+}
+
 impl<'a, T: Id + AccountDeserialize + Clone> Program<'a, T> {
     fn new(info: AccountInfo<'a>, _account: T) -> Program<'a, T> {
         Self { info, _account }

+ 1 - 1
lang/src/signer.rs

@@ -10,7 +10,7 @@ use std::ops::Deref;
 /// Type validating that the account signed the transaction. No other ownership
 /// or type checks are done. If this is used, one should not try to access the
 /// underlying account data.
-#[derive(Clone)]
+#[derive(Debug, Clone)]
 pub struct Signer<'info> {
     info: AccountInfo<'info>,
 }

+ 1 - 1
lang/src/system_account.rs

@@ -8,7 +8,7 @@ use solana_program::pubkey::Pubkey;
 use solana_program::system_program;
 use std::ops::Deref;
 
-#[derive(Clone)]
+#[derive(Debug, Clone)]
 pub struct SystemAccount<'info> {
     info: AccountInfo<'info>,
 }

+ 1 - 1
lang/src/system_program.rs

@@ -4,7 +4,7 @@ use solana_program::pubkey::Pubkey;
 
 pub use solana_program::system_program::ID;
 
-#[derive(Clone)]
+#[derive(Debug, Clone)]
 pub struct System;
 
 impl anchor_lang::AccountDeserialize for System {

+ 10 - 0
lang/src/sysvar.rs

@@ -5,6 +5,7 @@ use solana_program::entrypoint::ProgramResult;
 use solana_program::instruction::AccountMeta;
 use solana_program::program_error::ProgramError;
 use solana_program::pubkey::Pubkey;
+use std::fmt;
 use std::ops::{Deref, DerefMut};
 
 /// Container for sysvars.
@@ -13,6 +14,15 @@ pub struct Sysvar<'info, T: solana_program::sysvar::Sysvar> {
     account: T,
 }
 
+impl<'info, T: solana_program::sysvar::Sysvar + fmt::Debug> fmt::Debug for Sysvar<'info, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Sysvar")
+            .field("info", &self.info)
+            .field("account", &self.account)
+            .finish()
+    }
+}
+
 impl<'info, T: solana_program::sysvar::Sysvar> Sysvar<'info, T> {
     pub fn from_account_info(
         acc_info: &AccountInfo<'info>,

+ 1 - 1
lang/src/unchecked_account.rs

@@ -8,7 +8,7 @@ use solana_program::pubkey::Pubkey;
 use std::ops::Deref;
 
 /// Explicit wrapper for AccountInfo types.
-#[derive(Clone)]
+#[derive(Debug, Clone)]
 pub struct UncheckedAccount<'info>(AccountInfo<'info>);
 
 impl<'info> UncheckedAccount<'info> {