mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 01:18:34 +00:00
add command parser and tests (wip)
This commit is contained in:
1 parent
aa7e3612f3
commit
bd0d29973c
4 files changed
+89
No files matched your search
@@ -81,6 +81,8 @@ impl Dispatcher {
|
||||
static ref COMMAND_REGEX : Regex = Regex::new(r"^\s*(\w+)\s+(.*)$").expect("Regex should work");
|
||||
}
|
||||
|
||||
"".to_string()
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod dispatcher;
|
||||
mod handlers;
|
||||
@@ -8,6 +10,7 @@ mod protos;
|
||||
mod redis_client;
|
||||
mod redis_subscriber;
|
||||
mod secrets;
|
||||
mod text_chat_parsers;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use anyhow::{Context, Error};
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
use anyhow::anyhow;
|
||||
use log::debug;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::AnyhowResult;
|
||||
use regex::Regex;
|
||||
|
||||
struct FirstPassParsedCommand {
|
||||
/// The first "word" / "verb" of a sentence.
|
||||
/// This is all-lowercase, no spaces, `[\w\-]+`
|
||||
pub command: String,
|
||||
|
||||
/// The remaining portion, stripped of leading/trailing spaces.
|
||||
pub unparsed_arguments: String,
|
||||
}
|
||||
|
||||
impl FirstPassParsedCommand {
|
||||
|
||||
pub fn try_parse(message: &str) -> AnyhowResult<Self> {
|
||||
lazy_static! {
|
||||
static ref COMMAND_REGEX : Regex = Regex::new(r"^\s*([\w\-]+)\s+(.*)$").expect("should parse");
|
||||
}
|
||||
|
||||
let captures = match COMMAND_REGEX.captures(&message) {
|
||||
None => return Err(anyhow!("message could not parse")),
|
||||
Some(caps) => caps,
|
||||
};
|
||||
|
||||
let command = match captures.get(1) {
|
||||
None => return Err(anyhow!("could not get first capture group")),
|
||||
Some(cmd) => cmd.as_str().trim().to_lowercase(),
|
||||
};
|
||||
|
||||
debug!("Command: {}", command);
|
||||
|
||||
let unparsed_payload = match captures.get(2) {
|
||||
None => return Err(anyhow!("could not get second capture group")),
|
||||
Some(p) => p.as_str().trim(),
|
||||
};
|
||||
|
||||
debug!("Unparsed payload: {}", unparsed_payload);
|
||||
|
||||
Ok(Self {
|
||||
command: "".to_string(),
|
||||
unparsed_arguments: "".to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn failure_cases() {
|
||||
assert!(FirstPassParsedCommand::try_parse("").is_err());
|
||||
assert!(FirstPassParsedCommand::try_parse(" ").is_err());
|
||||
assert!(FirstPassParsedCommand::try_parse("foo").is_err());
|
||||
assert!(FirstPassParsedCommand::try_parse("foo-bar").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_two_words() {
|
||||
let r = FirstPassParsedCommand::try_parse("foo bar");
|
||||
assert!(r.is_ok());
|
||||
let r = r.expect("Is okay");
|
||||
assert_eq!(&r.command, "foo");
|
||||
assert_eq!(&r.unparsed_arguments, "bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_two_words_spaces() {
|
||||
let r = FirstPassParsedCommand::try_parse(" foo \t bar \t \n ");
|
||||
assert!(r.is_ok());
|
||||
let r = r.expect("Is okay");
|
||||
assert_eq!(&r.command, "foo");
|
||||
assert_eq!(&r.unparsed_arguments, "bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_hyphenated_command() {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod first_pass_command_parser;
|
||||
Reference in new issue
Block a user