Skip to content

Commit 368a1d4

Browse files
committed
plugins: drop dead code
Whatever I had in mind, is not going to work.
1 parent 0541ce1 commit 368a1d4

File tree

5 files changed

+51
-130
lines changed

5 files changed

+51
-130
lines changed

src/interface.rs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -563,59 +563,44 @@ fn run_plugin_function(
563563
args: Vec<String>,
564564
) -> Result<()> {
565565
// Get plugin instance from manager
566-
let plugin_meta = pm
567-
.get_plugin(plugin_name)
566+
let plugin = pm
567+
.get_plugin_instance(plugin_name)
568568
.ok_or_else(|| color_eyre::eyre::eyre!("Plugin not found: {}", plugin_name))?;
569569

570+
// Check if the function exists
571+
if !plugin.has_function(function_name) {
572+
return Err(color_eyre::eyre::eyre!(
573+
"Function '{}' not found in plugin '{}'",
574+
function_name,
575+
plugin_name
576+
));
577+
}
578+
570579
// Create Lua values from the arguments
571-
let lua = pm.lua();
580+
let lua = plugin.get_lua();
572581
let lua_args = args
573582
.iter()
574583
.map(|arg| mlua::Value::String(lua.create_string(arg).unwrap()))
575584
.collect::<Vec<_>>();
576585

577-
// Get the plugin table and check if the function exists
578-
let plugin_table = lua
579-
.globals()
580-
.get::<mlua::Table>(&*plugin_meta.name)
581-
.map_err(|e| color_eyre::eyre::eyre!("Error accessing plugin table: {}", e))?;
582-
583-
if plugin_table
584-
.contains_key(function_name)
585-
.map_err(|e| color_eyre::eyre::eyre!("Error checking function existence: {}", e))?
586-
{
587-
let function = plugin_table
588-
.get::<mlua::Function>(function_name)
589-
.map_err(|e| color_eyre::eyre::eyre!("Error getting function: {}", e))?;
590-
591-
// Call the function with proper arguments
592-
let args_multi = mlua::MultiValue::from_vec(lua_args);
593-
let result = function
594-
.call(args_multi)
595-
.map_err(|e| color_eyre::eyre::eyre!("Error executing plugin function: {}", e))?;
596-
597-
// Print the result
598-
match result {
599-
mlua::Value::Nil => println!("Function executed successfully (no result)"),
600-
mlua::Value::Boolean(b) => println!("Result: {}", b),
601-
mlua::Value::Integer(i) => println!("Result: {}", i),
602-
mlua::Value::Number(n) => println!("Result: {}", n),
603-
mlua::Value::String(s) => println!(
604-
"Result: {}",
605-
s.to_str()
606-
.map_err(|e| color_eyre::eyre::eyre!("Error converting string: {}", e))?
607-
),
608-
mlua::Value::Table(_) => println!("Result: [table]"),
609-
mlua::Value::Function(_) => println!("Result: [function]"),
610-
_ => println!("Result: [other]"),
611-
}
612-
613-
Ok(())
614-
} else {
615-
Err(color_eyre::eyre::eyre!(
616-
"Function '{}' not found in plugin '{}'",
617-
function_name,
618-
plugin_name
619-
))
586+
// Execute the function with the args
587+
let result = plugin.execute_function(function_name, lua_args)?;
588+
589+
// Print the result
590+
match result {
591+
mlua::Value::Nil => println!("Function executed successfully (no result)"),
592+
mlua::Value::Boolean(b) => println!("Result: {}", b),
593+
mlua::Value::Integer(i) => println!("Result: {}", i),
594+
mlua::Value::Number(n) => println!("Result: {}", n),
595+
mlua::Value::String(s) => println!(
596+
"Result: {}",
597+
s.to_str()
598+
.map_err(|e| color_eyre::eyre::eyre!("Error converting string: {}", e))?
599+
),
600+
mlua::Value::Table(_) => println!("Result: [table]"),
601+
mlua::Value::Function(_) => println!("Result: [function]"),
602+
_ => println!("Result: [other]"),
620603
}
604+
605+
Ok(())
621606
}

src/plugins/config.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ impl PluginConfig {
6464

6565
/// Save plugin configuration to disk
6666
pub fn save(&self) -> Result<()> {
67-
let config_paths = Self::get_config_paths();
68-
6967
// Try to save to the first config path that's writable
7068
if let Some(config_dir) = dirs::config_dir() {
7169
let config_file = config_dir.join("nh").join("plugins.toml");

src/plugins/manager.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use std::collections::HashMap;
44
use std::path::{Path, PathBuf};
5-
use std::sync::{Arc, Mutex};
5+
use std::rc::Rc;
6+
use std::sync::Mutex;
67

78
use color_eyre::eyre::{bail, eyre};
89
use mlua::Lua;
@@ -18,7 +19,7 @@ use crate::Result;
1819
#[derive(Debug)]
1920
pub struct PluginManager {
2021
/// Lua state
21-
lua: Arc<Lua>,
22+
lua: Rc<Lua>,
2223
/// Loaded plugins
2324
plugins: Mutex<HashMap<String, Plugin>>,
2425
/// Plugin hooks
@@ -37,7 +38,7 @@ impl PluginManager {
3738
let config = PluginConfig::load()?;
3839

3940
Ok(Self {
40-
lua: Arc::new(lua),
41+
lua: Rc::new(lua),
4142
plugins: Mutex::new(HashMap::new()),
4243
hooks,
4344
config,
@@ -163,7 +164,7 @@ impl PluginManager {
163164
let plugin = Plugin::new(
164165
name.to_string(),
165166
path.to_path_buf(),
166-
Arc::clone(&self.lua),
167+
Rc::clone(&self.lua),
167168
&self.hooks,
168169
)?;
169170

@@ -196,6 +197,12 @@ impl PluginManager {
196197
plugins.get(name).map(|p| p.get_metadata().clone())
197198
}
198199

200+
/// Get a direct reference to a plugin by name
201+
pub fn get_plugin_instance(&self, name: &str) -> Option<Plugin> {
202+
let plugins = self.plugins.lock().unwrap();
203+
plugins.get(name).cloned()
204+
}
205+
199206
/// Reload a specific plugin
200207
pub fn reload_plugin(&self, name: &str) -> Result<()> {
201208
let mut plugins = self.plugins.lock().unwrap();

src/plugins/plugin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fs;
22
use std::path::{Path, PathBuf};
3-
use std::sync::Arc;
3+
use std::rc::Rc;
44

55
use color_eyre::eyre::{eyre, Context};
66
use mlua::{Function, Lua, Table};
@@ -15,15 +15,15 @@ pub struct Plugin {
1515
/// Plugin metadata
1616
pub metadata: PluginMetadata,
1717
/// Lua state reference
18-
lua: Arc<Lua>,
18+
lua: Rc<Lua>,
1919
}
2020

2121
impl Plugin {
2222
/// Create a new plugin instance
2323
pub fn new(
2424
name: String,
2525
path: PathBuf,
26-
lua: Arc<Lua>,
26+
lua: Rc<Lua>,
2727
hook_manager: &HookManager,
2828
) -> Result<Self> {
2929
let code = fs::read_to_string(&path)
@@ -220,7 +220,7 @@ impl Plugin {
220220
}
221221

222222
/// Get the Lua state reference
223-
pub const fn get_lua(&self) -> &Arc<Lua> {
223+
pub const fn get_lua(&self) -> &Rc<Lua> {
224224
&self.lua
225225
}
226226

@@ -232,7 +232,7 @@ impl Plugin {
232232
info!("Reloading plugin: {}", name);
233233

234234
// Create a new instance with the same name and path
235-
let new_plugin = Self::new(name, path, Arc::clone(&self.lua), hook_manager)?;
235+
let new_plugin = Self::new(name, path, Rc::clone(&self.lua), hook_manager)?;
236236

237237
// Update this instance with the new plugin's data
238238
self.metadata = new_plugin.metadata;

src/plugins/types.rs

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::path::PathBuf;
33

4-
use mlua::{Lua, Table, Value};
4+
use mlua::{Lua, Table};
55

66
/// Plugin metadata
77
#[derive(Debug, Clone)]
@@ -137,9 +137,7 @@ pub enum PluginResult {
137137
}
138138

139139
/// Plugin execution context
140-
pub struct PluginContext<'lua> {
141-
/// Lua state
142-
pub lua: &'lua Lua,
140+
pub struct PluginContext {
143141
/// Command being executed
144142
pub command: Option<String>,
145143
/// Arguments for the command
@@ -150,28 +148,10 @@ pub struct PluginContext<'lua> {
150148
pub category: CommandCategory,
151149
}
152150

153-
impl<'lua> PluginContext<'lua> {
154-
/// Create a new plugin context
155-
pub fn new(lua: &'lua Lua, command: Option<String>, args: Vec<String>) -> Self {
156-
let data = lua
157-
.create_table()
158-
.expect("Failed to create context data table");
159-
160-
// By default, treat commands as user commands
161-
let category = CommandCategory::User;
162-
163-
Self {
164-
lua,
165-
command,
166-
args,
167-
data,
168-
category,
169-
}
170-
}
171-
151+
impl PluginContext {
172152
/// Create a new plugin context with specified category
173153
pub fn with_category(
174-
lua: &'lua Lua,
154+
lua: &Lua,
175155
command: Option<String>,
176156
args: Vec<String>,
177157
category: CommandCategory,
@@ -181,59 +161,10 @@ impl<'lua> PluginContext<'lua> {
181161
.expect("Failed to create context data table");
182162

183163
Self {
184-
lua,
185164
command,
186165
args,
187166
data,
188167
category,
189168
}
190169
}
191-
192-
/// Add a value to the context data
193-
pub fn add_data(&self, key: &str, value: Value) -> mlua::Result<()> {
194-
self.data.set(key, value)
195-
}
196-
197-
/// Get a value from the context data
198-
pub fn get_data<T: mlua::FromLua>(&self, key: &str) -> mlua::Result<T> {
199-
self.data.get(key)
200-
}
201-
202-
/// Convert the context to a Lua table
203-
pub fn to_lua_table(&self) -> mlua::Result<Table> {
204-
let ctx_table = self.lua.create_table()?;
205-
206-
// Set command if available
207-
if let Some(cmd) = &self.command {
208-
ctx_table.set("command", cmd.clone())?;
209-
} else {
210-
ctx_table.set("command", mlua::Nil)?;
211-
}
212-
213-
// Set arguments
214-
let args_table = self.lua.create_table()?;
215-
for (i, arg) in self.args.iter().enumerate() {
216-
args_table.set(i + 1, arg.clone())?;
217-
}
218-
ctx_table.set("args", args_table)?;
219-
220-
// Set data
221-
ctx_table.set("data", self.data.clone())?;
222-
223-
// Set category
224-
let category_str = match self.category {
225-
CommandCategory::User => "user",
226-
CommandCategory::System => "system",
227-
CommandCategory::Plugin => "plugin",
228-
CommandCategory::Any => "any",
229-
};
230-
ctx_table.set("category", category_str)?;
231-
232-
Ok(ctx_table)
233-
}
234-
235-
/// Execute Lua code in this context
236-
pub fn execute_lua(&self, code: &str) -> mlua::Result<Value> {
237-
self.lua.load(code).eval()
238-
}
239170
}

0 commit comments

Comments
 (0)