wnfs 0.1.6

WebNative filesystem core implementation
Documentation

This crate is a Rust implementation of the primitives for creating and manipulating IPLD graphs that encode WNFS.

A goal of the project is to be easily compiled to WebAssembly to be used in the browsers or other environments.

Outline

Usage

Creating a new public directory.

use wnfs::{PublicDirectory, Id};

use async_std::main;
use chrono::Utc;

#[async_std::main]
async fn main() {
  let dir = PublicDirectory::new(Utc::now());
  println!("id = {}", dir.get_id());
}

The in-memory files and directories you create with wnfs will need to be sealed and stored somewhere. For that, a type that implements the BlockStore trait like this one can be used.

use wnfs::{MemoryBlockStore, PublicDirectory, OpResult, ipld::Cid};

use async_std::main;
use chrono::Utc;

use std::rc::Rc;
// ...

The WNFS API is immutable, therefore, we need to keep track of the updated root directory after every change.

Each fs operation returns a possibly updated root directory that subsequent changes can be applied on.

// ...
#[async_std::main]
async fn main() {
    let time = Utc::now();
    let dir = Rc::new(PublicDirectory::new(time));
    let store = MemoryBlockStore::default();

    // Create a /pictures/cats directory.
    let OpResult { root_dir, .. } = dir
        .mkdir(&["pictures".into(), "cats".into()], time, &store)
        .await
        .unwrap();

    // Get a sample CIDv1.
    let cid = Cid::default();

    // Add a file to /pictures/cats.
    let OpResult { root_dir, .. } = root_dir
        .write(
            &["pictures".into(), "cats".into(), "tabby.png".into()],
            cid,
            time,
            &store,
        )
        .await
        .unwrap();

    // Create and add a file to /pictures/dogs directory.
    let OpResult { root_dir, .. } = root_dir
        .write(
            &["pictures".into(), "dogs".into(), "billie.jpeg".into()],
            cid,
            time,
            &store,
        )
        .await
        .unwrap();

    // Delete /pictures/cats directory.
    let OpResult { root_dir, .. } = root_dir
        .rm(&["pictures".into(), "cats".into()], &store)
        .await
        .unwrap();

    // List all files in /pictures directory.
    let OpResult { result, .. } = root_dir
        .ls(&["pictures".into()], &store)
        .await
        .unwrap();

    println!("Files in /pictures: {:#?}", result);
}

Building the Project

  • Build project

    cargo build --release
    

Testing the Project

  • Run tests

    cargo test --release