feat: add LazyLock to regexes

This commit is contained in:
Ladislav Hano 2025-01-27 18:00:15 +01:00
parent 62abce92f7
commit e0c48368d5
2 changed files with 14 additions and 14 deletions

View file

@ -1,4 +1,4 @@
use std::sync::Arc; use std::sync::{Arc, LazyLock};
use std::time::Duration; use std::time::Duration;
use anyhow::Context; use anyhow::Context;
@ -6,17 +6,19 @@ use serenity::builder::{CreateAttachment, CreateEmbed, CreateMessage};
use serenity::http::Http; use serenity::http::Http;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use tokio::time::sleep; use tokio::time::sleep;
use regex::Regex;
use serenity::model::id::ChannelId; use serenity::model::id::ChannelId;
use regex::Regex;
static RE_CHANNEL_ID: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"<#[0-9]*>$").unwrap());
/// Checks if the message should be mooved /// Checks if the message should be mooved
/// If the message should be mooved, try to move it and return Ok if mooved succesfully /// If the message should be mooved, try to move it and return Ok if mooved succesfully
pub async fn moove_check(msg: &Message) -> Option<u64> { pub async fn moove_check(msg: &Message) -> Option<u64> {
let word_count = msg.content.trim().split_whitespace().count(); let word_count = msg.content.trim().split_whitespace().count();
let re = Regex::new(r"<#[0-9]*>$").unwrap();
if word_count != 1 || re.captures(&msg.content).is_none() { if word_count != 1 || RE_CHANNEL_ID.captures(&msg.content).is_none() {
return None return None
} }

View file

@ -1,3 +1,5 @@
use std::sync::LazyLock;
use poise::CreateReply; use poise::CreateReply;
use radiobrowser::{ApiStation, StationSearchBuilder}; use radiobrowser::{ApiStation, StationSearchBuilder};
use regex::Regex; use regex::Regex;
@ -118,20 +120,16 @@ pub enum LinkString {
String String
} }
pub fn link_or_string(haystack: &str) -> LinkString { static RE_URL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^https?://([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$").unwrap());
let Ok(re) = Regex::new(r"^https?://([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$") else {
panic!("Wrong regex expression!");
};
return if re.is_match(haystack) { LinkString::Link } else { LinkString::String } pub fn link_or_string(haystack: &str) -> LinkString {
return if RE_URL.is_match(haystack) { LinkString::Link } else { LinkString::String }
} }
pub fn parse_radio_autocomplete(haystack: &str) -> Option<(String, String, String)> { static RE_AUTOCOMPLETE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^Name: (.*) Country: (.*) Language: (.*)").unwrap());
let Ok(re) = Regex::new(r"^Name: (.*) Country: (.*) Language: (.*)") else {
panic!("Wrong regex expression!");
};
let Some(captures) = re.captures(haystack) else { pub fn parse_radio_autocomplete(haystack: &str) -> Option<(String, String, String)> {
let Some(captures) = RE_AUTOCOMPLETE.captures(haystack) else {
return None return None
}; };