From e1497acef48559ce610ff430909559e646ecb6b1 Mon Sep 17 00:00:00 2001 From: YuKun Liu Date: Mon, 10 Oct 2022 22:38:30 -0700 Subject: [PATCH] feat: types --- src/plugin/mod.rs | 2 +- src/plugin/types.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/plugin/types.rs diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index e09eeced8..55e8883de 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -20,7 +20,7 @@ use self::{ }, }; -pub mod argument; +mod types; pub mod interface; lazy_static::lazy_static! { diff --git a/src/plugin/types.rs b/src/plugin/types.rs new file mode 100644 index 000000000..181e09ddc --- /dev/null +++ b/src/plugin/types.rs @@ -0,0 +1,119 @@ +use std::collections::HashMap; + +use mlua::ToLua; + +use super::LUA; + +#[derive(Debug, Clone)] +pub struct PluginConfig { + available: bool, + config_info: HashMap>, +} + +impl<'lua> ToLua<'lua> for PluginConfig { + fn to_lua(self, lua: &'lua mlua::Lua) -> mlua::Result> { + let table = lua.create_table()?; + + table.set("available", self.available)?; + + let config_info = lua.create_table()?; + + for (name, data) in self.config_info { + config_info.set(name, data)?; + } + + table.set("config_info", config_info)?; + + Ok(mlua::Value::Table(table)) + } +} + +impl PluginConfig { + pub fn from_toml_value(val: toml::Value) -> Self { + if let toml::Value::Table(tab) = val { + let available = tab + .get::<_>("available") + .unwrap_or(&toml::Value::Boolean(true)); + let available = available.as_bool().unwrap_or(true); + + let mut config_info = HashMap::new(); + + let lua = LUA.lock().unwrap(); + + for (name, value) in tab {} + + Self { + available, + config_info, + } + } else { + Self { + available: false, + config_info: HashMap::new(), + } + } + } +} + +#[derive(Debug, Clone)] +pub enum Value { + String(String), + Integer(i64), + Float(f64), + Boolean(bool), + Array(Vec), + Table(HashMap), +} + +impl Value { + pub fn from_toml(origin: toml::Value) -> Self { + match origin { + cargo_toml::Value::String(s) => Value::String(s), + cargo_toml::Value::Integer(i) => Value::Integer(i), + cargo_toml::Value::Float(f) => Value::Float(f), + cargo_toml::Value::Boolean(b) => Value::Boolean(b), + cargo_toml::Value::Datetime(d) => Value::String(d.to_string()), + cargo_toml::Value::Array(a) => { + let mut v = vec![]; + for i in a { + v.push(Value::from_toml(i)); + } + Value::Array(v) + }, + cargo_toml::Value::Table(t) => { + let mut h = HashMap::new(); + for (n, v) in t { + h.insert(n, Value::from_toml(v)); + } + Value::Table(h) + }, + } + } +} + +impl<'lua> ToLua<'lua> for Value { + fn to_lua(self, lua: &'lua mlua::Lua) -> mlua::Result> { + Ok( + match self { + Value::String(s) => mlua::Value::String(lua.create_string(&s)?), + Value::Integer(i) => mlua::Value::Integer(i), + Value::Float(f) => mlua::Value::Number(f), + Value::Boolean(b) => mlua::Value::Boolean(b), + Value::Array(a) => { + let table = lua.create_table()?; + for (i, v) in a.iter().enumerate() { + table.set(i, v.clone())?; + } + mlua::Value::Table(table) + }, + Value::Table(t) => { + let table = lua.create_table()?; + for (i, v) in t.iter() { + table.set(i.clone(), v.clone())?; + } + mlua::Value::Table(table) + }, + } + ) + } +} \ No newline at end of file