Skip to content

Commit

Permalink
Make debug impl more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Nov 24, 2024
1 parent b1562a2 commit 42949a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 7 additions & 1 deletion rust/src/code_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_with::serde_as;
const CONTRACT_KEY_SIZE: usize = 32;

#[serde_as]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
pub struct CodeHash(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] pub(crate) [u8; CONTRACT_KEY_SIZE]);

Expand Down Expand Up @@ -71,3 +71,9 @@ impl Display for CodeHash {
write!(f, "{}", self.encode())
}
}

impl std::fmt::Debug for CodeHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("CodeHash").field(&self.encode()).finish()
}
}
28 changes: 25 additions & 3 deletions rust/src/contract_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ impl<'a> DerefMut for StateSummary<'a> {
/// It is the part of the executable belonging to the full specification
/// and does not include any other metadata (like the parameters).
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
pub struct ContractCode<'a> {
// TODO: conver this to Arc<[u8]> instead
Expand Down Expand Up @@ -900,9 +900,17 @@ impl std::fmt::Display for ContractCode<'_> {
}
}

impl std::fmt::Debug for ContractCode<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ContractCode")
.field("hash", &self.code_hash);
Ok(())
}
}

/// The key representing the hash of the contract executable code hash and a set of `parameters`.
#[serde_as]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[repr(transparent)]
pub struct ContractInstanceId(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] [u8; CONTRACT_KEY_SIZE]);
Expand Down Expand Up @@ -970,6 +978,14 @@ impl Display for ContractInstanceId {
}
}

impl std::fmt::Debug for ContractInstanceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ContractInstanceId")
.field(&self.encode())
.finish()
}
}

/// A complete key specification, that represents a cryptographic hash that identifies the contract.
#[serde_as]
#[derive(Debug, Eq, Copy, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -1137,7 +1153,7 @@ fn internal_fmt_key(

// TODO: get rid of this when State is internally an Arc<[u8]>
/// The state for a contract.
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
#[derive(PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
pub struct WrappedState(
#[serde(
Expand Down Expand Up @@ -1223,6 +1239,12 @@ impl std::fmt::Display for WrappedState {
}
}

impl std::fmt::Debug for WrappedState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<Self as Display>::fmt(self, f)
}
}

/// Just as `freenet_stdlib::Contract` but with some convenience impl.
#[non_exhaustive]
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit 42949a5

Please sign in to comment.