Skip to content

Commit

Permalink
fix: do not create unnecessary instances of Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 8, 2022
1 parent 179ad3e commit a3e6d88
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde_json = "1.0"
tokio = { version = "1.0", features = ["rt-multi-thread"] }
log = "0.4"
regex = "1.0"
serde_regex = "1.1"
chrono = "0.4"

[dependencies.serenity]
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::Arc;

use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use configuration::application::Configuration;
use log::{error, info, trace, LevelFilter};
use logger::logging::SimpleLogger;
use model::application::Configuration;
use regex::Regex;
use serenity::client::{Context, EventHandler};
use serenity::model::channel::{GuildChannel, Message};
Expand All @@ -12,8 +12,8 @@ use serenity::model::prelude::command::Command;
use serenity::model::prelude::interaction::{Interaction, InteractionResponseType, MessageFlags};
use serenity::prelude::{GatewayIntents, RwLock, TypeMapKey};
use serenity::{async_trait, Client};
mod configuration;
mod logger;
mod model;

static LOGGER: SimpleLogger = SimpleLogger;

Expand All @@ -34,8 +34,8 @@ async fn get_configuration_lock(ctx: &Context) -> Arc<RwLock<Configuration>> {
.clone()
}

fn contains_match(strings: &Vec<String>, text: &String) -> bool {
strings.iter().any(|regex| Regex::new(regex).unwrap().is_match(&text))
fn contains_match(regex: &Vec<Regex>, text: &String) -> bool {
regex.iter().any(|r| r.is_match(&text))
}

fn load_configuration() -> Configuration {
Expand Down
7 changes: 5 additions & 2 deletions src/configuration/application.rs → src/model/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::{Error, Read, Write};

use regex::Regex;
use serde::{Deserialize, Serialize};

#[derive(Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -107,14 +108,16 @@ pub struct Author {
pub struct Includes {
pub channels: Vec<u64>,
#[serde(rename = "match")]
pub match_field: Vec<String>,
#[serde(with = "serde_regex")]
pub match_field: Vec<Regex>,
}

#[derive(Serialize, Deserialize)]
pub struct Excludes {
pub roles: Vec<u64>,
#[serde(rename = "match")]
pub match_field: Vec<String>,
#[serde(with = "serde_regex")]
pub match_field: Vec<Regex>,
}

#[derive(Serialize, Deserialize)]
Expand Down
File renamed without changes.

0 comments on commit a3e6d88

Please sign in to comment.