use std::sync::LazyLock;
use regex::Regex;
use thiserror::Error;
use crate::utils::StringValueData;
static WEBSITE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^https?://.+$").expect("Failed to compile website regex"));
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WebsiteType(String);
impl WebsiteType {
pub fn new(s: &str) -> Result<Self, InvalidLocationWebsite> {
Self::parse_from_str(s)
}
pub fn value(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("invalid website url: {0}")]
pub struct InvalidLocationWebsite(String);
impl StringValueData for WebsiteType {
type Error = InvalidLocationWebsite;
fn parse_from_str(s: &str) -> Result<Self, Self::Error> {
if !s.is_empty() && WEBSITE_RE.is_match(s) {
Ok(Self(s.into()))
} else {
Err(InvalidLocationWebsite(s.into()))
}
}
fn to_raw_value(&self) -> Box<str> {
self.0.clone().into_boxed_str()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_website_regex_compiles() {
LazyLock::force(&WEBSITE_RE);
}
}