Skip to main content

validy/
settings.rs

1use std::sync::OnceLock;
2#[cfg(feature = "pattern")]
3use std::{borrow::Cow, sync::Arc};
4
5#[cfg(feature = "pattern")]
6use moka::sync::Cache;
7#[cfg(feature = "pattern")]
8use regex::Regex;
9
10pub struct ValidationSettings {
11	#[cfg(feature = "pattern")]
12	pub regex_cache: Cache<Cow<'static, str>, Arc<Regex>>,
13}
14
15impl Default for ValidationSettings {
16	fn default() -> Self {
17		Self {
18			#[cfg(feature = "pattern")]
19			regex_cache: Cache::<Cow<'static, str>, Arc<Regex>>::builder()
20				.max_capacity(100)
21				.initial_capacity(10)
22				.build(),
23		}
24	}
25}
26
27static SETTINGS: OnceLock<ValidationSettings> = OnceLock::new();
28
29impl ValidationSettings {
30	pub fn get() -> &'static ValidationSettings {
31		SETTINGS.get_or_init(ValidationSettings::default)
32	}
33
34	pub fn init(settings: ValidationSettings) -> Result<(), ValidationSettings> {
35		SETTINGS.set(settings)
36	}
37}