diff options
author | Ashelyn Rose <git@ashen.earth> | 2024-07-15 10:28:55 -0600 |
---|---|---|
committer | Ashelyn Rose <git@ashen.earth> | 2024-07-15 10:28:55 -0600 |
commit | 67b1837ebd88cf3bff25aef2d536e7cb316eff98 (patch) | |
tree | 0ef228256fa444db221cc964098d33ba5533bf7f | |
parent | 51c9eabf4f58cddd6dee573ef3551f2a93584f33 (diff) |
better regex construction
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/config.rs | 26 | ||||
-rw-r--r-- | src/system.rs | 8 |
4 files changed, 22 insertions, 24 deletions
diff --git a/Cargo.lock b/Cargo.lock index c094cab..f373c0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1190,7 +1190,6 @@ dependencies = [ "regex", "reqwest", "serde", - "serde_regex", "tokio", "toml", "twilight-gateway", @@ -1264,16 +1263,6 @@ dependencies = [ ] [[package]] -name = "serde_regex" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8136f1a4ea815d7eac4101cfd0b16dc0cb5e1fe1b8609dfd728058656b7badf" -dependencies = [ - "regex", - "serde", -] - -[[package]] name = "serde_repr" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index 219aa7f..1fcdeeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ futures = "0.3.30" regex = "1.10.2" reqwest = "0.12" serde = { version = "1.0.196", features = [ "derive" ] } -serde_regex = "1.1.0" tokio = { version = "1.38.0", features = [ "rt", "macros", "time" ] } toml = "0.8.8" twilight-gateway = "0.15.4" diff --git a/src/config.rs b/src/config.rs index 794e477..27911ea 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; -use regex::Regex; -use serde::Deserialize; +use regex::{Regex, RegexBuilder}; +use serde::{Deserialize, Deserializer, de::Error}; #[derive(Deserialize)] pub enum AutoProxyScope { @@ -40,7 +40,7 @@ pub enum AutoproxyLatchScope { #[derive(Deserialize, Clone)] pub struct PluralkitConfig { - #[serde(with = "serde_regex")] + #[serde(deserialize_with = "parse_regex")] pub message_pattern: Regex, pub api_token: String, } @@ -64,7 +64,7 @@ pub type MemberName = String; #[derive(Deserialize, Clone)] pub struct Member { pub name: MemberName, - #[serde(with = "serde_regex")] + #[serde(deserialize_with = "parse_regex")] pub message_pattern: Regex, pub discord_token: String, pub presence: Option<PresenceMode>, @@ -99,3 +99,21 @@ impl Config { return config } } + +fn parse_regex<'de, D: Deserializer<'de>> (deserializer: D) -> Result<Regex, D::Error> { + let mut pattern = String::deserialize(deserializer)?; + + if !pattern.starts_with("^") { + pattern.insert(0, '^'); + } + + if !pattern.ends_with("$") { + pattern.push('$'); + } + + RegexBuilder::new(&pattern) + .multi_line(true) + .case_insensitive(true) + .build() + .map_err(|e| D::Error::custom(e)) +} diff --git a/src/system.rs b/src/system.rs index 6aebf39..fa44946 100644 --- a/src/system.rs +++ b/src/system.rs @@ -74,8 +74,6 @@ impl System { println!("Autoproxy timeout has expired: {} (last sent), {} (timeout scheduled)", current_last_message.as_secs(), last_message.as_secs()); self.latch_state = None; self.update_status_of_system(); - } else { - println!("Autoproxy timeout called but not expired: {} (last sent), {} (timeout scheduled)", current_last_message.as_secs(), last_message.as_secs()); } } }, @@ -278,12 +276,6 @@ impl crate::config::Member { match self.message_pattern.captures(message.content.as_str()) { None => None, Some(captures) => { - let full_match = captures.get(0).unwrap(); - - if full_match.len() != message.content.len() { - return None - } - match captures.name("content") { None => None, Some(matched_content) => Some(matched_content.as_str()), |