41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
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)))
|
|
});
|
|
}
|
|
}
|