Hi all.
I want to develop a plugin system within my program, and I have a trait that functions defined by plugins should implement.
Currently, my code gets all the functions in a HashMap and then calls them by their name. Problem is, I have to create that hashmap myself by inserting every function myself.
I would really appreciate it if there was a way to say, suppose, all pub members of mod functions::
that implement this trait PluginFunction
call register(hashmap)
function. So as I add more functions as mod
in functions
it’ll be automatically added on compile.
Pseudocode:
Files:
src/
├── attrs.rs
├── functions
│ ├── attrs.rs
│ ├── export.rs
│ └── render.rs
├── functions.rs
├── lib.rs
Basically, in mod functions
I want:
impl AllFunctions{
pub fn new() -> Self {
let mut functions_map = HashMap::new();[[
register_all!(crate::functions::* implementing PluginFunction, &mut functions_map);
Self { function_map }
}
}
Right now I’m doing:
impl AllFunctions{
pub fn new() -> Self {
let mut functions_map = HashMap::new();[[
crate::functions::attrs::PrintAttr{}.register(&mut functions_map);
crate::functions::export::ExportCSV{}.register(&mut functions_map);
crate::functions::render::RenderText{}.register(&mut functions_map);
// More as I add more functions
Self { function_map }
}
}
That seems like a good compromise if I don’t find something better. Thank you.
I’m hoping to make it easy for people to add more functions, that’s why I want minimal code change required to add more functions.