diff options
Diffstat (limited to 'src/update.rs')
-rw-r--r-- | src/update.rs | 41 |
1 files changed, 4 insertions, 37 deletions
diff --git a/src/update.rs b/src/update.rs index b5b0ba7..7f5e28d 100644 --- a/src/update.rs +++ b/src/update.rs @@ -1,49 +1,16 @@ -use std::collections::HashMap; -use serde_json::Value as JsonValue; use std::future::Future; use futures::future::BoxFuture; -use log::{ warn, trace }; use crate::client::Client; +use pert_types::types::Update; pub trait Handler: Send + Sync + 'static { - fn handle(&self, _: Client, _: JsonValue) -> BoxFuture<'static, ()>; + fn handle(&self, _: Client, _: Update) -> BoxFuture<'static, ()>; } impl<C, F> Handler for C -where C: Send + Sync + 'static + Fn(Client, JsonValue) -> F, +where C: Send + Sync + 'static + Fn(Client, Update) -> F, F: Future<Output = ()> + 'static + Send { - fn handle(&self, client: Client, req: JsonValue) -> BoxFuture<'static, ()> { + fn handle(&self, client: Client, req: Update) -> BoxFuture<'static, ()> { Box::pin((*self)(client, req)) } } - -pub struct UpdateRouter { - router: HashMap<String, Box<dyn Handler>>, - rt: tokio::runtime::Handle, -} - -impl UpdateRouter { - pub fn new(rt: tokio::runtime::Handle) -> Self { - Self { - router: HashMap::new(), - rt: rt, - } - } - - pub fn add_handler<H: Handler>(&mut self, update_type: &str, handler: H) { - self.router.insert(update_type.to_owned(), Box::new(handler)); - } - - pub fn dispatch(&self, client: &Client, update: JsonValue) { - let update_type: &str = update["@type"].as_str().unwrap(); - match self.router.get(update_type) { - Some(handler) => { - self.rt.spawn(handler.handle(client.clone(), update)); - }, - None => { - warn!("no handler for {}", update_type); - trace!("request was: {}", update); - }, - } - } -} |