Skip to content

Commit

Permalink
refactor: Implement and use as_metadata_value
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Sep 4, 2023
1 parent 139ad3c commit b05add8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 4 additions & 2 deletions wnfs/examples/file_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ async fn main() -> Result<()> {

// We store the content in the file metadata.
// This will update the `file: Rc<PrivateFile>` for us with a new reference.
content.store_in_metadata(file.get_metadata_mut_rc()?, "thumbnail")?;
file.get_metadata_mut_rc()?
.put("thumbnail", content.as_metadata_value()?);

// We store the new reference in the forest.
file.as_node().store(forest, store, rng).await?;

// When can look up the private forest content again.
let content = PrivateForestContent::load_from_metadata(file.get_metadata(), "thumbnail")?;
let content_ipld = file.get_metadata().get("thumbnail").unwrap();
let content = PrivateForestContent::from_metadata_value(content_ipld)?;

assert_eq!(
content.get_content(forest, store).await?,
Expand Down
21 changes: 12 additions & 9 deletions wnfs/src/private/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use async_once_cell::OnceCell;
use async_stream::try_stream;
use chrono::{DateTime, Utc};
use futures::{future, stream::LocalBoxStream, AsyncRead, Stream, StreamExt, TryStreamExt};
use libipld_core::cid::Cid;
use libipld_core::{
cid::Cid,
ipld::Ipld,
serde::{from_ipld, to_ipld},
};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeSet, iter, rc::Rc};
Expand Down Expand Up @@ -755,20 +759,19 @@ impl PrivateForestContent {

/// Load some previously stored keys & pointers to encrypted private forest content
/// from given metadata key.
pub fn load_from_metadata(metadata: &Metadata, key: &str) -> Result<Self> {
let wrapped: MetadataContentCapsule<Self> = metadata
.get_deserializable(key)
.ok_or_else(|| FsError::NotFound)??;
pub fn from_metadata_value(value: &Ipld) -> Result<Self> {
let wrapped: MetadataContentCapsule<Self> = from_ipld(value.clone())?;

Ok(match wrapped {
MetadataContentCapsule::PrivateForestContent(content) => content,
})
}

/// Store these pointers & key to some private forest content in given metadata under given key.
pub fn store_in_metadata(&self, metadata: &mut Metadata, key: &str) -> Result<()> {
metadata.put_serializable(key, MetadataContentCapsule::PrivateForestContent(&self))?;
Ok(())
// Serialize these pointers & keys into some data that can be stored in a `PrivateFile`'s metadata.
pub fn as_metadata_value(&self) -> Result<Ipld> {
Ok(to_ipld(MetadataContentCapsule::PrivateForestContent(
&self,
))?)
}

/// Decrypt & stream out the contents that `self` points to in given forest.
Expand Down

0 comments on commit b05add8

Please sign in to comment.