Add tempdir support

This commit is contained in:
2025-06-27 03:53:08 +10:00
parent 0c4639c3e9
commit cdfb2522ac
4 changed files with 64 additions and 1 deletions

View File

@@ -98,6 +98,14 @@ impl lb_fslib {
prefix,
})
}
pub extern "Lua-C" fn temp_dir(&self) -> Result<lb_temp_dir> {
Ok(tempfile::tempdir()?.into())
}
pub extern "Lua-C" fn temp_dir_in(&self, path: &str) -> Result<lb_temp_dir> {
Ok(tempfile::tempdir_in(path)?.into())
}
}
/// Iterator over the entries in a directory.
@@ -401,3 +409,20 @@ impl lb_glob_dir {
Ok(None)
}
}
/// Directory in the filesystem that is automatically deleted when it is garbage-collected.
#[derive(Debug, From)]
#[cdef]
pub struct lb_temp_dir(#[opaque] tempfile::TempDir);
#[metatype]
impl lb_temp_dir {
pub extern "Lua-C" fn path(&self) -> String {
self.0.path().to_string_lossy().into()
}
#[tostring]
pub extern "Lua" fn tostring(&self) -> String {
self.path()
}
}