diff --git a/src/routes/bots/create.rs b/src/routes/bots/create.rs index e8231b7f..8bb04041 100644 --- a/src/routes/bots/create.rs +++ b/src/routes/bots/create.rs @@ -1,21 +1,15 @@ use crate::database::*; use crate::util::result::{Error, Result}; use crate::util::variables::MAX_BOT_COUNT; +use crate::util::regex::RE_USERNAME; use mongodb::bson::{doc, to_document}; -use regex::Regex; use rocket::serde::json::{Json, Value}; use serde::{Deserialize, Serialize}; use ulid::Ulid; use nanoid::nanoid; use validator::Validate; -// ! FIXME: should be global somewhere; maybe use config(?) -// ! tip: CTRL + F, RE_USERNAME -lazy_static! { - static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap(); -} - #[derive(Validate, Serialize, Deserialize)] pub struct Data { #[validate(length(min = 2, max = 32), regex = "RE_USERNAME")] diff --git a/src/routes/bots/edit.rs b/src/routes/bots/edit.rs index 546bd4d2..84fa853a 100644 --- a/src/routes/bots/edit.rs +++ b/src/routes/bots/edit.rs @@ -1,19 +1,13 @@ use crate::notifications::events::ClientboundNotification; use crate::util::result::{Error, Result, EmptyResponse}; use crate::{database::*, notifications::events::RemoveBotField}; +use crate::util::regex::RE_USERNAME; use mongodb::bson::doc; -use regex::Regex; use rocket::serde::json::Json; use serde::{Deserialize, Serialize}; use validator::Validate; -// ! FIXME: should be global somewhere; maybe use config(?) -// ! tip: CTRL + F, RE_USERNAME -lazy_static! { - static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap(); -} - #[derive(Validate, Serialize, Deserialize)] pub struct Data { #[validate(length(min = 2, max = 32), regex = "RE_USERNAME")] diff --git a/src/routes/onboard/complete.rs b/src/routes/onboard/complete.rs index 8a51cac4..b2249d3f 100644 --- a/src/routes/onboard/complete.rs +++ b/src/routes/onboard/complete.rs @@ -1,17 +1,13 @@ use crate::database::*; use crate::util::result::{Error, Result, EmptyResponse}; +use crate::util::regex::RE_USERNAME; use mongodb::bson::doc; use rauth::entities::Session; -use regex::Regex; use rocket::serde::json::Json; use serde::{Deserialize, Serialize}; use validator::Validate; -lazy_static! { - static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap(); -} - #[derive(Validate, Serialize, Deserialize)] pub struct Data { #[validate(length(min = 2, max = 32), regex = "RE_USERNAME")] diff --git a/src/routes/users/change_username.rs b/src/routes/users/change_username.rs index 2c4a8c55..71ce5701 100644 --- a/src/routes/users/change_username.rs +++ b/src/routes/users/change_username.rs @@ -1,20 +1,13 @@ use crate::database::*; use crate::notifications::events::ClientboundNotification; use crate::util::result::{Error, Result, EmptyResponse}; - +use crate::util::regex::RE_USERNAME; use mongodb::bson::doc; use rauth::entities::Account; -use regex::Regex; use rocket::serde::json::Json; use serde::{Deserialize, Serialize}; use validator::Validate; -// ! FIXME: should be global somewhere; maybe use config(?) -// ! tip: CTRL + F, RE_USERNAME -lazy_static! { - static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap(); -} - #[derive(Validate, Serialize, Deserialize)] pub struct Data { #[validate(length(min = 2, max = 32), regex = "RE_USERNAME")] diff --git a/src/util/mod.rs b/src/util/mod.rs index 08aa9ccf..79bec515 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,4 @@ pub mod result; pub mod variables; pub mod ratelimit; +pub mod regex; \ No newline at end of file diff --git a/src/util/regex.rs b/src/util/regex.rs new file mode 100644 index 00000000..e2310902 --- /dev/null +++ b/src/util/regex.rs @@ -0,0 +1,4 @@ +use once_cell::sync::Lazy; +use regex::Regex; + +pub static RE_USERNAME: Lazy = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_.]+$").unwrap());