diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..0f4a190 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,11 @@ +[target.x86_64-apple-darwin] +rustflags = [ + "-C", "link-arg=-undefined", + "-C", "link-arg=dynamic_lookup", +] + +[target.aarch64-apple-darwin] +rustflags = [ + "-C", "link-arg=-undefined", + "-C", "link-arg=dynamic_lookup", +] \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5bd05d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..878cd5f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +resolver = "2" +members = [ + "lua_modules/filesystem" +] + +[workspace.dependencies] +mlua = { version = "0.9.1", features = ["lua54", "module"] } diff --git a/justfile b/justfile new file mode 100755 index 0000000..acfd1bd --- /dev/null +++ b/justfile @@ -0,0 +1,22 @@ +#!/usr/bin/env just --justfile +set dotenv-load := true + +run-fs: (lua "fs.example.lua") +build-filesystem: (_copy-so "dev" "filesystem") + +lua file: + LUA_CPATH=c_modules/?.so lua lua/{{file}} + +_copy-so profile module: (_build profile module) + #!/usr/bin/env sh + if [ {{profile}} == "dev" ] + then + target_name="debug" + else + target_name="release" + fi + mkdir -p c_modules + ln -sf ../target/${target_name}/lib{{module}}.dylib c_modules/{{module}}.so + +_build profile module: + cargo build --profile={{profile}} --package {{module}} \ No newline at end of file diff --git a/lua/fs.example.lua b/lua/fs.example.lua new file mode 100644 index 0000000..169f2d1 --- /dev/null +++ b/lua/fs.example.lua @@ -0,0 +1,21 @@ +local fs = require("filesystem") + +local dirs = fs.directory(".") + +for dir in dirs do + print(dir) +end + +--for dir in dirs do +-- local files = dir:filter { +-- exclude = function(file) +-- return file.is_file and not file.basename:starts_with("._") +-- end +-- } +-- +-- zip { +-- from = files, +-- into = dir.basename, +-- extension = "cbz", +-- } +--end \ No newline at end of file diff --git a/lua_modules/filesystem/src/types/file_tree.rs b/lua_modules/filesystem/src/types/file_tree.rs new file mode 100644 index 0000000..3fb2626 --- /dev/null +++ b/lua_modules/filesystem/src/types/file_tree.rs @@ -0,0 +1,51 @@ +use mlua::{MetaMethod, UserData, UserDataMethods}; +use std::fs::{DirEntry, FileType, ReadDir}; +use std::path::PathBuf; + +pub trait Resource { + fn path(&self) -> String; + fn file_type(&self) -> FileType; +} +pub struct FileSystemResource { + path: PathBuf, +} +impl Resource for FileSystemResource { + fn path(&self) -> String { + self.path.display().to_string() + } + + fn file_type(&self) -> FileType { + self.path.metadata().unwrap().file_type() + } +} + +pub trait FileTreeIter: Iterator {} + +pub struct DirectoryFileTreeIter { + pub read_dir: ReadDir, +} + +impl Iterator for DirectoryFileTreeIter { + type Item = FileSystemResource; + + fn next(&mut self) -> Option { + self.read_dir.next().map(|entry| entry.unwrap().into()) + } +} + +impl From for FileSystemResource { + fn from(value: DirEntry) -> Self { + FileSystemResource { path: value.path() } + } +} + +impl UserData for DirectoryFileTreeIter { + fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_meta_method_mut(MetaMethod::Call, |lua, dir_iter, _: ()| Ok(dir_iter.next())) + } +} +impl UserData for FileSystemResource { + fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_meta_method(MetaMethod::ToString, |lua, fsr, _: ()| Ok(fsr.path())) + } +} diff --git a/lua_modules/filesystem/src/types/mod.rs b/lua_modules/filesystem/src/types/mod.rs new file mode 100644 index 0000000..ba68211 --- /dev/null +++ b/lua_modules/filesystem/src/types/mod.rs @@ -0,0 +1 @@ +pub mod file_tree;