Fix fs writeFile & readFile not working with invalid utf8

This commit is contained in:
Filip Tibell 2023-03-11 08:16:16 +01:00
parent 7261ca90ef
commit a59b0190ab
No known key found for this signature in database
2 changed files with 12 additions and 4 deletions

View file

@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
print("Hello, world!") print("Hello, world!")
``` ```
### Fixed
- Fixed `fs.writeFile` and `fs.readFile` not working with strings / files that are invalid utf-8
## `0.5.5` - March 8th, 2023 ## `0.5.5` - March 8th, 2023
### Added ### Added

View file

@ -19,8 +19,9 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.build_readonly() .build_readonly()
} }
async fn fs_read_file(_: &'static Lua, path: String) -> LuaResult<String> { async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult<LuaString> {
fs::read_to_string(&path).await.map_err(LuaError::external) let bytes = fs::read(&path).await.map_err(LuaError::external)?;
lua.create_string(&bytes)
} }
async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult<Vec<String>> { async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult<Vec<String>> {
@ -52,8 +53,11 @@ async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult<Vec<String>> {
Ok(dir_strings_no_prefix) Ok(dir_strings_no_prefix)
} }
async fn fs_write_file(_: &'static Lua, (path, contents): (String, String)) -> LuaResult<()> { async fn fs_write_file(
fs::write(&path, &contents) _: &'static Lua,
(path, contents): (String, LuaString<'_>),
) -> LuaResult<()> {
fs::write(&path, &contents.as_bytes())
.await .await
.map_err(LuaError::external) .map_err(LuaError::external)
} }