mirror of
https://github.com/lune-org/lune.git
synced 2025-04-04 10:30:54 +01:00
fix(net, serde): fix serde not accepting non BString
types
This commit is contained in:
parent
39b8c89e8b
commit
d1d7cb77e4
4 changed files with 8 additions and 16 deletions
|
@ -40,7 +40,7 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
|
|||
|
||||
fn net_json_encode<'lua>(
|
||||
lua: &'lua Lua,
|
||||
(val, pretty): (BString, Option<bool>),
|
||||
(val, pretty): (LuaValue<'lua>, Option<bool>),
|
||||
) -> LuaResult<LuaString<'lua>> {
|
||||
EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default()))
|
||||
.serialize_to_string(lua, val)
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::sync::{
|
|||
Arc,
|
||||
};
|
||||
|
||||
use bstr::{BString, ByteSlice};
|
||||
use mlua::prelude::*;
|
||||
|
||||
use futures_util::{
|
||||
|
@ -160,7 +161,7 @@ where
|
|||
|
||||
methods.add_async_method(
|
||||
"send",
|
||||
|_, this, (string, as_binary): (LuaString, Option<bool>)| async move {
|
||||
|_, this, (string, as_binary): (BString, Option<bool>)| async move {
|
||||
this.send(if as_binary.unwrap_or_default() {
|
||||
WsMessage::Binary(string.as_bytes().to_vec())
|
||||
} else {
|
||||
|
|
|
@ -57,14 +57,11 @@ impl EncodeDecodeConfig {
|
|||
pub fn serialize_to_string<'lua>(
|
||||
self,
|
||||
lua: &'lua Lua,
|
||||
value: BString,
|
||||
value: LuaValue<'lua>,
|
||||
) -> LuaResult<LuaString<'lua>> {
|
||||
let bytes = match self.format {
|
||||
EncodeDecodeFormat::Json => {
|
||||
let serialized: JsonValue = lua.from_value_with(
|
||||
LuaValue::String(lua.create_string(value)?),
|
||||
LUA_DESERIALIZE_OPTIONS,
|
||||
)?;
|
||||
let serialized: JsonValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
|
||||
if self.pretty {
|
||||
serde_json::to_vec_pretty(&serialized).into_lua_err()?
|
||||
} else {
|
||||
|
@ -72,19 +69,13 @@ impl EncodeDecodeConfig {
|
|||
}
|
||||
}
|
||||
EncodeDecodeFormat::Yaml => {
|
||||
let serialized: YamlValue = lua.from_value_with(
|
||||
LuaValue::String(lua.create_string(value)?),
|
||||
LUA_DESERIALIZE_OPTIONS,
|
||||
)?;
|
||||
let serialized: YamlValue = lua.from_value_with(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(
|
||||
LuaValue::String(lua.create_string(value)?),
|
||||
LUA_DESERIALIZE_OPTIONS,
|
||||
)?;
|
||||
let serialized: TomlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
|
||||
let s = if self.pretty {
|
||||
toml::to_string_pretty(&serialized).into_lua_err()?
|
||||
} else {
|
||||
|
|
|
@ -20,7 +20,7 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
|
|||
|
||||
fn serde_encode<'lua>(
|
||||
lua: &'lua Lua,
|
||||
(format, val, pretty): (EncodeDecodeFormat, BString, Option<bool>),
|
||||
(format, val, pretty): (EncodeDecodeFormat, LuaValue<'lua>, Option<bool>),
|
||||
) -> LuaResult<LuaString<'lua>> {
|
||||
let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default()));
|
||||
config.serialize_to_string(lua, val)
|
||||
|
|
Loading…
Add table
Reference in a new issue