den_stdlib_fs/
lib.rs

1#[rquickjs::module(
2    rename = "den:fs",
3    rename_vars = "camelCase",
4    rename_types = "camelCase"
5)]
6pub mod fs {
7    use rquickjs::{module::Declarations, Ctx, Exception, Result};
8
9    #[qjs(declare)]
10    pub fn declare(declare: &Declarations) -> rquickjs::Result<()> {
11        declare.declare("hardLink")?;
12        declare.declare("canonicalize")?;
13        declare.declare("rename")?;
14        declare.declare("createDir")?;
15        declare.declare("removeDirAll")?;
16        declare.declare("symlinkMetadata")?;
17        declare.declare("metadata")?;
18        declare.declare("createDirAll")?;
19        declare.declare("setPermissions")?;
20        declare.declare("copy")?;
21        declare.declare("readDir")?;
22        declare.declare("readLink")?;
23        declare.declare("readToString")?;
24        declare.declare("removeFile")?;
25        declare.declare("read")?;
26        declare.declare("write")?;
27        declare.declare("removeDir")?;
28        Ok(())
29    }
30
31    #[rquickjs::function(rename = "canonicalize")]
32    pub async fn canonicalize(path: String) -> Result<Option<String>> {
33        Ok(tokio::fs::canonicalize(path)
34            .await?
35            .to_str()
36            .map(|x| x.to_string()))
37    }
38    #[rquickjs::function(rename = "copy")]
39    pub async fn copy(from: String, to: String) -> Result<()> {
40        tokio::fs::copy(from, to).await?;
41        Ok(())
42    }
43    #[rquickjs::function(rename = "createDir")]
44    pub async fn create_dir(path: String) -> Result<()> {
45        tokio::fs::create_dir(path).await?;
46        Ok(())
47    }
48    #[rquickjs::function(rename = "createDirAll")]
49    pub async fn create_dir_all(path: String) -> Result<()> {
50        tokio::fs::create_dir_all(path).await?;
51        Ok(())
52    }
53    #[rquickjs::function(rename = "hardLink")]
54    pub async fn hard_link(src: String, dst: String) -> Result<()> {
55        tokio::fs::hard_link(src, dst).await?;
56        Ok(())
57    }
58    #[rquickjs::function(rename = "metadata")]
59    pub async fn metadata(ctx: Ctx<'_>) -> Result<()> {
60        Err(Exception::throw_internal(&ctx, "not implemented"))
61    }
62    #[rquickjs::function(rename = "read")]
63    pub async fn read(path: String) -> Result<Vec<u8>> {
64        Ok(tokio::fs::read(path).await?)
65    }
66    #[rquickjs::function(rename = "readDir")]
67    pub async fn read_dir(ctx: Ctx<'_>) -> Result<()> {
68        Err(Exception::throw_internal(&ctx, "not implemented"))
69    }
70
71    #[rquickjs::function(rename = "readLink")]
72    pub async fn read_link(ctx: Ctx<'_>) -> Result<()> {
73        Err(Exception::throw_internal(&ctx, "not implemented"))
74    }
75
76    #[rquickjs::function(rename = "readToString")]
77    pub async fn read_to_string(path: String) -> Result<String> {
78        Ok(tokio::fs::read_to_string(path).await?)
79    }
80
81    #[rquickjs::function(rename = "removeDir")]
82    #[qjs(rename = "removeDir")]
83    pub async fn remove_dir(path: String) -> Result<()> {
84        tokio::fs::remove_dir(path).await?;
85        Ok(())
86    }
87
88    #[rquickjs::function(rename = "removeDirAll")]
89    pub async fn remove_dir_all(path: String) -> Result<()> {
90        tokio::fs::remove_dir_all(path).await?;
91        Ok(())
92    }
93
94    #[rquickjs::function(rename = "removeFile")]
95    pub async fn remove_file(path: String) -> Result<()> {
96        tokio::fs::remove_file(path).await?;
97        Ok(())
98    }
99
100    #[rquickjs::function(rename = "rename")]
101    pub async fn rename(from: String, to: String) -> Result<()> {
102        tokio::fs::rename(from, to).await?;
103        Ok(())
104    }
105
106    #[rquickjs::function(rename = "setPermissions")]
107    pub async fn set_permissions(ctx: Ctx<'_>) -> Result<()> {
108        Err(Exception::throw_internal(&ctx, "not implemented"))
109    }
110
111    #[rquickjs::function(rename = "symlinkMetadata")]
112    pub async fn symlink_metadata(ctx: Ctx<'_>) -> Result<()> {
113        Err(Exception::throw_internal(&ctx, "not implemented"))
114    }
115
116    #[rquickjs::function(rename = "write")]
117    pub async fn write(path: String, contents: Vec<u8>) -> Result<()> {
118        tokio::fs::write(path, contents).await?;
119        Ok(())
120    }
121}