mirror of
https://github.com/lune-org/lune.git
synced 2025-04-15 09:23:49 +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>(
|
fn net_json_encode<'lua>(
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
(val, pretty): (BString, Option<bool>),
|
(val, pretty): (LuaValue<'lua>, Option<bool>),
|
||||||
) -> LuaResult<LuaString<'lua>> {
|
) -> LuaResult<LuaString<'lua>> {
|
||||||
EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default()))
|
EncodeDecodeConfig::from((EncodeDecodeFormat::Json, pretty.unwrap_or_default()))
|
||||||
.serialize_to_string(lua, val)
|
.serialize_to_string(lua, val)
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::sync::{
|
||||||
Arc,
|
Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use bstr::{BString, ByteSlice};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use futures_util::{
|
use futures_util::{
|
||||||
|
@ -160,7 +161,7 @@ where
|
||||||
|
|
||||||
methods.add_async_method(
|
methods.add_async_method(
|
||||||
"send",
|
"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() {
|
this.send(if as_binary.unwrap_or_default() {
|
||||||
WsMessage::Binary(string.as_bytes().to_vec())
|
WsMessage::Binary(string.as_bytes().to_vec())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -57,14 +57,11 @@ impl EncodeDecodeConfig {
|
||||||
pub fn serialize_to_string<'lua>(
|
pub fn serialize_to_string<'lua>(
|
||||||
self,
|
self,
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
value: BString,
|
value: LuaValue<'lua>,
|
||||||
) -> LuaResult<LuaString<'lua>> {
|
) -> LuaResult<LuaString<'lua>> {
|
||||||
let bytes = match self.format {
|
let bytes = match self.format {
|
||||||
EncodeDecodeFormat::Json => {
|
EncodeDecodeFormat::Json => {
|
||||||
let serialized: JsonValue = lua.from_value_with(
|
let serialized: JsonValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
|
||||||
LuaValue::String(lua.create_string(value)?),
|
|
||||||
LUA_DESERIALIZE_OPTIONS,
|
|
||||||
)?;
|
|
||||||
if self.pretty {
|
if self.pretty {
|
||||||
serde_json::to_vec_pretty(&serialized).into_lua_err()?
|
serde_json::to_vec_pretty(&serialized).into_lua_err()?
|
||||||
} else {
|
} else {
|
||||||
|
@ -72,19 +69,13 @@ impl EncodeDecodeConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EncodeDecodeFormat::Yaml => {
|
EncodeDecodeFormat::Yaml => {
|
||||||
let serialized: YamlValue = lua.from_value_with(
|
let serialized: YamlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
|
||||||
LuaValue::String(lua.create_string(value)?),
|
|
||||||
LUA_DESERIALIZE_OPTIONS,
|
|
||||||
)?;
|
|
||||||
let mut writer = Vec::with_capacity(128);
|
let mut writer = Vec::with_capacity(128);
|
||||||
serde_yaml::to_writer(&mut writer, &serialized).into_lua_err()?;
|
serde_yaml::to_writer(&mut writer, &serialized).into_lua_err()?;
|
||||||
writer
|
writer
|
||||||
}
|
}
|
||||||
EncodeDecodeFormat::Toml => {
|
EncodeDecodeFormat::Toml => {
|
||||||
let serialized: TomlValue = lua.from_value_with(
|
let serialized: TomlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?;
|
||||||
LuaValue::String(lua.create_string(value)?),
|
|
||||||
LUA_DESERIALIZE_OPTIONS,
|
|
||||||
)?;
|
|
||||||
let s = if self.pretty {
|
let s = if self.pretty {
|
||||||
toml::to_string_pretty(&serialized).into_lua_err()?
|
toml::to_string_pretty(&serialized).into_lua_err()?
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub fn create(lua: &Lua) -> LuaResult<LuaTable> {
|
||||||
|
|
||||||
fn serde_encode<'lua>(
|
fn serde_encode<'lua>(
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
(format, val, pretty): (EncodeDecodeFormat, BString, Option<bool>),
|
(format, val, pretty): (EncodeDecodeFormat, LuaValue<'lua>, Option<bool>),
|
||||||
) -> LuaResult<LuaString<'lua>> {
|
) -> LuaResult<LuaString<'lua>> {
|
||||||
let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default()));
|
let config = EncodeDecodeConfig::from((format, pretty.unwrap_or_default()));
|
||||||
config.serialize_to_string(lua, val)
|
config.serialize_to_string(lua, val)
|
||||||
|
|
Loading…
Add table
Reference in a new issue