@@ -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}
0 commit comments