about summary refs log tree commit diff
path: root/src/formula/mod.rs
blob: d440bc8febfaf8e7c050591ca501d1cc623d6f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{marker::PhantomData, str::FromStr};

#[cfg(feature = "camino")]
mod camino;
#[cfg(feature = "camino")]
pub use camino::Utf8PathFormula;

mod boolean;
mod numbers;
mod path;

pub use boolean::{Boolean, BooleanFormula};
pub use numbers::*;
pub use path::PathFormula;

pub struct Formula<T: FromStr> {
	pub(crate) check_fn: Option<(String, Box<dyn FnMut(&T) -> bool>)>,
	pub(crate) failure: String,
	pub(crate) missing: Option<String>,
	phantom: PhantomData<T>,
}

impl<T: FromStr> Formula<T> {
	pub fn new<S: Into<String>>(failure: S) -> Self {
		Self {
			check_fn: None,
			failure: failure.into(),
			missing: None,
			phantom: PhantomData::default(),
		}
	}

	pub fn check<S: Into<String>, F>(mut self, fail: S, check: F) -> Self
	where
		F: FnMut(&T) -> bool + 'static,
	{
		self.check_fn = Some((fail.into(), Box::new(check)));
		self
	}

	pub fn required<S: Into<String>>(mut self, missing: S) -> Self {
		self.missing = Some(missing.into());
		self
	}
}

impl<T: FromStr> From<String> for Formula<T> {
	fn from(value: String) -> Self {
		Formula::new(value)
	}
}

impl<T: FromStr> From<&str> for Formula<T> {
	fn from(value: &str) -> Self {
		Formula::new(value)
	}
}