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 /src/config.rs | |
parent | 51c9eabf4f58cddd6dee573ef3551f2a93584f33 (diff) |
better regex construction
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 26 |
1 files changed, 22 insertions, 4 deletions
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)) +} |