11use std:: collections:: HashMap ;
2- use std:: ffi:: OsStr ;
3- use std:: io;
2+ use std:: io:: { Error as IOError , Result as IOResult } ;
43use std:: path:: PathBuf ;
54use std:: sync:: Arc ;
65
@@ -13,48 +12,44 @@ use libloading::Library;
1312use tokio:: runtime:: Handle ;
1413use tokio:: sync:: Mutex ;
1514
16- use http_server_plugin:: {
17- Function , InvocationError , PluginDeclaration , CORE_VERSION , RUSTC_VERSION ,
18- } ;
15+ use http_server_plugin:: { Plugin , PluginDeclaration , PluginError , CORE_VERSION , RUSTC_VERSION } ;
1916
20- /// A proxy object which wraps a [`Function`] and makes sure it can't outlive
17+ use crate :: plugins_path;
18+
19+ /// A proxy object which wraps a [`Plugin`] and makes sure it can't outlive
2120/// the library it came from.
2221#[ derive( Clone ) ]
23- pub struct FunctionProxy {
24- function : Arc < dyn Function > ,
22+ pub struct PluginProxy {
23+ function : Arc < dyn Plugin > ,
2524 _lib : Arc < Library > ,
2625}
2726
2827#[ async_trait]
29- impl Function for FunctionProxy {
30- async fn call (
31- & self ,
32- parts : Parts ,
33- bytes : Bytes ,
34- ) -> Result < Response < Full < Bytes > > , InvocationError > {
28+ impl Plugin for PluginProxy {
29+ async fn call ( & self , parts : Parts , bytes : Bytes ) -> Result < Response < Full < Bytes > > , PluginError > {
3530 self . function . call ( parts, bytes) . await
3631 }
3732}
3833
39- pub struct ExternalFunctions {
34+ pub struct PluginStore {
35+ functions : Mutex < HashMap < String , PluginProxy > > ,
4036 handle : Arc < Handle > ,
41- functions : Mutex < HashMap < String , FunctionProxy > > ,
4237 libraries : Mutex < Vec < Arc < Library > > > ,
4338}
4439
45- impl Default for ExternalFunctions {
40+ impl Default for PluginStore {
4641 fn default ( ) -> Self {
4742 Self :: new ( )
4843 }
4944}
5045
51- impl ExternalFunctions {
52- pub fn new ( ) -> ExternalFunctions {
46+ impl PluginStore {
47+ pub fn new ( ) -> Self {
5348 let handle = Arc :: new ( Handle :: current ( ) ) ;
5449
55- ExternalFunctions {
56- handle,
50+ Self {
5751 functions : Mutex :: new ( HashMap :: default ( ) ) ,
52+ handle,
5853 libraries : Mutex :: new ( Vec :: new ( ) ) ,
5954 }
6055 }
@@ -65,20 +60,26 @@ impl ExternalFunctions {
6560 ///
6661 /// This function is unsafe because it loads a shared library and calls
6762 /// functions from it.
68- pub async unsafe fn load < P : AsRef < OsStr > > (
63+ pub async unsafe fn load (
6964 & self ,
7065 rt_handle : Arc < Handle > ,
7166 config_path : PathBuf ,
72- library_path : P ,
73- ) -> io:: Result < ( ) > {
74- let library = Arc :: new ( Library :: new ( library_path) . unwrap ( ) ) ;
67+ plugin_filename : & str ,
68+ ) -> IOResult < ( ) > {
69+ let plugin_path = plugins_path ( )
70+ . map_err ( |err| IOError :: other ( format ! ( "Failed to retrieve plugin path: {err}" ) ) ) ?
71+ . join ( plugin_filename) ;
72+ let library = Library :: new ( & plugin_path) . map_err ( |err| {
73+ IOError :: other ( format ! ( "Failed to load plugin from {plugin_path:?}: {err}" ) )
74+ } ) ?;
75+ let library = Arc :: new ( library) ;
7576 let decl = library
7677 . get :: < * mut PluginDeclaration > ( b"PLUGIN_DECLARATION\0 " )
7778 . unwrap ( )
7879 . read ( ) ;
7980
8081 if decl. rustc_version != RUSTC_VERSION || decl. core_version != CORE_VERSION {
81- return Err ( io :: Error :: new ( io :: ErrorKind :: Other , "Version mismatch " ) ) ;
82+ return Err ( IOError :: other ( "Version Mismatch. " ) ) ;
8283 }
8384
8485 let mut registrar = PluginRegistrar :: new ( Arc :: clone ( & library) ) ;
@@ -91,28 +92,30 @@ impl ExternalFunctions {
9192 Ok ( ( ) )
9293 }
9394
94- async fn get_function ( & self , func : & str ) -> Option < FunctionProxy > {
95+ async fn get ( & self , func : & str ) -> Option < PluginProxy > {
9596 self . functions . lock ( ) . await . get ( func) . cloned ( )
9697 }
9798
98- pub async fn call (
99+ pub async fn run (
99100 & self ,
100- func : & str ,
101+ plugin : & str ,
101102 parts : Parts ,
102103 bytes : Bytes ,
103- ) -> Result < Response < Full < Bytes > > , InvocationError > {
104- let function_proxy = self . get_function ( func ) . await . unwrap ( ) ;
104+ ) -> Result < Response < Full < Bytes > > , PluginError > {
105+ let function_proxy = self . get ( plugin ) . await . unwrap ( ) ;
105106 let join_handle = self
106107 . handle
107108 . spawn ( async move { function_proxy. call ( parts, bytes) . await } )
108109 . await ;
109110
110- join_handle. unwrap ( )
111+ join_handle. map_err ( |err| PluginError :: SpawnError {
112+ err : err. to_string ( ) ,
113+ } ) ?
111114 }
112115}
113116
114117struct PluginRegistrar {
115- functions : HashMap < String , FunctionProxy > ,
118+ functions : HashMap < String , PluginProxy > ,
116119 lib : Arc < Library > ,
117120}
118121
@@ -126,8 +129,8 @@ impl PluginRegistrar {
126129}
127130
128131impl http_server_plugin:: PluginRegistrar for PluginRegistrar {
129- fn register_function ( & mut self , name : & str , function : Arc < dyn Function > ) {
130- let proxy = FunctionProxy {
132+ fn register_function ( & mut self , name : & str , function : Arc < dyn Plugin > ) {
133+ let proxy = PluginProxy {
131134 function,
132135 _lib : Arc :: clone ( & self . lib ) ,
133136 } ;
0 commit comments