luby/crates/lb/src/fs.rs

44 lines
1.0 KiB
Rust

//! The `lb:fs` library provides synchronous and asynchronous utilities for interacting with the
//! file system.
//!
//! # Exports
//!
//! See [`lb_fslib`] for items exported by this library.
use luaffi::{cdef, metatype};
use std::io;
use tokio::fs;
/// Items exported by the `lb:fs` library.
///
/// This library can be obtained by calling `require` in Lua.
///
/// ```lua
/// local fs = require("lb:fs");
/// ```
#[cdef(module = "lb:fs")]
pub struct lb_fslib;
#[metatype]
impl lb_fslib {
#[new]
extern "Lua-C" fn new() -> Self {
Self
}
pub async extern "Lua-C" fn read(&self, path: &str) -> io::Result<Vec<u8>> {
fs::read(path).await
}
pub extern "Lua-C" fn read_sync(&self, path: &str) -> io::Result<Vec<u8>> {
std::fs::read(path)
}
pub async extern "Lua-C" fn write(&self, path: &str, contents: &[u8]) -> io::Result<()> {
fs::write(path, contents).await
}
pub extern "Lua-C" fn write_sync(&self, path: &str, contents: &[u8]) -> io::Result<()> {
std::fs::write(path, contents)
}
}