From 8dbe5325ec9de6ca89d009cdbabab1c829e3d1a0 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 20 Apr 2024 14:22:45 +0530 Subject: [PATCH] feat: return buffers for fs too --- src/lune/builtins/fs/mod.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/lune/builtins/fs/mod.rs b/src/lune/builtins/fs/mod.rs index 8cda26d..7478118 100644 --- a/src/lune/builtins/fs/mod.rs +++ b/src/lune/builtins/fs/mod.rs @@ -31,10 +31,10 @@ pub fn create(lua: &Lua) -> LuaResult { .build_readonly() } -async fn fs_read_file(lua: &Lua, path: String) -> LuaResult { +async fn fs_read_file(lua: &Lua, path: String) -> LuaResult { let bytes = fs::read(&path).await.into_lua_err()?; - lua.create_string(bytes) + lua.create_buffer(bytes) } async fn fs_read_dir(_: &Lua, path: String) -> LuaResult> { @@ -66,19 +66,8 @@ async fn fs_read_dir(_: &Lua, path: String) -> LuaResult> { Ok(dir_strings_no_prefix) } -async fn fs_write_file(lua: &Lua, (path, contents): (String, LuaValue<'_>)) -> LuaResult<()> { - let contents_str = match contents { - LuaValue::String(str) => Ok(BString::from(str.to_str()?)), - LuaValue::UserData(inner) => lua.unpack::(LuaValue::UserData(inner)), - other => Err(LuaError::runtime(format!( - "Expected type string or buffer, got {}", - other.type_name() - ))), - }?; - - fs::write(&path, contents_str.as_bytes()) - .await - .into_lua_err() +async fn fs_write_file(_: &Lua, (path, contents): (String, BString)) -> LuaResult<()> { + fs::write(&path, contents.as_bytes()).await.into_lua_err() } async fn fs_write_dir(_: &Lua, path: String) -> LuaResult<()> {