mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-14 05:42:32 +00:00
parse message types (wip)
This commit is contained in:
1 parent
b69f1c2a76
commit
512829cb09
4 files changed
+37
-66
No files matched your search
+4
-42
@@ -3,6 +3,7 @@ use lazy_static::lazy_static;
|
||||
use log::{info, warn};
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use crate::proto_utils::get_payload_type;
|
||||
|
||||
pub trait Handler {
|
||||
/*fn handle_message(&self,
|
||||
@@ -29,10 +30,9 @@ impl Dispatcher {
|
||||
pub fn handle_pubsub_event(&self, message: protos::PubsubEventPayloadV1) {
|
||||
info!("Handling Proto: {:?}", message);
|
||||
|
||||
let payload_type = message.ingestion_payload_type
|
||||
.and_then(|num| protos::pubsub_event_payload_v1::IngestionPayloadType::from_i32(num));
|
||||
let maybe_payload_type = get_payload_type(&message);
|
||||
|
||||
let payload_type = match payload_type {
|
||||
let payload_type = match maybe_payload_type {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
warn!("No payload type; skipping.");
|
||||
@@ -42,48 +42,10 @@ impl Dispatcher {
|
||||
|
||||
match payload_type {
|
||||
protos::pubsub_event_payload_v1::IngestionPayloadType::TwitchMessage => {
|
||||
|
||||
info!("TWITCH MESSAGE!!!");
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
/*let message = match twitch_message.message_contents {
|
||||
None => return,
|
||||
Some(ref m) => m.clone(),
|
||||
};
|
||||
|
||||
let captures = match COMMAND_REGEX.captures(&message) {
|
||||
None => return,
|
||||
Some(caps) => caps,
|
||||
};
|
||||
|
||||
let command = match captures.get(1) {
|
||||
None => return,
|
||||
Some(cmd) => cmd.as_str().trim().to_lowercase(),
|
||||
};
|
||||
|
||||
info!("Command: {}", command);
|
||||
|
||||
let unparsed_payload = match captures.get(2) {
|
||||
None => return,
|
||||
Some(p) => p.as_str().trim(),
|
||||
};
|
||||
|
||||
info!("Unparsed payload: {}", unparsed_payload);
|
||||
|
||||
if let Some(ref handler) = self.handlers.get(&command) {
|
||||
handler.handle_message(&command, unparsed_payload, twitch_message);
|
||||
}*/
|
||||
}
|
||||
|
||||
fn parse_message(&self, message: &str) -> String {
|
||||
lazy_static! {
|
||||
static ref COMMAND_REGEX : Regex = Regex::new(r"^\s*(\w+)\s+(.*)$").expect("Regex should work");
|
||||
}
|
||||
|
||||
"".to_string()
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ extern crate serde_derive;
|
||||
|
||||
mod dispatcher;
|
||||
mod handlers;
|
||||
mod message_to_command_parser;
|
||||
mod proto_utils;
|
||||
mod protos;
|
||||
mod redis_client;
|
||||
mod redis_subscriber;
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
use crate::AnyhowResult;
|
||||
use regex::Regex;
|
||||
|
||||
pub struct MessageToCommandParser {}
|
||||
|
||||
pub struct ParsedCommandFirstPass {
|
||||
pub command: String,
|
||||
pub raw_arguments: String,
|
||||
}
|
||||
|
||||
impl MessageToCommandParser {
|
||||
|
||||
pub fn parse_raw_command(message: &str) -> AnyhowResult<ParsedCommandFirstPass> {
|
||||
lazy_static! {
|
||||
static ref COMMAND_REGEX : Regex = Regex::new(r"^\s*(\w+)\s+(.*)$").expect("Regex should work");
|
||||
}
|
||||
|
||||
Ok(ParsedCommandFirstPass {
|
||||
command: "".to_string(),
|
||||
raw_arguments: "".to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
use anyhow::anyhow;
|
||||
use crate::AnyhowResult;
|
||||
use crate::protos::protos::IngestionTwitchMessage;
|
||||
use crate::protos::protos::pubsub_event_payload_v1::IngestionPayloadType;
|
||||
use crate::protos::protos;
|
||||
use prost::Message;
|
||||
|
||||
// TODO: add some type safety w/ generics and type bounds
|
||||
|
||||
pub fn get_payload_type(event_payload: &protos::PubsubEventPayloadV1)
|
||||
-> Option<protos::pubsub_event_payload_v1::IngestionPayloadType>
|
||||
{
|
||||
event_payload.ingestion_payload_type
|
||||
.and_then(|num| IngestionPayloadType::from_i32(num))
|
||||
}
|
||||
|
||||
pub fn get_twitch_message(event_payload: &protos::PubsubEventPayloadV1)
|
||||
-> AnyhowResult<IngestionTwitchMessage>
|
||||
{
|
||||
let payload_type = match get_payload_type(event_payload) {
|
||||
None => return Err(anyhow!("no payload type!")),
|
||||
Some(p) => p,
|
||||
};
|
||||
|
||||
match payload_type {
|
||||
IngestionPayloadType::TwitchMessage => {},
|
||||
_ => return Err(anyhow!("wrong payload type!")),
|
||||
}
|
||||
|
||||
IngestionTwitchMessage::decode(event_payload.ingestion_payload_data())
|
||||
.map_err(|_| anyhow!("error decoding proto"))
|
||||
}
|
||||
Reference in new issue
Block a user