Skip to content

Commit

Permalink
More wasm api
Browse files Browse the repository at this point in the history
  • Loading branch information
appcypher committed Apr 14, 2022
1 parent 14d8eb4 commit c711c58
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 36 deletions.
81 changes: 74 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,82 @@
This project will implement a pure rust crate for creating and manipulating IPLD graphs that encode WNFS.
Its goal is to be as dependency-less as possible in order to be easily compiled to WASM and used in browsers or other environments.

##

## Building Project
## Building the Project

#### REQUIREMENTS

- Rust toolchain

Rust toolchain can be installed by following the instructions [here](https://doc.rust-lang.org/cargo/getting-started/installation.html)

- WebAssembly toolchain

We need to build the project in WebAssembly. To do so, we need to install `wasm-pack` and tweak Rust toolchain context.

<details>
<summary>Read more</summary>

- Install `wasm32-unknown-unknown` target

```bash
rustup target add wasm32-unknown-unknown
```

- [rust-analyzer](https://rust-analyzer.github.io/manual.html#installation) is the go-to IDE tool for Rust and if you have it set up, you may want to set the `rust-analyzer.cargo.target` [setting](https://code.visualstudio.com/docs/getstarted/settings#_workspace-settings) to `wasm32-unknown-unknown`

- Install wasm-pack

```bash
cargo install wasm-pack
```

</details>

- **wnfs** script

If you are on a Unix platform, you can optionally install the `wnfs` script.

```bash
sh script/wnfs.sh setup
```

And you can use it like this afterwards

```bash
wnfs help
```

#### STEPS

- Clone the repository.

```bash
git https://github.com/fission-suite/rs-wnfs.git
```

- Change directory

```bash
cd rs-wnfs
```

- Build the project

```bash
sh scripts/wnfs.sh build
```

## Testing the Project

- Install [Rustup](https://www.rust-lang.org/tools/install)
- Run all tests

- Clone the repository:
```bash
sh scripts/wnfs.sh test
```

```bash
git clone https://github.com/fission-suite/rs-wnfs.git
```
- Show code coverage

```bash
sh scripts/wnfs.sh coverage
```
18 changes: 9 additions & 9 deletions crates/fs/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## The FileSystem

### Building the Project
## Building the Project

- Build project

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

### Testing the Project
## Testing the Project

- Build project
- Run tests

```bash
cargo test --release
```
```bash
cargo test --release
```
4 changes: 2 additions & 2 deletions crates/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,14 @@ mod public_directory_tests {
async fn files_added_to_directory_looked_up_unsuccessful() {
let root = PublicDirectory::new(Utc::now());

let mut store = MemoryBlockStore::default();
let store = MemoryBlockStore::default();

let content_cid = Cid::default();

let time = Utc::now();

let root = root
.write(&[String::from("text.txt")], content_cid, time, &mut store)
.write(&[String::from("text.txt")], content_cid, time, &store)
.await
.unwrap();

Expand Down
28 changes: 14 additions & 14 deletions crates/wasm/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
## The WebAssembly API

### Building the Project
## Building the Project

- Install `wasm-pack`

```bash
cargo install wasm-pack
```
```bash
cargo install wasm-pack
```

- Build project

```bash
wasm-pack build --target web
```
```bash
wasm-pack build --target web
```

### Testing the Project
## Testing the Project

- Start the test

```bash
wasm-pack test --chrome
```
```bash
wasm-pack test --chrome
```

- Run tests in the browser

```bash
open http://127.0.0.1:8000
```
```bash
open http://127.0.0.1:8000
```
2 changes: 0 additions & 2 deletions crates/wasm/fs/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use wnfs::public::PublicDirectory as WnfsPublicDirectory;
#[wasm_bindgen]
pub struct PublicDirectory(WnfsPublicDirectory);

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl PublicDirectory {
/// Creates a new directory using the given metadata.
Expand All @@ -19,7 +18,6 @@ impl PublicDirectory {
}
}

#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod public_directory_tests {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions crates/wasm/fs/public/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::fs::JsResult;
#[wasm_bindgen]
pub struct PublicFile(WnfsPublicFile);

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl PublicFile {
/// Creates a new file in a WNFS public file system.
Expand All @@ -24,7 +23,6 @@ impl PublicFile {
}
}

#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod public_file_tests {
use super::*;
Expand Down
174 changes: 174 additions & 0 deletions scripts/wnfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/bin/bash

# PATHS
# Get current working directory
current_dir=`pwd`

# Get the absolute path of where script is running from
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd)"
script_path="$script_dir/wnfs.sh"

# RETURN VARIABLE
ret=""

# ARGUMENTS
args="${@:2}" # All arguments except the first

# COLORS
red='\033[0;31m'
green='\033[0;32m'
none='\033[0m'

# DESCRIPTION:
# Where execution starts
#
main() {
case $1 in
build )
build
;;
test )
test
;;
coverage )
coverage
;;
setup )
setup
;;
*)
help
;;
esac

exit 0
}

# DESCRIPTION:
# Prints the help info.
#
# USAGE:
# wnfs build
#
help() {
echo ""
echo "Rust WNFS Utility Script"
echo ""
echo "USAGE:"
echo " wnfs [COMMAND] [...args]"
echo ""
echo "COMMAND:"
echo " * build - build project"
echo " * test - run tests"
echo " * coverage - show code coverage"
echo " * setup - install wnfs script"
echo " * -h, help - print this help message"
echo ""
echo ""
}

#-------------------------------------------------------------------------------
# Commands
#-------------------------------------------------------------------------------

# DESCRIPTION:
# Builds the project.
#
# USAGE:
# wnfs build
#
build() {
displayln "build command not implemented yet"
}


# DESCRIPTION:
# Runs tests.
#
# USAGE:
# wnfs test
#
test() {
displayln "test command not implemented yet"
}


# DESCRIPTION:
# Shows the code coverage of the project
#
# USAGE:
# wnfs coverage
#
coverage() {
displayln "coverage command not implemented yet"
}

#------------------------------------------------------------------------------
# Helper functions
#------------------------------------------------------------------------------

# DESCRIPTION:
# Gets the value following a flag
#
get_flag_value() {
local found=false
local key=$1
local count=0

# For every argument in the list of arguments
for arg in $args; do
count=$((count + 1))
# Check if any of the argument matches the key provided
if [[ $arg = $key ]]; then
found=true
break
fi
done

local args=($args)
local value=${args[count]}

# Check if argument specified was found
if [[ $found = true ]]; then

# Check if there exists a word after the key
# And that such word doesn't start with hyphen
if [[ ! -z $value ]] && [[ $value != "-"* ]]; then
ret=$value
else
ret=""
fi

else
ret=""
fi
}

# DESCRIPTION:
# Sets up the cript by making it excutable and available system wide
#
setup() {
display "Make script executable"
chmod u+x $script_path

display "Drop a link to it in /usr/local/bin"
ln -s $script_path /usr/local/bin/wnfs
}

# DESCRIPTION:
# A custom print function
#
display() {
printf "::: $1 :::\n"
}


# DESCRIPTION:
# A custom print function that starts its output with a newline
#
displayln() {
printf "\n::: $1 :::\n"
}


main $@

0 comments on commit c711c58

Please sign in to comment.