Skip to main content

options/validation/
validate.rs

1use super::Result;
2use cfg_if::cfg_if;
3
4/// Validates options.
5#[cfg_attr(feature = "async", maybe_impl::traits(Send, Sync))]
6pub trait Validate<T> {
7    /// Validates named options or all options if no name is specified.
8    ///
9    /// # Arguments
10    ///
11    /// * `name` - The optional name of the options to validate
12    /// * `options` - The options to validate
13    fn run(&self, name: &str, options: &T) -> Result;
14}
15
16macro_rules! validate {
17    ($($bounds:tt)+) => {
18        impl<F: Fn(&str, &T) -> Result + $($bounds)+, T> Validate<T> for F {
19            fn run(&self, name: &str, options: &T) -> Result {
20                (self)(name, options)
21            }
22        }
23    }
24}
25
26cfg_if! {
27    if #[cfg(feature = "async")] {
28        validate!(Sized + Send + Sync);
29    } else {
30        validate!(Sized);
31    }
32}