options/
configure.rs

1use std::marker::PhantomData;
2
3/// Defines the behavior of something that configures [`Options`](crate::Options).
4///
5/// # Remarks
6///
7/// These are all run first
8pub trait ConfigureOptions<T> {
9    /// Configures the corresponding options.
10    ///
11    /// # Arguments
12    ///
13    /// * `name` - The optional name of the options to configure
14    /// * `options` - The options to configure
15    fn configure(&self, name: Option<&str>, options: &mut T);
16}
17
18/// Defines the behavior of something that configures [`Options`](crate::Options).
19///
20/// # Remarks
21///
22/// These are all run last
23pub trait PostConfigureOptions<T> {
24    /// Configures the corresponding options.
25    ///
26    /// # Arguments
27    ///
28    /// * `name` - The optional name of the options to configure
29    /// * `options` - The options to configure
30    fn post_configure(&self, name: Option<&str>, options: &mut T);
31}
32
33/// Creates and returns [options configuration](ConfigureOptions) for the specified action.
34///
35/// # Arguments
36///
37/// * `action` - The configuration action
38pub fn configure<T, F>(action: F) -> impl ConfigureOptions<T>
39where
40    F: Fn(Option<&str>, &mut T),
41{
42    _ConfigureOptions::new(action)
43}
44
45/// Creates and returns [options post-configuration](PostConfigureOptions) for the specified action.
46///
47/// # Arguments
48///
49/// * `action` - The post configuration action
50pub fn post_configure<T, F>(action: F) -> impl PostConfigureOptions<T>
51where
52    F: Fn(Option<&str>, &mut T),
53{
54    _ConfigureOptions::new(action)
55}
56
57struct _ConfigureOptions<TOptions, TAction>
58where
59    TAction: Fn(Option<&str>, &mut TOptions),
60{
61    action: TAction,
62    _marker: PhantomData<TOptions>,
63}
64
65impl<TOptions, TAction> _ConfigureOptions<TOptions, TAction>
66where
67    TAction: Fn(Option<&str>, &mut TOptions),
68{
69    fn new(action: TAction) -> Self {
70        Self {
71            action,
72            _marker: PhantomData,
73        }
74    }
75}
76
77impl<TOptions, TAction> ConfigureOptions<TOptions> for _ConfigureOptions<TOptions, TAction>
78where
79    TAction: Fn(Option<&str>, &mut TOptions),
80{
81    fn configure(&self, name: Option<&str>, options: &mut TOptions) {
82        (self.action)(name, options)
83    }
84}
85
86impl<TOptions, TAction> PostConfigureOptions<TOptions> for _ConfigureOptions<TOptions, TAction>
87where
88    TAction: Fn(Option<&str>, &mut TOptions),
89{
90    fn post_configure(&self, name: Option<&str>, options: &mut TOptions) {
91        (self.action)(name, options)
92    }
93}