rework but everything is dyn
parent
2e63947569
commit
b9b9fc393c
|
|
@ -168,6 +168,8 @@ name = "filesystem"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mlua",
|
"mlua",
|
||||||
|
"thiserror",
|
||||||
|
"walkdir",
|
||||||
"zip",
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -349,6 +351,15 @@ version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.188"
|
version = "1.0.188"
|
||||||
|
|
@ -408,6 +419,26 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.48"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.48"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.28"
|
version = "0.3.28"
|
||||||
|
|
@ -443,6 +474,47 @@ version = "0.9.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "walkdir"
|
||||||
|
version = "2.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
|
||||||
|
dependencies = [
|
||||||
|
"same-file",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zip"
|
name = "zip"
|
||||||
version = "0.6.6"
|
version = "0.6.6"
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,4 @@ members = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
mlua = { version = "0.9.1", features = ["lua54", "module"] }
|
mlua = { version = "0.9.1", features = ["lua54", "module"] }
|
||||||
|
|
@ -1 +1,7 @@
|
||||||
local fs = require("filesystem")
|
local fs = require("filesystem")
|
||||||
|
|
||||||
|
local root = fs.directory(".")
|
||||||
|
|
||||||
|
for entry in root do
|
||||||
|
print(entry)
|
||||||
|
end
|
||||||
|
|
@ -7,5 +7,7 @@ edition = "2021"
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
thiserror = "1.0.48"
|
||||||
mlua = { workspace = true }
|
mlua = { workspace = true }
|
||||||
zip = "0.6"
|
zip = "0.6"
|
||||||
|
walkdir = "2.4.0"
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
use crate::driver_filesystem::filesystem_resource::FilesystemResourceLocation;
|
||||||
|
use crate::resource::{DynResource, DynResourceLocation, Resource, ResourceLocation};
|
||||||
|
use crate::source::ResourceSource;
|
||||||
|
use std::io::Sink;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
pub struct DirectorySource {
|
||||||
|
location: String,
|
||||||
|
iter: walkdir::IntoIter,
|
||||||
|
}
|
||||||
|
impl DirectorySource {
|
||||||
|
pub fn open_dir(location: String) -> Self {
|
||||||
|
let iter = WalkDir::new(location.clone()).into_iter();
|
||||||
|
DirectorySource { location, iter }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResourceSource for DirectorySource {
|
||||||
|
fn location(&self) -> DynResourceLocation {
|
||||||
|
Box::new(FilesystemResourceLocation::from(PathBuf::from(
|
||||||
|
self.location.clone(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for DirectorySource {
|
||||||
|
type Item = DynResourceLocation;
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
use crate::resource::ResourceLocation;
|
||||||
|
self.iter
|
||||||
|
.next()
|
||||||
|
.map(|entry| entry.unwrap())
|
||||||
|
.map(|entry| entry.path().to_path_buf())
|
||||||
|
.map(|entry| {
|
||||||
|
let loc = FilesystemResourceLocation::from(entry);
|
||||||
|
Box::new(loc) as Box<dyn ResourceLocation>
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
use crate::resource::ResourceLocation;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub struct FilesystemResourceLocation {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResourceLocation for FilesystemResourceLocation {
|
||||||
|
fn location(&self) -> String {
|
||||||
|
self.path.display().to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PathBuf> for FilesystemResourceLocation {
|
||||||
|
fn from(path: PathBuf) -> Self {
|
||||||
|
FilesystemResourceLocation { path }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod directory_source;
|
||||||
|
pub mod filesystem_resource;
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
use mlua::prelude::{LuaError, LuaResult};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum FsError {
|
||||||
|
#[error("fs error happened: {msg}")]
|
||||||
|
GenericError { msg: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Res<T = ()> = Result<T, FsError>;
|
||||||
|
|
||||||
|
impl From<FsError> for LuaError {
|
||||||
|
fn from(fs_error: FsError) -> Self {
|
||||||
|
LuaError::RuntimeError(fs_error.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,24 @@
|
||||||
|
mod driver_filesystem;
|
||||||
|
mod driver_zip_archive;
|
||||||
|
mod error;
|
||||||
|
mod lua_bindings;
|
||||||
|
mod operations;
|
||||||
|
mod resource;
|
||||||
|
mod sink;
|
||||||
|
mod source;
|
||||||
|
|
||||||
|
use crate::source::DynResourceSource;
|
||||||
|
use driver_filesystem::directory_source::DirectorySource;
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
fn directory(lua: &Lua, path: String) -> LuaResult<DynResourceSource> {
|
||||||
|
let source = DirectorySource::open_dir(path);
|
||||||
|
Ok(Box::new(source))
|
||||||
|
}
|
||||||
|
|
||||||
#[mlua::lua_module]
|
#[mlua::lua_module]
|
||||||
fn filesystem(lua: &Lua) -> LuaResult<LuaTable> {
|
fn filesystem(lua: &Lua) -> LuaResult<LuaTable> {
|
||||||
let exports = lua.create_table()?;
|
let exports = lua.create_table()?;
|
||||||
|
exports.set("directory", lua.create_function(directory)?)?;
|
||||||
Ok(exports)
|
Ok(exports)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
use crate::resource::DynResourceLocation;
|
||||||
|
use mlua::{MetaMethod, UserData, UserDataFields, UserDataMethods};
|
||||||
|
|
||||||
|
mod resource_bindings;
|
||||||
|
mod source_bindings;
|
||||||
|
|
||||||
|
impl UserData for DynResourceLocation {
|
||||||
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
methods.add_meta_method_mut(MetaMethod::ToString, |lua, this, _: ()| Ok(this.location()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
use crate::source::DynResourceSource;
|
||||||
|
use mlua::{MetaMethod, UserData, UserDataMethods};
|
||||||
|
|
||||||
|
impl UserData for DynResourceSource {
|
||||||
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
methods.add_meta_method_mut(MetaMethod::Call, |lua, this, _: ()| Ok(this.next()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
mod operation_copy;
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
use crate::sink::DynResourceSink;
|
||||||
|
use crate::source::DynResourceSource;
|
||||||
|
|
||||||
|
pub fn copy_operation(from: DynResourceSource, to: DynResourceSink) {}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
use crate::sink::ResourceSink;
|
||||||
|
use crate::source::ResourceSource;
|
||||||
|
|
||||||
|
/// Points to a resource. Think of it as a path.
|
||||||
|
pub trait ResourceLocation {
|
||||||
|
fn location(&self) -> String;
|
||||||
|
}
|
||||||
|
/// A concrete resource. Think of as a directory or a file
|
||||||
|
pub trait Resource {}
|
||||||
|
// A resource ready to be read. Think of it as a open file for reading
|
||||||
|
pub trait ResourceInputStream: std::io::Read {}
|
||||||
|
// A resource ready to be written. Think of it as a open file for reading
|
||||||
|
pub trait ResourceOutputStream: std::io::Write {}
|
||||||
|
pub type DynResource = Box<dyn Resource>;
|
||||||
|
pub type DynResourceLocation = Box<dyn ResourceLocation>;
|
||||||
|
pub type DynResourceInputStream = Box<dyn ResourceInputStream>;
|
||||||
|
pub type DynResourceOutputStream = Box<dyn ResourceOutputStream>;
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
use crate::resource::Resource;
|
||||||
|
|
||||||
|
pub trait ResourceSink {}
|
||||||
|
pub type DynResourceSink = Box<dyn ResourceSink>;
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
use crate::resource::{DynResource, DynResourceLocation};
|
||||||
|
pub trait ResourceSource: Iterator<Item = DynResourceLocation> {
|
||||||
|
fn location(&self) -> DynResourceLocation;
|
||||||
|
}
|
||||||
|
pub type DynResourceSource = Box<dyn ResourceSource<Item = DynResourceLocation>>;
|
||||||
Loading…
Reference in New Issue