1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

move lua helpers into own crate

This commit is contained in:
Wez Furlong 2020-10-02 10:28:58 -07:00
parent 52aa9cf71b
commit d64dab2a2b
12 changed files with 76 additions and 34 deletions

15
Cargo.lock generated
View File

@ -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",

View File

@ -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"

16
luahelper/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "luahelper"
version = "0.1.0"
authors = ["Wez Furlong <wez@wezfurlong.org>"]
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"

39
luahelper/src/lib.rs Normal file
View File

@ -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::Value<'lua>, 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<Self, mlua::Error> {
Ok(crate::scripting::from_lua_value(value)?)
}
}
};
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -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)]

View File

@ -1,4 +1,5 @@
use crate::keyassignment::{KeyAssignment, MouseEventTrigger};
use luahelper::impl_lua_conversion;
use serde::{Deserialize, Deserializer, Serialize};
use termwiz::input::{KeyCode, Modifiers};

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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::Value<'lua>, 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<Self, mlua::Error> {
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