can list directory content

develop
Kinch 2023-09-17 17:13:40 +02:00
parent bae3a32b8f
commit 1d07610dab
7 changed files with 116 additions and 0 deletions

11
.cargo/config Normal file
View File

@ -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",
]

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target
.idea

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[workspace]
resolver = "2"
members = [
"lua_modules/filesystem"
]
[workspace.dependencies]
mlua = { version = "0.9.1", features = ["lua54", "module"] }

22
justfile Executable file
View File

@ -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}}

21
lua/fs.example.lua Normal file
View File

@ -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

View File

@ -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<RES: Resource>: Iterator<Item = RES> {}
pub struct DirectoryFileTreeIter {
pub read_dir: ReadDir,
}
impl Iterator for DirectoryFileTreeIter {
type Item = FileSystemResource;
fn next(&mut self) -> Option<Self::Item> {
self.read_dir.next().map(|entry| entry.unwrap().into())
}
}
impl From<DirEntry> 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()))
}
}

View File

@ -0,0 +1 @@
pub mod file_tree;