commit a1d6dab7f30c9c742fa25999e8607ef007203eeb
parent 39b5eccf1d5d92639aef21a9c5c0e8bb09aa0629
Author: donicrosby <donicrosby1995@gmail.com>
Date: Tue, 9 Nov 2021 21:15:38 -0500
Use lazy_static macro to speed up command processing
Diffstat:
3 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/mrsbfh-macros/src/lib.rs b/mrsbfh-macros/src/lib.rs
@@ -254,13 +254,11 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {
let cloned_config = config.clone();
let cloned_client = client.clone();
tokio::spawn(async move {
- let whitespace_deduplicator_magic = regex::Regex::new(r"\s+").unwrap();
- let command_matcher_magic = regex::Regex::new(r"!([\w-]+)").unwrap();
- let normalized_body = whitespace_deduplicator_magic.replace_all(&msg_body, " ");
+ let normalized_body = mrsbfh::commands::command_utils::WHITESPACE_DEDUPLICATOR_MAGIC.replace_all(&msg_body, " ");
let mut split = msg_body.split_whitespace();
let command_raw = split.next().expect("This is not a command").to_lowercase();
- let command = command_matcher_magic.captures(command_raw.as_str())
+ let command = mrsbfh::commands::command_utils::COMMAND_MATCHER_MAGIC.captures(command_raw.as_str())
.map_or(String::new(), |caps| {
caps.get(1)
.map_or(String::new(),
diff --git a/mrsbfh/src/commands.rs b/mrsbfh/src/commands.rs
@@ -119,5 +119,16 @@
//! in the example.
//!
+pub mod command_utils {
+ use lazy_static::lazy_static;
+
+ lazy_static! {
+ pub static ref WHITESPACE_DEDUPLICATOR_MAGIC: regex::Regex =
+ regex::Regex::new(r"\s+").unwrap();
+ pub static ref COMMAND_MATCHER_MAGIC: regex::Regex =
+ regex::Regex::new(r"!([\w-]+)").unwrap();
+ }
+}
+
#[cfg(feature = "macros")]
pub use mrsbfh_macros::{command, command_generate, commands};
diff --git a/mrsbfh/src/lib.rs b/mrsbfh/src/lib.rs
@@ -75,7 +75,6 @@ impl MatrixMessageExt for Sender {
}
}
-pub use lazy_static::lazy_static;
pub use pulldown_cmark;
pub use serde_yaml;
pub use tokio;