From 31ebaa8eaf1bdf39c8bfff427c8f55a8c94a7493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Thu, 25 Apr 2024 11:39:23 +0200 Subject: [PATCH] chore: Write better `Debug` impls --- wnfs/Cargo.toml | 1 + wnfs/src/private/directory.rs | 18 +++++++++++++++++- wnfs/src/private/encrypted.rs | 15 ++++++++++++++- wnfs/src/private/file.rs | 18 +++++++++++++++++- wnfs/src/private/keys/privateref.rs | 4 ++-- wnfs/src/private/link.rs | 15 ++++++++++++++- wnfs/src/utils/common.rs | 9 +++++++++ 7 files changed, 74 insertions(+), 6 deletions(-) diff --git a/wnfs/Cargo.toml b/wnfs/Cargo.toml index 9b2cfad7..8d567824 100644 --- a/wnfs/Cargo.toml +++ b/wnfs/Cargo.toml @@ -28,6 +28,7 @@ bytes = "1.4.0" chacha20poly1305 = "0.10" chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } futures = "0.3" +hex = "0.4.3" insta = { version = "1.30", features = ["json"] } libipld-core = { version = "0.16" } multihash = "0.19" diff --git a/wnfs/src/private/directory.rs b/wnfs/src/private/directory.rs index c4a509f3..f1b90968 100644 --- a/wnfs/src/private/directory.rs +++ b/wnfs/src/private/directory.rs @@ -48,7 +48,6 @@ pub struct PrivateDirectory { pub(crate) content: PrivateDirectoryContent, } -#[derive(Debug)] pub(crate) struct PrivateDirectoryContent { pub(crate) persisted_as: OnceCell, pub(crate) previous: BTreeSet<(usize, Encrypted)>, @@ -1419,6 +1418,23 @@ impl PrivateDirectory { } } +impl Debug for PrivateDirectoryContent { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PrivateDirectoryContent") + .field( + "persisted_as", + &self + .persisted_as + .get() + .map_or("None".to_string(), |cid| format!("Some({cid})")), + ) + .field("previous", &self.previous) + .field("metadata", &self.metadata) + .field("entries", &self.entries) + .finish() + } +} + impl PrivateDirectoryContent { /// Serializes the directory to dag-cbor. pub(crate) async fn to_dag_cbor( diff --git a/wnfs/src/private/encrypted.rs b/wnfs/src/private/encrypted.rs index acc740d1..9928f034 100644 --- a/wnfs/src/private/encrypted.rs +++ b/wnfs/src/private/encrypted.rs @@ -1,4 +1,5 @@ use super::TemporalKey; +use crate::utils::OnceCellDebug; use anyhow::Result; use once_cell::sync::OnceCell; use serde::{de::DeserializeOwned, Deserialize, Serialize}; @@ -16,12 +17,24 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// It can be resolved to the plaintext value `T`, when /// you call `resolve_value`. Any subsequent calls to /// `resolve_value` will re-use a cached, decrypted value. -#[derive(Debug, Clone, Eq)] +#[derive(Clone, Eq)] pub struct Encrypted { ciphertext: Vec, value_cache: OnceCell, } +impl std::fmt::Debug for Encrypted { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Encrypted") + .field("ciphertext", &hex::encode(&self.ciphertext)) + .field( + "value_cache", + &OnceCellDebug(self.value_cache.get().map(|inner| format!("{inner:?}"))), + ) + .finish() + } +} + impl Encrypted { /// Constructs an `Encrypted` wrapper from a given plaintext value. /// diff --git a/wnfs/src/private/file.rs b/wnfs/src/private/file.rs index 968cd2ca..99abfed5 100644 --- a/wnfs/src/private/file.rs +++ b/wnfs/src/private/file.rs @@ -81,7 +81,6 @@ pub struct PrivateFile { pub(crate) content: PrivateFileContent, } -#[derive(Debug)] pub(crate) struct PrivateFileContent { pub(crate) persisted_as: OnceCell, pub(crate) previous: BTreeSet<(usize, Encrypted)>, @@ -89,6 +88,23 @@ pub(crate) struct PrivateFileContent { pub(crate) content: FileContent, } +impl std::fmt::Debug for PrivateFileContent { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PrivateFileContent") + .field( + "persisted_as", + &self + .persisted_as + .get() + .map_or("None".to_string(), |cid| format!("Some({cid})")), + ) + .field("previous", &self.previous) + .field("metadata", &self.metadata) + .field("content", &self.content) + .finish() + } +} + /// The content of a file. /// It is stored inline or stored in blocks. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/wnfs/src/private/keys/privateref.rs b/wnfs/src/private/keys/privateref.rs index 100c98a1..ecc95f11 100644 --- a/wnfs/src/private/keys/privateref.rs +++ b/wnfs/src/private/keys/privateref.rs @@ -130,8 +130,8 @@ impl Debug for PrivateRef { f.debug_struct("PrivateRef") .field("label", &rev_name_hash_str) - .field("temporal_key", &self.temporal_key.0) - .field("content_cid", &self.content_cid) + .field("temporal_key", &hex::encode(self.temporal_key.0)) + .field("content_cid", &format!("{}", self.content_cid)) .finish() } } diff --git a/wnfs/src/private/link.rs b/wnfs/src/private/link.rs index d265dd28..1c06d87f 100644 --- a/wnfs/src/private/link.rs +++ b/wnfs/src/private/link.rs @@ -1,6 +1,7 @@ use super::{ forest::traits::PrivateForest, PrivateDirectory, PrivateFile, PrivateNode, PrivateRef, }; +use crate::utils::OnceCellDebug; use anyhow::{anyhow, Result}; use async_once_cell::OnceCell; use async_recursion::async_recursion; @@ -12,7 +13,6 @@ use wnfs_common::{ }; use wnfs_nameaccumulator::Name; -#[derive(Debug)] pub(crate) enum PrivateLink { Encrypted { private_ref: PrivateRef, @@ -25,6 +25,19 @@ pub(crate) enum PrivateLink { }, } +impl std::fmt::Debug for PrivateLink { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Encrypted { private_ref, cache } => f + .debug_struct("Encrypted") + .field("private_ref", private_ref) + .field("cache", &OnceCellDebug(cache.get())) + .finish(), + Self::Decrypted { node } => f.debug_struct("Decrypted").field("node", node).finish(), + } + } +} + impl PrivateLink { pub(crate) fn from_ref(private_ref: PrivateRef) -> Self { Self::Encrypted { diff --git a/wnfs/src/utils/common.rs b/wnfs/src/utils/common.rs index 40ac16c6..b3175f3a 100644 --- a/wnfs/src/utils/common.rs +++ b/wnfs/src/utils/common.rs @@ -1,7 +1,16 @@ use crate::error::FsError; use anyhow::Result; +use std::fmt::Debug; use wnfs_common::utils::error; +pub struct OnceCellDebug(pub Option); + +impl Debug for OnceCellDebug { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("OnceCell").field(&self.0).finish() + } +} + //-------------------------------------------------------------------------------------------------- // Functions //--------------------------------------------------------------------------------------------------