Skip to content

Commit

Permalink
chore: Write better Debug impls
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Apr 25, 2024
1 parent bfb18a7 commit 31ebaa8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions wnfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion wnfs/src/private/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub struct PrivateDirectory {
pub(crate) content: PrivateDirectoryContent,
}

#[derive(Debug)]
pub(crate) struct PrivateDirectoryContent {
pub(crate) persisted_as: OnceCell<Cid>,
pub(crate) previous: BTreeSet<(usize, Encrypted<Cid>)>,
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 14 additions & 1 deletion wnfs/src/private/encrypted.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<T> {
ciphertext: Vec<u8>,
value_cache: OnceCell<T>,
}

impl<T: std::fmt::Debug> std::fmt::Debug for Encrypted<T> {
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<T> Encrypted<T> {
/// Constructs an `Encrypted` wrapper from a given plaintext value.
///
Expand Down
18 changes: 17 additions & 1 deletion wnfs/src/private/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,30 @@ pub struct PrivateFile {
pub(crate) content: PrivateFileContent,
}

#[derive(Debug)]
pub(crate) struct PrivateFileContent {
pub(crate) persisted_as: OnceCell<Cid>,
pub(crate) previous: BTreeSet<(usize, Encrypted<Cid>)>,
pub(crate) metadata: Metadata,
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)]
Expand Down
4 changes: 2 additions & 2 deletions wnfs/src/private/keys/privateref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
15 changes: 14 additions & 1 deletion wnfs/src/private/link.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +13,6 @@ use wnfs_common::{
};
use wnfs_nameaccumulator::Name;

#[derive(Debug)]
pub(crate) enum PrivateLink {
Encrypted {
private_ref: PrivateRef,
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions wnfs/src/utils/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use crate::error::FsError;
use anyhow::Result;
use std::fmt::Debug;
use wnfs_common::utils::error;

pub struct OnceCellDebug<T>(pub Option<T>);

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

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 31ebaa8

Please sign in to comment.