Skip to content

Commit

Permalink
refactor: Adjust wnfs-wasm code
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Dec 5, 2023
1 parent 2693ae7 commit 0b725f7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
21 changes: 21 additions & 0 deletions wnfs-hamt/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ pub struct KeyValueChange<K, V> {
pub value2: Option<V>,
}

//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

impl<K, V> KeyValueChange<K, V> {
pub fn map<W>(self, f: &impl Fn(V) -> W) -> KeyValueChange<K, W> {
let Self {
r#type,
key,
value1,
value2,
} = self;
KeyValueChange {
r#type,
key,
value1: value1.map(f),
value2: value2.map(f),
}
}
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions wnfs-wasm/src/fs/private/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use libipld_core::cid::Cid;
use std::rc::Rc;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen_futures::future_to_promise;
use wnfs::private::forest::{
hamt::HamtForest as WnfsHamtForest, traits::PrivateForest as WnfsPrivateForest,
use wnfs::{
common::Storable,
private::forest::{
hamt::HamtForest as WnfsHamtForest, traits::PrivateForest as WnfsPrivateForest,
},
};
use wnfs_nameaccumulator::AccumulatorSetup;

Expand Down Expand Up @@ -47,7 +50,7 @@ impl PrivateForest {
let cid = Cid::read_bytes(&cid[..]).map_err(error("Cannot parse cid"))?;

Ok(future_to_promise(async move {
let forest: WnfsHamtForest = WnfsHamtForest::load(&cid, &store)
let forest = WnfsHamtForest::load(&cid, &store)
.await
.map_err(error("Couldn't deserialize forest"))?;

Expand Down Expand Up @@ -103,7 +106,7 @@ impl PrivateForest {

Ok(value!(diff
.into_iter()
.map(|c| value!(ForestChange(c)))
.map(|c| value!(ForestChange(c.map(&|c| c.0))))
.collect::<Array>()))
}))
}
Expand Down
5 changes: 2 additions & 3 deletions wnfs-wasm/src/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::rc::Rc;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;
use wnfs::{
common::BlockStore as WnfsBlockStore,
common::Storable,
public::{PublicDirectory as WnfsPublicDirectory, PublicNode as WnfsPublicNode},
traits::Id,
};
Expand Down Expand Up @@ -97,8 +97,7 @@ impl PublicDirectory {
let cid = Cid::read_bytes(&cid[..]).map_err(error("Cannot parse cid"))?;

Ok(future_to_promise(async move {
let directory: WnfsPublicDirectory = store
.get_deserializable(&cid)
let directory = WnfsPublicDirectory::load(&cid, &store)
.await
.map_err(error("Couldn't deserialize directory"))?;

Expand Down
5 changes: 2 additions & 3 deletions wnfs-wasm/src/fs/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::rc::Rc;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_futures::future_to_promise;
use wnfs::{
common::BlockStore as WnfsBlockStore,
common::Storable,
public::{PublicFile as WnfsPublicFile, PublicNode as WnfsPublicNode},
traits::Id,
};
Expand Down Expand Up @@ -66,8 +66,7 @@ impl PublicFile {
let cid = Cid::try_from(cid).map_err(|e| Error::new(&format!("Cannot parse cid: {e}")))?;

Ok(future_to_promise(async move {
let file: WnfsPublicFile = store
.get_deserializable(&cid)
let file = WnfsPublicFile::load(&cid, &store)
.await
.map_err(|e| Error::new(&format!("Couldn't deserialize directory: {e}")))?;

Expand Down
2 changes: 1 addition & 1 deletion wnfs/src/private/forest/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct HamtForestSerializable {

/// Links to ciphertexts
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
pub struct Ciphertexts(BTreeSet<Cid>);
pub struct Ciphertexts(pub BTreeSet<Cid>);

impl_storable_from_serde! { Ciphertexts }

Expand Down

0 comments on commit 0b725f7

Please sign in to comment.