feat: add LazyLock to regexes
This commit is contained in:
parent
62abce92f7
commit
e0c48368d5
2 changed files with 14 additions and 14 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::sync::Arc;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
|
@ -6,17 +6,19 @@ use serenity::builder::{CreateAttachment, CreateEmbed, CreateMessage};
|
|||
use serenity::http::Http;
|
||||
use serenity::model::channel::Message;
|
||||
use tokio::time::sleep;
|
||||
use regex::Regex;
|
||||
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
|
||||
/// 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> {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
use poise::CreateReply;
|
||||
use radiobrowser::{ApiStation, StationSearchBuilder};
|
||||
use regex::Regex;
|
||||
|
@ -118,20 +120,16 @@ pub enum LinkString {
|
|||
String
|
||||
}
|
||||
|
||||
pub fn link_or_string(haystack: &str) -> LinkString {
|
||||
let Ok(re) = Regex::new(r"^https?://([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$") else {
|
||||
panic!("Wrong regex expression!");
|
||||
};
|
||||
static RE_URL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^https?://([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$").unwrap());
|
||||
|
||||
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)> {
|
||||
let Ok(re) = Regex::new(r"^Name: (.*) Country: (.*) Language: (.*)") else {
|
||||
panic!("Wrong regex expression!");
|
||||
};
|
||||
static RE_AUTOCOMPLETE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^Name: (.*) Country: (.*) Language: (.*)").unwrap());
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue