mirror of
https://github.com/kristoferssolo/tg-relay-rs.git
synced 2026-02-25 05:08:15 +00:00
refactor: validation
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
pub mod utils;
|
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
@@ -17,6 +15,10 @@ pub trait Validate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to create a lazy static Regex (reused across impls).
|
/// Helper function to create a lazy static Regex (reused across impls).
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// If no pattern found
|
||||||
pub fn lazy_regex(pattern: &str) -> &'static Regex {
|
pub fn lazy_regex(pattern: &str) -> &'static Regex {
|
||||||
static RE: OnceLock<Regex> = OnceLock::new();
|
static RE: OnceLock<Regex> = OnceLock::new();
|
||||||
RE.get_or_init(|| Regex::new(pattern).expect("failed to compile validation regex"))
|
RE.get_or_init(|| Regex::new(pattern).expect("failed to compile validation regex"))
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
use crate::error::{Error, Result};
|
|
||||||
|
|
||||||
/// Trims whitespace and rejects empty strings.
|
|
||||||
pub fn validate_non_empty(input: &str) -> Result<&str> {
|
|
||||||
let trimmed = input.trim();
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
return Err(Error::validation_falied("input cannot be empty"));
|
|
||||||
}
|
|
||||||
Ok(trimmed)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user