mirror of
https://github.com/wez/wezterm.git
synced 2024-11-22 13:16:39 +03:00
add YAML/TOML serialization/deserialization feature
This commit is contained in:
parent
61c1fd8e17
commit
8596422abf
26
Cargo.lock
generated
26
Cargo.lock
generated
@ -1555,7 +1555,6 @@ dependencies = [
|
||||
"dirs-next",
|
||||
"env_logger 0.10.2",
|
||||
"filesystem",
|
||||
"json",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"log",
|
||||
@ -1564,6 +1563,7 @@ dependencies = [
|
||||
"objc",
|
||||
"plugin",
|
||||
"procinfo-funcs",
|
||||
"serde-funcs",
|
||||
"share-data",
|
||||
"spawn-funcs",
|
||||
"ssh-funcs",
|
||||
@ -2776,17 +2776,6 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "json"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"config",
|
||||
"luahelper",
|
||||
"serde_json",
|
||||
"wezterm-dynamic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "k9"
|
||||
version = "0.11.6"
|
||||
@ -4766,6 +4755,19 @@ dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-funcs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"config",
|
||||
"luahelper",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"toml 0.8.9",
|
||||
"wezterm-dynamic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_cbor"
|
||||
version = "0.11.2"
|
||||
|
@ -23,7 +23,7 @@ logging = { path = "../lua-api-crates/logging" }
|
||||
mux-lua = { path = "../lua-api-crates/mux" }
|
||||
procinfo-funcs = { path = "../lua-api-crates/procinfo-funcs" }
|
||||
filesystem = { path = "../lua-api-crates/filesystem" }
|
||||
json = { path = "../lua-api-crates/json" }
|
||||
serde-funcs = { path = "../lua-api-crates/serde-funcs" }
|
||||
plugin = { path = "../lua-api-crates/plugin" }
|
||||
share-data = { path = "../lua-api-crates/share-data" }
|
||||
ssh-funcs = { path = "../lua-api-crates/ssh-funcs" }
|
||||
|
@ -199,7 +199,7 @@ fn register_lua_modules() {
|
||||
mux_lua::register,
|
||||
procinfo_funcs::register,
|
||||
filesystem::register,
|
||||
json::register,
|
||||
serde_funcs::register,
|
||||
plugin::register,
|
||||
ssh_funcs::register,
|
||||
spawn_funcs::register,
|
||||
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "json"
|
||||
name = "serde-funcs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
@ -11,3 +11,5 @@ config = { path = "../../config" }
|
||||
luahelper = { path = "../../luahelper" }
|
||||
wezterm-dynamic = { path = "../../wezterm-dynamic" }
|
||||
serde_json = "1.0.82"
|
||||
serde_yaml = "0.9.31"
|
||||
toml = "0.8.9"
|
@ -1,17 +1,67 @@
|
||||
use config::lua::get_or_create_module;
|
||||
use config::lua::{get_or_create_module, get_or_create_sub_module};
|
||||
use config::lua::mlua::{self, IntoLua, Lua, Value as LuaValue};
|
||||
use luahelper::lua_value_to_dynamic;
|
||||
use serde_json::{Map, Value as JValue};
|
||||
use std::collections::HashSet;
|
||||
use wezterm_dynamic::{FromDynamic, Value as DynValue};
|
||||
use serde_yaml;
|
||||
use toml;
|
||||
|
||||
|
||||
pub fn register(lua: &Lua) -> anyhow::Result<()> {
|
||||
let serde_mod = get_or_create_sub_module(lua, "serde")?;
|
||||
|
||||
serde_mod.set("json_encode", lua.create_function(json_encode)?)?;
|
||||
serde_mod.set("json_decode", lua.create_function(json_decode)?)?;
|
||||
serde_mod.set("yaml_encode", lua.create_function(yaml_encode)?)?;
|
||||
serde_mod.set("yaml_decode", lua.create_function(yaml_decode)?)?;
|
||||
serde_mod.set("toml_encode", lua.create_function(toml_encode)?)?;
|
||||
serde_mod.set("toml_decode", lua.create_function(toml_decode)?)?;
|
||||
|
||||
// For backward compatibility.
|
||||
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
|
||||
wezterm_mod.set("json_parse", lua.create_function(json_parse)?)?;
|
||||
wezterm_mod.set("json_parse", lua.create_function(json_decode)?)?;
|
||||
wezterm_mod.set("json_encode", lua.create_function(json_encode)?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn json_encode(_: &Lua, value: LuaValue) -> mlua::Result<String> {
|
||||
let mut visited = HashSet::new();
|
||||
let json = lua_value_to_json_value(value, &mut visited)?;
|
||||
serde_json::to_string(&json).map_err(|err| mlua::Error::external(format!("{err:#}")))
|
||||
}
|
||||
|
||||
fn yaml_encode(_: &Lua, value: LuaValue) -> mlua::Result<String> {
|
||||
let mut visited = HashSet::new();
|
||||
let json = lua_value_to_json_value(value, &mut visited)?;
|
||||
serde_yaml::to_string(&json).map_err(|err| mlua::Error::external(format!("{err:#}")))
|
||||
}
|
||||
|
||||
fn toml_encode(_: &Lua, value: LuaValue) -> mlua::Result<String> {
|
||||
let mut visited = HashSet::new();
|
||||
let json = lua_value_to_json_value(value, &mut visited)?;
|
||||
toml::to_string(&json).map_err(|err| mlua::Error::external(format!("{err:#}")))
|
||||
}
|
||||
|
||||
fn json_decode(lua: &Lua, text: String) -> mlua::Result<LuaValue> {
|
||||
let value = serde_json::from_str(&text)
|
||||
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
|
||||
json_value_to_lua_value(lua, value)
|
||||
}
|
||||
|
||||
fn yaml_decode(lua: &Lua, text: String) -> mlua::Result<LuaValue> {
|
||||
let value: JValue = serde_yaml::from_str(&text)
|
||||
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
|
||||
json_value_to_lua_value(lua, value)
|
||||
}
|
||||
|
||||
fn toml_decode(lua: &Lua, text: String) -> mlua::Result<LuaValue> {
|
||||
let value: JValue = toml::from_str(&text)
|
||||
.map_err(|err| mlua::Error::external(format!("{err:#}")))?;
|
||||
json_value_to_lua_value(lua, value)
|
||||
}
|
||||
|
||||
fn json_value_to_lua_value<'lua>(lua: &'lua Lua, value: JValue) -> mlua::Result<LuaValue> {
|
||||
Ok(match value {
|
||||
JValue::Null => LuaValue::Nil,
|
||||
@ -47,12 +97,6 @@ fn json_value_to_lua_value<'lua>(lua: &'lua Lua, value: JValue) -> mlua::Result<
|
||||
})
|
||||
}
|
||||
|
||||
fn json_parse<'lua>(lua: &'lua Lua, text: String) -> mlua::Result<LuaValue> {
|
||||
let value =
|
||||
serde_json::from_str(&text).map_err(|err| mlua::Error::external(format!("{err:#}")))?;
|
||||
json_value_to_lua_value(lua, value)
|
||||
}
|
||||
|
||||
fn dyn_to_json(value: DynValue) -> anyhow::Result<JValue> {
|
||||
Ok(match value {
|
||||
DynValue::Null => JValue::Null,
|
||||
@ -226,9 +270,3 @@ fn lua_value_to_json_value(value: LuaValue, visited: &mut HashSet<usize>) -> mlu
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn json_encode<'lua>(_: &'lua Lua, value: LuaValue) -> mlua::Result<String> {
|
||||
let mut visited = HashSet::new();
|
||||
let json = lua_value_to_json_value(value, &mut visited)?;
|
||||
serde_json::to_string(&json).map_err(|err| mlua::Error::external(format!("{err:#}")))
|
||||
}
|
Loading…
Reference in New Issue
Block a user