add source sinkd concept

develop
Kinch 2023-09-17 21:27:11 +02:00
parent f17bc694cc
commit ca1dca8414
12 changed files with 546 additions and 126 deletions

323
Cargo.lock generated
View File

@ -2,12 +2,44 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aes"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base64ct"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bstr"
version = "1.6.2"
@ -18,12 +50,40 @@ dependencies = [
"serde",
]
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bzip2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"jobserver",
"libc",
]
@ -33,11 +93,129 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "cpufeatures"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "deranged"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946"
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "filesystem"
version = "0.1.0"
dependencies = [
"mlua",
"zip",
]
[[package]]
name = "flate2"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6c98ee8095e9d1dcbf2fcc6d95acccb90d1c81db1e44725c6a984b1dbdfb010"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "jobserver"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2"
dependencies = [
"libc",
]
[[package]]
@ -52,6 +230,15 @@ version = "2.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c"
[[package]]
name = "miniz_oxide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
]
[[package]]
name = "mlua"
version = "0.9.1"
@ -103,6 +290,29 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "password-hash"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700"
dependencies = [
"base64ct",
"rand_core",
"subtle",
]
[[package]]
name = "pbkdf2"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917"
dependencies = [
"digest",
"hmac",
"password-hash",
"sha2",
]
[[package]]
name = "pkg-config"
version = "0.3.27"
@ -127,6 +337,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rustc-hash"
version = "1.1.0"
@ -153,6 +369,34 @@ dependencies = [
"syn",
]
[[package]]
name = "sha1"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sha2"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "subtle"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "2.0.36"
@ -164,8 +408,87 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "time"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48"
dependencies = [
"deranged",
"serde",
"time-core",
]
[[package]]
name = "time-core"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "zip"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261"
dependencies = [
"aes",
"byteorder",
"bzip2",
"constant_time_eq",
"crc32fast",
"crossbeam-utils",
"flate2",
"hmac",
"pbkdf2",
"sha1",
"time",
"zstd",
]
[[package]]
name = "zstd"
version = "0.11.2+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "5.0.2+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db"
dependencies = [
"libc",
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.8+zstd.1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5556e6ee25d32df2586c098bbfa278803692a20d0ab9565e049480d52707ec8c"
dependencies = [
"cc",
"libc",
"pkg-config",
]

View File

@ -1,8 +1,6 @@
local fs = require("filesystem")
local dirs = fs.directory("/Users/kinch/Desktop/Rebuild World")
local FileTree = {foo = "bar"}
local FileTree = {}
function FileTree:new(iterator)
local file_tree = {
iterator = iterator,
@ -13,6 +11,16 @@ function FileTree:new(iterator)
return file_tree
end
function directory(path_or_dir)
if type(path_or_dir) == "string" then
return FileTree:new(fs.directory_source(path_or_dir))
end
if path_or_dir.path ~= nil then
return FileTree:new(fs.directory_source(path_or_dir.path))
end
error("Invalid type for directory")
end
function FileTree:filter(filter_fn)
table.insert(self.filters, filter_fn)
return self
@ -24,7 +32,7 @@ function FileTree:__call()
return nil
end
for k,filter in pairs(self.filters) do
for _,filter in pairs(self.filters) do
if (not filter(next)) then
return self()
end
@ -36,42 +44,35 @@ function is_dir(entry)
return entry:is_dir()
end
function is_file(entry)
return entry:is_dir()
end
function starts_with(prefix)
return function(entry)
return entry.path:find("^"..prefix)
end
end
function contains(infix)
return function(entry)
return entry.path:find(infix)
end
end
function does_not(predicate)
return function(...) return not predicate(...) end
end
local comic_dir = FileTree:new(fs.directory("."))
local comic_dir = directory("/Users/kinch/Desktop/Rebuild World")
comic_dir:filter(is_dir)
for chapter_dir in comic_dir do
FileTree:new(fs.directory(chapter_dir)):filter(does_not(starts_with("./")))
print(chapter_dir)
--local images = FileTree
-- :new(fs.directory(chapter_dir.path))
-- :filter(does_not(contains("._")))
--for img in images do
-- print(img)
--end
end
--for dir in dirs do
-- if dir:is_dir() then
-- for sub_dir in fs.directory(dir.path) do
-- print(sub_dir)
-- end
-- end
--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

@ -8,3 +8,4 @@ crate-type = ["cdylib"]
[dependencies]
mlua = { workspace = true }
zip = "0.6"

View File

@ -1,16 +1,16 @@
mod types;
use crate::types::file_tree::*;
use crate::source::directory_source::DirectorySource;
use mlua::prelude::*;
fn directory(lua: &Lua, path: String) -> LuaResult<DirectoryFileTreeIter> {
let read_dir = std::fs::read_dir(path).unwrap();
Ok(DirectoryFileTreeIter { read_dir })
pub mod resources;
pub mod sinks;
pub mod source;
fn directory_source(lua: &Lua, path: String) -> LuaResult<DirectorySource> {
Ok(DirectorySource::from(path))
}
#[mlua::lua_module]
fn filesystem(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("directory", lua.create_function(directory)?)?;
exports.set("directory_source", lua.create_function(directory_source)?)?;
Ok(exports)
}

View File

@ -0,0 +1,72 @@
pub mod resource_filesystem;
use mlua::{MetaMethod, UserData, UserDataFields, UserDataMethods};
use std::fs::FileType;
use std::ops::{Deref, DerefMut};
#[derive(Copy, Clone)]
pub enum ResourceType {
Directory,
RegularFile,
}
pub trait Resource {
fn path(&self) -> String;
fn basename(&self) -> Option<String>;
fn dirname(&self) -> Option<String>;
fn extension(&self) -> Option<String>;
fn stem(&self) -> Option<String>;
fn file_type(&mut self) -> ResourceType;
//noinspection RsSelfConvention
fn is_dir(&mut self) -> bool {
if let ResourceType::Directory = self.file_type() {
return true;
}
false
}
//noinspection RsSelfConvention
fn is_file(&mut self) -> bool {
if let ResourceType::RegularFile = self.file_type() {
return true;
}
false
}
}
pub struct ResourceWrapper<ResType>(ResType);
impl<ResType: Resource> From<ResType> for ResourceWrapper<ResType> {
fn from(value: ResType) -> Self {
ResourceWrapper(value)
}
}
impl<ResType> Deref for ResourceWrapper<ResType> {
type Target = ResType;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<Restype> DerefMut for ResourceWrapper<Restype> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<ResType: Resource> UserData for ResourceWrapper<ResType> {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("file_name", |lua, this| Ok(this.basename()));
fields.add_field_method_get("dirname", |lua, this| Ok(this.dirname()));
fields.add_field_method_get("extension", |lua, this| Ok(this.extension()));
fields.add_field_method_get("stem", |lua, this| Ok(this.stem()));
fields.add_field_method_get("path", |lua, this| Ok(this.path()));
}
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method_mut("is_dir", |lua, this, _: ()| Ok(this.is_dir()));
methods.add_method_mut("is_file", |lua, this, _: ()| Ok(this.is_file()));
methods.add_meta_method(MetaMethod::ToString, |lua, this, _: ()| Ok(this.path()))
}
}

View File

@ -0,0 +1,67 @@
use crate::resources::{Resource, ResourceType};
use std::fs::FileType;
use std::path::{Path, PathBuf};
pub struct FilesystemResource {
path: PathBuf,
resource_type: Option<ResourceType>,
}
impl From<PathBuf> for FilesystemResource {
fn from(path: PathBuf) -> Self {
FilesystemResource {
path,
resource_type: None,
}
}
}
impl Resource for FilesystemResource {
fn path(&self) -> String {
self.path.display().to_string()
}
fn basename(&self) -> Option<String> {
self.path
.file_name()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
}
fn dirname(&self) -> Option<String> {
self.path.parent().map(|p| p.display().to_string())
}
fn extension(&self) -> Option<String> {
self.path
.extension()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
}
fn stem(&self) -> Option<String> {
self.path
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_string())
}
fn file_type(&mut self) -> ResourceType {
if let Some(resource_type) = self.resource_type {
return resource_type;
} else {
let resource_type = self.path.metadata().unwrap().file_type().into();
self.resource_type = Some(resource_type);
resource_type
}
}
}
impl From<FileType> for ResourceType {
fn from(file_type: FileType) -> Self {
if file_type.is_file() {
ResourceType::RegularFile
} else {
ResourceType::Directory
}
}
}

View File

@ -0,0 +1,3 @@
mod zip_sink;
pub trait ResourceSink {}

View File

@ -0,0 +1,40 @@
use crate::resources::resource_filesystem::FilesystemResource;
use crate::resources::ResourceWrapper;
use mlua::{MetaMethod, UserData, UserDataMethods};
use std::fs::{read_dir, ReadDir};
use std::path::PathBuf;
pub struct DirectorySource {
path: PathBuf,
read_dir: ReadDir,
}
impl From<String> for DirectorySource {
fn from(path: String) -> Self {
let path = PathBuf::from(path);
let read_dir = std::fs::read_dir(&path).expect("could not read dir");
DirectorySource { path, read_dir }
}
}
impl Iterator for DirectorySource {
type Item = FilesystemResource;
fn next(&mut self) -> Option<Self::Item> {
self.read_dir
.next()
.map(|entry| entry.expect("Could not read directory content").path())
.map(|path| FilesystemResource::from(path))
}
}
impl UserData for DirectorySource {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::ToString, |_, this, _: ()| {
Ok(this.path.display().to_string())
});
methods.add_meta_method_mut(MetaMethod::Call, |_, this, _: ()| {
Ok(this.next().map(|entry| ResourceWrapper::from(entry)))
});
}
}

View File

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

View File

@ -1,87 +0,0 @@
use crate::types::file_tree::ResourceType::{Directory, RegularFile};
use mlua::{MetaMethod, UserData, UserDataFields, UserDataMethods};
use std::fs::{DirEntry, FileType, ReadDir};
use std::path::PathBuf;
pub enum ResourceType {
Directory,
RegularFile,
}
impl From<FileType> for ResourceType {
fn from(file_type: FileType) -> Self {
if file_type.is_file() {
RegularFile
} else {
Directory
}
}
}
pub trait Resource {
fn path(&self) -> String;
fn file_type(&self) -> ResourceType;
fn is_dir(&self) -> bool {
if let Directory = self.file_type() {
true
} else {
false
}
}
fn is_file(&self) -> bool {
if let RegularFile = self.file_type() {
true
} else {
false
}
}
}
pub struct FileSystemResource {
path: PathBuf,
}
impl Resource for FileSystemResource {
fn path(&self) -> String {
self.path.display().to_string()
}
fn file_type(&self) -> ResourceType {
self.path.metadata().unwrap().file_type().into()
}
}
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_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("path", |lua, fsr| Ok(fsr.path.display().to_string()))
}
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::ToString, |lua, fsr, _: ()| Ok(fsr.path()));
methods.add_method("is_dir", |lua, fsr, _: ()| Ok(fsr.is_dir()));
methods.add_method("is_file", |lua, fsr, _: ()| Ok(fsr.is_file()));
}
}

View File

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