feat: accept and return buffers in @lune/net and @lune/serde

This commit is contained in:
Erica Marigold 2024-04-20 14:17:58 +05:30
parent d3d1a4dbd5
commit 89d66a93de
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
3 changed files with 21 additions and 9 deletions

View file

@ -1,5 +1,6 @@
#![allow(unused_variables)]
use bstr::BString;
use mlua::prelude::*;
use mlua_luau_scheduler::LuaSpawnExt;
@ -39,13 +40,13 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
fn net_json_encode<'lua>(
lua: &'lua Lua,
(val, pretty): (LuaValue<'lua>, Option<bool>),
(val, pretty): (BString, Option<bool>),
) -> LuaResult<LuaString<'lua>> {
EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default()))
.serialize_to_string(lua, val)
}
fn net_json_decode<'lua>(lua: &'lua Lua, json: LuaString<'lua>) -> LuaResult<LuaValue<'lua>> {
fn net_json_decode<'lua>(lua: &'lua Lua, json: BString) -> LuaResult<LuaValue<'lua>> {
EncodeDecodeConfig::from(EncodeDecodeFormat::Json).deserialize_from_string(lua, json)
}

View file

@ -1,3 +1,4 @@
use bstr::{BString, ByteSlice};
use mlua::prelude::*;
use serde_json::Value as JsonValue;
@ -56,11 +57,14 @@ impl EncodeDecodeConfig {
pub fn serialize_to_string<'lua>(
self,
lua: &'lua Lua,
value: LuaValue<'lua>,
value: BString,
) -> LuaResult<LuaString<'lua>> {
let bytes = match self.format {
EncodeDecodeFormat::Json => {
let serialized: JsonValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
let serialized: JsonValue = lua.from_value_with(
LuaValue::String(lua.create_string(value)?),
LUA_DESERIALIZE_OPTIONS,
)?;
if self.pretty {
serde_json::to_vec_pretty(&serialized).into_lua_err()?
} else {
@ -68,13 +72,19 @@ impl EncodeDecodeConfig {
}
}
EncodeDecodeFormat::Yaml => {
let serialized: YamlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
let serialized: YamlValue = lua.from_value_with(
LuaValue::String(lua.create_string(value)?),
LUA_DESERIALIZE_OPTIONS,
)?;
let mut writer = Vec::with_capacity(128);
serde_yaml::to_writer(&mut writer, &serialized).into_lua_err()?;
writer
}
EncodeDecodeFormat::Toml => {
let serialized: TomlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
let serialized: TomlValue = lua.from_value_with(
LuaValue::String(lua.create_string(value)?),
LUA_DESERIALIZE_OPTIONS,
)?;
let s = if self.pretty {
toml::to_string_pretty(&serialized).into_lua_err()?
} else {
@ -89,7 +99,7 @@ impl EncodeDecodeConfig {
pub fn deserialize_from_string<'lua>(
self,
lua: &'lua Lua,
string: LuaString<'lua>,
string: BString,
) -> LuaResult<LuaValue<'lua>> {
let bytes = string.as_bytes();
match self.format {

View file

@ -1,3 +1,4 @@
use bstr::BString;
use mlua::prelude::*;
pub(super) mod compress_decompress;
@ -19,7 +20,7 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
fn serde_encode<'lua>(
lua: &'lua Lua,
(format, val, pretty): (EncodeDecodeFormat, LuaValue<'lua>, Option<bool>),
(format, val, pretty): (EncodeDecodeFormat, BString, Option<bool>),
) -> LuaResult<LuaString<'lua>> {
let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default()));
config.serialize_to_string(lua, val)
@ -27,7 +28,7 @@ fn serde_encode<'lua>(
fn serde_decode<'lua>(
lua: &'lua Lua,
(format, str): (EncodeDecodeFormat, LuaString<'lua>),
(format, str): (EncodeDecodeFormat, BString),
) -> LuaResult<LuaValue<'lua>> {
let config = EncodeDecodeConfig::from(format);
config.deserialize_from_string(lua, str)