From 28e8f4e67c5a79f1a2e0e082696705476dfe5e6f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 29 Apr 2021 00:49:16 -0400 Subject: [PATCH] fixed dispatching, prototype handler --- src/dispatcher.rs | 16 +++++++++++----- src/handlers/coordinate_and_geocode_handler.rs | 17 +++++++++++++++++ src/handlers/mod.rs | 1 + src/main.rs | 10 +++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/handlers/coordinate_and_geocode_handler.rs create mode 100644 src/handlers/mod.rs diff --git a/src/dispatcher.rs b/src/dispatcher.rs index fd30cd44ee..e0ad6b0eda 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -42,13 +42,19 @@ impl Dispatcher { Some(caps) => caps, }; - info!("Captures: {:?}", captures); + let command = match captures.get(1) { + None => return, + Some(cmd) => cmd.as_str().trim().to_lowercase(), + }; - if let Some(ref handler) = self.handlers.get(&captures[0]) { - info!("Captures: {:?}", captures); - handler.handle_message(&captures[0], &captures[1], twitch_message); + let unparsed_payload = match captures.get(2) { + None => return, + Some(p) => p.as_str().trim(), + }; + + if let Some(ref handler) = self.handlers.get(&command) { + handler.handle_message(&command, unparsed_payload, twitch_message); } - } } diff --git a/src/handlers/coordinate_and_geocode_handler.rs b/src/handlers/coordinate_and_geocode_handler.rs new file mode 100644 index 0000000000..63532d3c21 --- /dev/null +++ b/src/handlers/coordinate_and_geocode_handler.rs @@ -0,0 +1,17 @@ +use crate::dispatcher::Handler; +use crate::protos::protos; + +pub struct CoordinateAndGeocodeHandler { +} + +impl CoordinateAndGeocodeHandler { + pub fn new() -> Self { + Self {} + } +} + +impl Handler for CoordinateAndGeocodeHandler { + fn handle_message(&self, command: &str, unparsed_command_args: &str, twitch_message: protos::TwitchMessage) { + todo!() + } +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs new file mode 100644 index 0000000000..30b015c978 --- /dev/null +++ b/src/handlers/mod.rs @@ -0,0 +1 @@ +pub mod coordinate_and_geocode_handler; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 845ff04f7f..d392995da4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,12 @@ extern crate serde_derive; mod dispatcher; +mod handlers; mod protos; mod redis_client; mod redis_subscriber; mod secrets; + use anyhow::anyhow; use anyhow::{Context, Error}; use log::{warn, info}; @@ -18,6 +20,7 @@ use crate::secrets::Secrets; use crate::redis_client::RedisClient; use crate::redis_subscriber::RedisSubscribeClient; use crate::dispatcher::Dispatcher; +use crate::handlers::coordinate_and_geocode_handler::CoordinateAndGeocodeHandler; pub type AnyhowResult = anyhow::Result; @@ -33,7 +36,12 @@ async fn main() -> anyhow::Result<()> { let secrets = Secrets::from_file("secrets.toml")?; - let dispatcher = Dispatcher::new(); + let mut dispatcher = Dispatcher::new(); + + let coord_geo_handler = CoordinateAndGeocodeHandler::new(); + + dispatcher.add_handler("goto", Box::new(coord_geo_handler)); + let mut redis_client = RedisSubscribeClient::new( &secrets.redis,