Skip to content

Commit

Permalink
Fix wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
appcypher committed Apr 14, 2022
1 parent c624021 commit 2547c39
Show file tree
Hide file tree
Showing 16 changed files with 701 additions and 187 deletions.
111 changes: 99 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ hashbrown = "0.12.0"
async-trait = "0.1.53"
async-std = { version = "1.11.0", features = ["attributes"] }
async-recursion = "1.0.0"
field_names = "0.2.0"

[lib]
path = "lib.rs"
Expand Down
18 changes: 17 additions & 1 deletion crates/fs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
## The FileSystem
## The FileSystem

### Building the Project

- Build project

```bash
cargo build --release
```

### Testing the Project

- Build project

```bash
cargo test --release
```
28 changes: 15 additions & 13 deletions crates/fs/common/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ use super::FsError;
//--------------------------------------------------------------------------------------------------

/// For types that implement getting a block from a CID.
#[async_trait]
#[async_trait(?Send)]
pub trait BlockStoreLookup {
async fn get_block<'a>(&'a self, cid: &Cid) -> Result<Cow<'a, [u8]>>;
async fn get_block<'a>(&'a self, cid: &Cid) -> Result<Cow<'a, Vec<u8>>>;
}

/// For types that implement loading a cbor model from a blockstore using a CID.
#[async_trait]
/// For types that implement loading decodable object from a blockstore using a CID.
#[async_trait(?Send)]
pub trait BlockStoreCidLoad {
/// Loads a cbor model from the store with provided CID.
/// Loads a decodable object from the store with provided CID.
async fn load<T: Decode<C>, C: Codec>(&self, cid: &Cid, decoder: C) -> Result<T>;
}

/// For types that implement block store operations.
#[async_trait]
/// For types that implement block store operations like adding, getting content from the store.
#[async_trait(?Send)]
pub trait BlockStore: BlockStoreLookup + BlockStoreCidLoad {
async fn put_block(&mut self, bytes: Vec<u8>, codec: IpldCodec) -> Result<Cid>;
}

/// An in-memory block store to simulate IPFS. IPFS is basically an glorified HashMap.
/// An in-memory block store to simulate IPFS.
///
/// IPFS is basically an glorified HashMap.
#[derive(Debug, Default)]
pub struct MemoryBlockStore(HashMap<String, Vec<u8>>);

Expand All @@ -52,7 +54,7 @@ impl MemoryBlockStore {
}
}

#[async_trait]
#[async_trait(?Send)]
impl BlockStore for MemoryBlockStore {
/// Stores an array of bytes in the block store.
async fn put_block(&mut self, bytes: Vec<u8>, codec: IpldCodec) -> Result<Cid> {
Expand All @@ -65,10 +67,10 @@ impl BlockStore for MemoryBlockStore {
}
}

#[async_trait]
#[async_trait(?Send)]
impl BlockStoreLookup for MemoryBlockStore {
/// Retrieves an array of bytes from the block store with given CID.
async fn get_block<'a>(&'a self, cid: &Cid) -> Result<Cow<'a, [u8]>> {
async fn get_block<'a>(&'a self, cid: &Cid) -> Result<Cow<'a, Vec<u8>>> {
let bytes = self
.0
.get(&cid.to_string())
Expand All @@ -78,9 +80,9 @@ impl BlockStoreLookup for MemoryBlockStore {
}
}

#[async_trait]
#[async_trait(?Send)]
impl BlockStoreCidLoad for MemoryBlockStore {
/// Loads a cbor-encoded data from the store with provided CID.
/// Loads a CBOR-encoded data from the store with provided CID.
async fn load<T: Decode<C>, C: Codec>(&self, cid: &Cid, decoder: C) -> Result<T> {
let bytes = self.get_block(cid).await?;
let decoded = decoder.decode(bytes.as_ref())?;
Expand Down
7 changes: 6 additions & 1 deletion crates/fs/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ use std::{

use anyhow::Result;

/// File system errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FsError {
CIDNotFoundInBlockstore,
InvalidPath,
NodeNotFound,
NotAFile,
NotADirectory,
NotFound,
FileAlreadyExists,
UndecodableCborData(String),
}

impl std::error::Error for FsError {}
Expand Down
Loading

0 comments on commit 2547c39

Please sign in to comment.