From d64dab2a2b306966f469fd81a247e7819072cd8f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 2 Oct 2020 10:28:58 -0700 Subject: [PATCH] move lua helpers into own crate --- Cargo.lock | 15 ++++++- Cargo.toml | 2 +- luahelper/Cargo.toml | 16 ++++++++ luahelper/src/lib.rs | 39 +++++++++++++++++++ .../src}/serde_lua/mod.rs | 0 .../src}/serde_lua/ser.rs | 0 src/config/font.rs | 1 + src/config/keys.rs | 1 + src/config/mod.rs | 1 + src/frontend/mod.rs | 1 + src/keyassignment.rs | 1 + src/scripting/mod.rs | 33 +--------------- 12 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 luahelper/Cargo.toml create mode 100644 luahelper/src/lib.rs rename {src/scripting => luahelper/src}/serde_lua/mod.rs (100%) rename {src/scripting => luahelper/src}/serde_lua/ser.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 17e15b933..0b5e7d2fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1783,6 +1783,19 @@ dependencies = [ "cc", ] +[[package]] +name = "luahelper" +version = "0.1.0" +dependencies = [ + "bstr 0.2.13", + "log", + "mlua", + "serde", + "serde_json", + "strsim 0.10.0", + "thiserror", +] + [[package]] name = "luajit-src" version = "210.1.0+resty31116c4" @@ -4061,6 +4074,7 @@ dependencies = [ "libc", "log", "lru", + "luahelper", "metrics", "mlua", "notify", @@ -4082,7 +4096,6 @@ dependencies = [ "serial", "shared_library", "ssh2", - "strsim 0.10.0", "structopt", "tabout", "terminfo", diff --git a/Cargo.toml b/Cargo.toml index 86c835baf..a0dc0311d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ leb128 = "0.2" libc = "0.2" log = "0.4" lru = "0.5" +luahelper = { path = "luahelper" } open = "1.2" pulldown-cmark = "0.7" metrics = { version="0.12", features=["std"]} @@ -66,7 +67,6 @@ serde_json = "1.0" serial = "0.4" ssh2 = "0.8" structopt = "0.3" -strsim = "0.10" tabout = { path = "tabout" } wezterm-term = { path = "term", features=["use_serde"] } terminfo = "0.7" diff --git a/luahelper/Cargo.toml b/luahelper/Cargo.toml new file mode 100644 index 000000000..d14159544 --- /dev/null +++ b/luahelper/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "luahelper" +version = "0.1.0" +authors = ["Wez Furlong "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bstr = "0.2" +log = "0.4" +mlua = {version="0.2", features=["vendored"]} +serde = {version="1.0", features = ["rc", "derive"]} +serde_json = "1.0" +strsim = "0.10" +thiserror = "1.0" diff --git a/luahelper/src/lib.rs b/luahelper/src/lib.rs new file mode 100644 index 000000000..44d0585ef --- /dev/null +++ b/luahelper/src/lib.rs @@ -0,0 +1,39 @@ +#![macro_use] + +mod serde_lua; +pub use serde_lua::from_lua_value; +pub use serde_lua::ser::to_lua_value; + +/// Implement lua conversion traits for a type. +/// This implementation requires that the type implement +/// serde Serialize and Deserialize. +/// Why do we need these traits? They allow `create_function` to +/// operate in terms of our internal types rather than forcing +/// the implementer to use generic Value parameter or return values. +#[macro_export] +macro_rules! impl_lua_conversion { + ($struct:ident) => { + impl<'lua> mlua::ToLua<'lua> for $struct { + fn to_lua(self, lua: &'lua mlua::Lua) -> Result, mlua::Error> { + Ok(crate::scripting::to_lua_value(lua, self)?) + } + } + + impl<'lua> mlua::FromLua<'lua> for $struct { + fn from_lua( + value: mlua::Value<'lua>, + _lua: &'lua mlua::Lua, + ) -> Result { + Ok(crate::scripting::from_lua_value(value)?) + } + } + }; +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/scripting/serde_lua/mod.rs b/luahelper/src/serde_lua/mod.rs similarity index 100% rename from src/scripting/serde_lua/mod.rs rename to luahelper/src/serde_lua/mod.rs diff --git a/src/scripting/serde_lua/ser.rs b/luahelper/src/serde_lua/ser.rs similarity index 100% rename from src/scripting/serde_lua/ser.rs rename to luahelper/src/serde_lua/ser.rs diff --git a/src/config/font.rs b/src/config/font.rs index ce54f9752..045080269 100644 --- a/src/config/font.rs +++ b/src/config/font.rs @@ -1,4 +1,5 @@ use crate::config::*; +use luahelper::impl_lua_conversion; use termwiz::color::RgbColor; #[derive(Debug, Copy, Deserialize, Serialize, Clone, PartialEq, Eq, Hash)] diff --git a/src/config/keys.rs b/src/config/keys.rs index a396cff44..f0ac6ac14 100644 --- a/src/config/keys.rs +++ b/src/config/keys.rs @@ -1,4 +1,5 @@ use crate::keyassignment::{KeyAssignment, MouseEventTrigger}; +use luahelper::impl_lua_conversion; use serde::{Deserialize, Deserializer, Serialize}; use termwiz::input::{KeyCode, Modifiers}; diff --git a/src/config/mod.rs b/src/config/mod.rs index ae47688f4..8fff5dc1c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -8,6 +8,7 @@ use crate::frontend::FrontEndSelection; use crate::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnCommand}; use anyhow::{anyhow, bail, Context, Error}; use lazy_static::lazy_static; +use luahelper::impl_lua_conversion; use portable_pty::{CommandBuilder, PtySize}; use serde::{Deserialize, Serialize}; use std; diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 2b3754bb1..0a03da6b0 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -3,6 +3,7 @@ use crate::mux::tab::Tab; use crate::mux::window::WindowId; use anyhow::{anyhow, Error}; use downcast_rs::{impl_downcast, Downcast}; +use luahelper::impl_lua_conversion; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::rc::Rc; diff --git a/src/keyassignment.rs b/src/keyassignment.rs index 59102f613..472fd7db6 100644 --- a/src/keyassignment.rs +++ b/src/keyassignment.rs @@ -3,6 +3,7 @@ use crate::config::LeaderKey; use crate::frontend::gui::SelectionMode; use crate::mux::domain::DomainId; use crate::mux::pane::Pattern; +use luahelper::impl_lua_conversion; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index d45c53dda..0e1f1f85e 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -1,42 +1,11 @@ -#![macro_use] - use crate::config::{FontAttributes, TextStyle}; use anyhow::anyhow; use bstr::BString; +pub use luahelper::*; use mlua::{Lua, Table, Value}; use serde::*; use std::path::Path; -mod serde_lua; - -pub use serde_lua::from_lua_value; -pub use serde_lua::ser::to_lua_value; - -/// Implement lua conversion traits for a type. -/// This implementation requires that the type implement -/// serde Serialize and Deserialize. -/// Why do we need these traits? They allow `create_function` to -/// operate in terms of our internal types rather than forcing -/// the implementer to use generic Value parameter or return values. -macro_rules! impl_lua_conversion { - ($struct:ident) => { - impl<'lua> mlua::ToLua<'lua> for $struct { - fn to_lua(self, lua: &'lua mlua::Lua) -> Result, mlua::Error> { - Ok(crate::scripting::to_lua_value(lua, self)?) - } - } - - impl<'lua> mlua::FromLua<'lua> for $struct { - fn from_lua( - value: mlua::Value<'lua>, - _lua: &'lua mlua::Lua, - ) -> Result { - Ok(crate::scripting::from_lua_value(value)?) - } - } - }; -} - /// Set up a lua context for executing some code. /// The path to the directory containing the configuration is /// passed in and is used to pre-set some global values in