use anyhow::Result;
use chrono::Utc;
use rand_chacha::ChaCha12Rng;
use rand_core::SeedableRng;
use wnfs::private::{
PrivateFile, PrivateForestContent,
forest::{hamt::HamtForest, traits::PrivateForest},
};
use wnfs_common::MemoryBlockStore;
#[async_std::main]
async fn main() -> Result<()> {
let store = &MemoryBlockStore::default();
let rng = &mut ChaCha12Rng::from_entropy();
let forest = &mut HamtForest::new_rsa_2048(rng);
let mut file = PrivateFile::with_content_rc(
&forest.empty_name(),
Utc::now(),
b"main content".to_vec(),
forest,
store,
rng,
)
.await?;
let content = PrivateForestContent::new(
file.header.get_name(),
b"secondary content".to_vec(),
forest,
store,
rng,
)
.await?;
file.get_metadata_mut_rc()?
.put("thumbnail", content.as_metadata_value()?);
file.as_node().store(forest, store, rng).await?;
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?,
b"secondary content".to_vec()
);
Ok(())
}