I apologize if I'm not asking this question properly. I feel like part of the issue is that I'm having hard time trying to idiomatically describe what I'm trying to do.
I am currently trying to figure out if I can register handler functions defined in lib.rs
into a service, based only on reading a YAML manifest file.
I have a manifest file config.yaml
event: count
handler: count_handler
I have a lib.rs
with the following definitions:
// This is somewhat pseudo-code (has not been tested, but gives you the idea)
fn count_handler(d: Vec<u8>) -> Vec<u8> {
println!("count_handler was called!");
Vec::new()
}
type HandlerFn = fn(Vec<u8>) -> Vec<u8>;
struct Service {
handlers: HashMap<String, HandlerFn>,
}
impl Service {
fn new() -> Self {
let handlers: HashMap::new();
Self { handlers }
}
fn register_handler(&mut self, name: String, handler: HandlerFn) {
self.handlers.insert(name, handler);
}
fn start() {
println!("I'm doing some things with these handlers: {:?}", self.handlers);
}
}
What I'm trying to figure out is:
Is there any way to automagically register my thecount_handler
in theService
using only the manifest file?- Update: There seems to be no way to do this using only the manifest file, so let me clarify further
- Is there any way to do this using some type of macro? (proc macro?)
- I basically need whatever mapping is generated to happen without the user manually registering the handlers.
Ideally, I would be able to do the following:
fn main() {
let manifest: Manifest = Manifest::from_file("config.yaml");
// assert_eq!(manifest.event, "count".to_owned());
// assert_eq!(manifest.handler, "count_handler".to_owned());
let service = Service::new();
service.start();
// "I'm doing some things with these handlers: HashMap{ "count": Function(count_handler) }"
}
Some constrants and clarifications:
- Assume the
handler
defined in the manifest, is always present inlib.rs
- The
count_handler
could be any function that satisfiesHandlerFn
- The
handler
in the manifest, will always be indexed in theService
by theevent
- In this case,
<"count", count_handler()>
is registered in theService
- In this case,
I don't think this is possible in Rust, but just wanted to get some clarity.