Add basic glob function

This commit is contained in:
2025-06-26 23:56:09 +10:00
parent b1572cc9f1
commit 2964ebbe1b
3 changed files with 31 additions and 1 deletions

View File

@@ -9,11 +9,13 @@ repository.workspace = true
[features]
task = ["tokio/rt", "tokio/time"]
fs = ["tokio/fs"]
fs = ["tokio/fs", "dep:glob"]
net = ["tokio/net"]
glob = ["dep:glob"]
[dependencies]
derive_more = { version = "2.0.1", features = ["full"] }
glob = { version = "0.3.2", optional = true }
luaffi = { path = "../luaffi" }
luajit = { path = "../luajit" }
sysexits = "0.9.0"

View File

@@ -26,6 +26,12 @@ pub enum Error {
/// I/O error.
#[error("{0}")]
Io(#[from] std::io::Error),
/// Glob error.
#[error("{0}")]
Glob(#[from] glob::GlobError),
/// Glob pattern error.
#[error("{0}")]
GlobPattern(#[from] glob::PatternError),
}
type Result<T> = std::result::Result<T, Error>;
@@ -70,6 +76,10 @@ impl lb_fslib {
pub extern "Lua-C" fn read_dir_sync(&self, path: &str) -> Result<lb_read_dir_sync> {
Ok(std::fs::read_dir(path)?.into())
}
pub extern "Lua-C" fn glob(&self, pattern: &str) -> Result<lb_glob_paths> {
Ok(glob::glob(pattern)?.into())
}
}
/// Iterator over the entries in a directory.
@@ -289,3 +299,20 @@ impl lb_file_perms {
self.0.set_readonly(readonly);
}
}
/// Iterator that yields paths from the filesystem that match a particular pattern.
#[derive(Debug, From)]
#[cdef]
pub struct lb_glob_paths(#[opaque] glob::Paths);
#[metatype]
impl lb_glob_paths {
#[call]
pub extern "Lua-C" fn next(&mut self) -> Result<Option<String>> {
Ok(self
.0
.next()
.transpose()?
.map(|s| s.to_string_lossy().into()))
}
}