diff --git a/src/commands/utils.rs b/src/commands/utils.rs index 1e26275..6907530 100644 --- a/src/commands/utils.rs +++ b/src/commands/utils.rs @@ -1,3 +1,5 @@ +use std::sync::OnceLock; + use crate::{ commands::{respond_embed, respond_err, respond_ok}, structures::RepositoryDetails, @@ -269,8 +271,8 @@ pub async fn add_repo( #[description = "The owner of the repository."] owner: String, #[description = "The respository name."] repository: String, ) -> Result<(), Error> { - let key_regex = Regex::new(r"[a-z+]+$").unwrap(); - let repo_details_regex = Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap(); + let key_regex = get_key_regex(); + let repo_details_regex = get_repo_details_regex(); if !key_regex.is_match(&key) { respond_err( &ctx, @@ -403,3 +405,13 @@ fn rm_repo(ctx: &Context<'_>, key: &str) { rwlock_guard.issue_prefixes.remove(key); } + +fn get_key_regex() -> &'static Regex { + static REGEX: OnceLock = OnceLock::new(); + REGEX.get_or_init(|| Regex::new(r"[a-z+]+$").unwrap()) +} + +fn get_repo_details_regex() -> &'static Regex { + static REGEX: OnceLock = OnceLock::new(); + REGEX.get_or_init(|| Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap()) +} diff --git a/src/events/code.rs b/src/events/code.rs index baf4463..56fc8da 100644 --- a/src/events/code.rs +++ b/src/events/code.rs @@ -1,6 +1,6 @@ use regex::{Match, Regex}; -use std::path::Path; use std::str::FromStr; +use std::{path::Path, sync::OnceLock}; use poise::serenity_prelude::{self as serenity, Colour, Context, CreateEmbed, Message}; @@ -80,9 +80,7 @@ struct FileReference<'a> { impl FileReference<'_> { pub fn try_from_str(text: &str) -> Option> { - let r = - Regex::new(r"https://github.com/(.+?)/(.+?)/blob/(.+?)/(.+?)#L([0-9]+)(?:-L([0-9]+))?") - .expect("Expected url regex"); + let r = get_file_reference_regex(); let files: Vec = r .captures_iter(text) @@ -156,3 +154,11 @@ impl FileReference<'_> { } } } + +fn get_file_reference_regex() -> &'static Regex { + static REGEX: OnceLock = OnceLock::new(); + REGEX.get_or_init(|| { + Regex::new(r"https://github.com/(.+?)/(.+?)/blob/(.+?)/(.+?)#L([0-9]+)(?:-L([0-9]+))?") + .unwrap() + }) +} diff --git a/src/events/issues/mod.rs b/src/events/issues/mod.rs index 7be18ab..7c072c5 100644 --- a/src/events/issues/mod.rs +++ b/src/events/issues/mod.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use std::{sync::OnceLock, time::Duration}; use crate::{commands::interaction_err, structures::Embeddable, Data}; @@ -152,8 +152,7 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option let client = octocrab::instance(); let ratelimit = client.ratelimit(); - // TODO: stop compiling this every time. - let regex = Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").expect("Expected numbers regex"); + let regex = get_issue_regex(); let custom_repos = { data.state.read().unwrap().issue_prefixes.clone() }; @@ -197,3 +196,8 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option Some(embeds) } } + +fn get_issue_regex() -> &'static Regex { + static REGEX: OnceLock = OnceLock::new(); + REGEX.get_or_init(|| Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").unwrap()) +}