Add basic fs lib

This commit is contained in:
2025-06-25 10:40:36 +10:00
parent 69ac13ea47
commit e0898c22a0
8 changed files with 131 additions and 2 deletions

32
crates/lb/src/fs.rs Normal file
View File

@@ -0,0 +1,32 @@
//! The `lb:fs` module provides utilities for interacting with the file system asynchronously.
//!
//! See [`lb_libfs`] for items exported by this module.
use luaffi::{cdef, metatype};
use std::io;
use tokio::fs;
/// Items exported by the `lb:fs` module.
///
/// This module can be obtained by calling `require` in Lua.
///
/// ```lua
/// local fs = require("lb:fs");
/// ```
#[cdef]
pub struct lb_libfs;
#[metatype]
impl lb_libfs {
#[new]
extern "Lua-C" fn new() -> Self {
Self
}
pub extern "Lua" fn read(&self, path: string) -> string {
self.__read(path)
}
async extern "Lua-C" fn __read(&self, path: &str) -> io::Result<Vec<u8>> {
fs::read(path).await
}
}