2023-01-19 01:47:14 +00:00
|
|
|
use std::path::{PathBuf, MAIN_SEPARATOR};
|
|
|
|
|
2023-01-23 02:31:55 +00:00
|
|
|
use mlua::prelude::*;
|
2023-02-03 19:15:20 +00:00
|
|
|
use tokio::fs;
|
2023-01-19 01:47:14 +00:00
|
|
|
|
2023-01-23 23:52:31 +00:00
|
|
|
use crate::utils::table::TableBuilder;
|
2023-01-21 20:48:56 +00:00
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
2023-02-10 11:14:28 +00:00
|
|
|
TableBuilder::new(lua)?
|
|
|
|
.with_async_function("readFile", fs_read_file)?
|
|
|
|
.with_async_function("readDir", fs_read_dir)?
|
|
|
|
.with_async_function("writeFile", fs_write_file)?
|
|
|
|
.with_async_function("writeDir", fs_write_dir)?
|
|
|
|
.with_async_function("removeFile", fs_remove_file)?
|
|
|
|
.with_async_function("removeDir", fs_remove_dir)?
|
|
|
|
.with_async_function("isFile", fs_is_file)?
|
|
|
|
.with_async_function("isDir", fs_is_dir)?
|
|
|
|
.build_readonly()
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_read_file(_: &'static Lua, path: String) -> LuaResult<String> {
|
2023-01-23 02:31:55 +00:00
|
|
|
fs::read_to_string(&path).await.map_err(LuaError::external)
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult<Vec<String>> {
|
2023-01-19 01:47:14 +00:00
|
|
|
let mut dir_strings = Vec::new();
|
2023-01-23 02:31:55 +00:00
|
|
|
let mut dir = fs::read_dir(&path).await.map_err(LuaError::external)?;
|
2023-02-03 19:15:20 +00:00
|
|
|
while let Some(dir_entry) = dir.next_entry().await.map_err(LuaError::external)? {
|
2023-01-19 01:47:14 +00:00
|
|
|
if let Some(dir_path_str) = dir_entry.path().to_str() {
|
|
|
|
dir_strings.push(dir_path_str.to_owned());
|
|
|
|
} else {
|
2023-01-23 02:31:55 +00:00
|
|
|
return Err(LuaError::RuntimeError(format!(
|
2023-01-19 01:47:14 +00:00
|
|
|
"File path could not be converted into a string: '{}'",
|
|
|
|
dir_entry.path().display()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut dir_string_prefix = path;
|
|
|
|
if !dir_string_prefix.ends_with(MAIN_SEPARATOR) {
|
|
|
|
dir_string_prefix.push(MAIN_SEPARATOR);
|
|
|
|
}
|
|
|
|
let dir_strings_no_prefix = dir_strings
|
|
|
|
.iter()
|
|
|
|
.map(|inner_path| {
|
|
|
|
inner_path
|
|
|
|
.trim()
|
|
|
|
.strip_prefix(&dir_string_prefix)
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
Ok(dir_strings_no_prefix)
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_write_file(_: &'static Lua, (path, contents): (String, String)) -> LuaResult<()> {
|
2023-01-19 02:11:47 +00:00
|
|
|
fs::write(&path, &contents)
|
2023-01-19 01:47:14 +00:00
|
|
|
.await
|
2023-01-23 02:31:55 +00:00
|
|
|
.map_err(LuaError::external)
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_write_dir(_: &'static Lua, path: String) -> LuaResult<()> {
|
2023-01-23 02:31:55 +00:00
|
|
|
fs::create_dir_all(&path).await.map_err(LuaError::external)
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_remove_file(_: &'static Lua, path: String) -> LuaResult<()> {
|
2023-01-23 02:31:55 +00:00
|
|
|
fs::remove_file(&path).await.map_err(LuaError::external)
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_remove_dir(_: &'static Lua, path: String) -> LuaResult<()> {
|
2023-01-23 02:31:55 +00:00
|
|
|
fs::remove_dir_all(&path).await.map_err(LuaError::external)
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_is_file(_: &'static Lua, path: String) -> LuaResult<bool> {
|
2023-01-19 01:47:14 +00:00
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.exists() {
|
|
|
|
Ok(fs::metadata(path)
|
|
|
|
.await
|
2023-01-23 02:31:55 +00:00
|
|
|
.map_err(LuaError::external)?
|
2023-01-19 01:47:14 +00:00
|
|
|
.is_file())
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:39:39 +00:00
|
|
|
async fn fs_is_dir(_: &'static Lua, path: String) -> LuaResult<bool> {
|
2023-01-19 01:47:14 +00:00
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.exists() {
|
|
|
|
Ok(fs::metadata(path)
|
|
|
|
.await
|
2023-01-23 02:31:55 +00:00
|
|
|
.map_err(LuaError::external)?
|
2023-01-19 01:47:14 +00:00
|
|
|
.is_dir())
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|