From 2964ebbe1b2812a6d3f16030307699773a8df9a0 Mon Sep 17 00:00:00 2001 From: luaneko Date: Thu, 26 Jun 2025 23:56:09 +1000 Subject: [PATCH] Add basic glob function --- Cargo.lock | 1 + crates/lb/Cargo.toml | 4 +++- crates/lb/src/fs.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 0edbc65..148146b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1032,6 +1032,7 @@ name = "lb" version = "0.0.1" dependencies = [ "derive_more", + "glob", "luaffi", "luajit", "sysexits", diff --git a/crates/lb/Cargo.toml b/crates/lb/Cargo.toml index ec8a46f..0005354 100644 --- a/crates/lb/Cargo.toml +++ b/crates/lb/Cargo.toml @@ -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" diff --git a/crates/lb/src/fs.rs b/crates/lb/src/fs.rs index 1c3bc99..31fdf8d 100644 --- a/crates/lb/src/fs.rs +++ b/crates/lb/src/fs.rs @@ -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 = std::result::Result; @@ -70,6 +76,10 @@ impl lb_fslib { pub extern "Lua-C" fn read_dir_sync(&self, path: &str) -> Result { Ok(std::fs::read_dir(path)?.into()) } + + pub extern "Lua-C" fn glob(&self, pattern: &str) -> Result { + 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> { + Ok(self + .0 + .next() + .transpose()? + .map(|s| s.to_string_lossy().into())) + } +}