From 67b1837ebd88cf3bff25aef2d536e7cb316eff98 Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Mon, 15 Jul 2024 10:28:55 -0600 Subject: better regex construction --- src/config.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/config.rs') 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, @@ -99,3 +99,21 @@ impl Config { return config } } + +fn parse_regex<'de, D: Deserializer<'de>> (deserializer: D) -> Result { + 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)) +} -- cgit 1.4.1