Skip to main content

struct_input/util/
formats.rs

1use std::sync::OnceLock;
2use regex::Regex;
3
4#[derive(Debug, Clone)]
5pub enum Format {
6    BaseUrl,
7    Name,
8    NotAllowWhitespace,
9    None,
10}
11
12impl Format {
13    pub fn valid(&self, text: &str) -> bool {
14        match self {
15            Format::BaseUrl => {
16                static RE: OnceLock<Regex> = OnceLock::new();
17                RE.get_or_init(||{Regex::new(r"^[a-zA-Z]+://[a-zA-Z0-9.]+(:[0-9]+)?$").unwrap()})
18                    .is_match(text)
19            },
20            Format::Name => {
21                static RE: OnceLock<Regex> = OnceLock::new();
22                RE.get_or_init(||{Regex::new(r"^[a-zA-Z0-9-]+$").unwrap()})
23                    .is_match(text)
24            },
25            Format::NotAllowWhitespace => {
26                !text.chars().any(|c| c.is_whitespace())
27            },
28            Format::None => true
29        }
30    }
31}