Skip to content

Commit

Permalink
feat: Add as_file and is_file to PrivateNode (wasm) (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset authored Jan 10, 2023
1 parent b70c312 commit f02658b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion wnfs-wasm/src/fs/private/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use wnfs::{Id, PrivateFile as WnfsPrivateFile};

/// A file in a WNFS public file system.
#[wasm_bindgen]
pub struct PrivateFile(Rc<WnfsPrivateFile>);
pub struct PrivateFile(pub(crate) Rc<WnfsPrivateFile>);

//--------------------------------------------------------------------------------------------------
// Implementations
Expand Down
17 changes: 16 additions & 1 deletion wnfs-wasm/src/fs/private/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use js_sys::Error;
use wasm_bindgen::prelude::wasm_bindgen;
use wnfs::{private::PrivateNode as WnfsPrivateNode, Id};

use crate::fs::{JsResult, PrivateDirectory};
use crate::fs::{JsResult, PrivateDirectory, PrivateFile};

//--------------------------------------------------------------------------------------------------
// Type Definitions
Expand All @@ -28,11 +28,26 @@ impl PrivateNode {
Ok(PrivateDirectory(dir))
}

#[wasm_bindgen(js_name = "asFile")]
pub fn as_file(&self) -> JsResult<PrivateFile> {
let file = self
.0
.as_file()
.map_err(|e| Error::new(&format!("Cannot cast to a file: {e}")))?;

Ok(PrivateFile(file))
}

#[wasm_bindgen(js_name = "isDir")]
pub fn is_dir(&self) -> bool {
self.0.is_dir()
}

#[wasm_bindgen(js_name = "isFile")]
pub fn is_file(&self) -> bool {
self.0.is_file()
}

#[wasm_bindgen(js_name = "getId")]
pub fn get_id(&self) -> String {
self.0.get_id()
Expand Down

0 comments on commit f02658b

Please sign in to comment.