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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Helpers for configuration validation.
//!
//! See [`config_validator`][crate::Extensible::config_validator].
/// A validation action.
///
/// The validator (see [`config_validator`][crate::Extensible::config_validator]) is
/// supposed to either return an error or an action to be taken once validation completes.
///
/// By default, the [`Action`] is empty, but an [`on_success`][Action::on_success] and
/// [`on_abort`][Action::on_abort] callbacks can be attached to it. These'll execute once the
/// validation completes (only one of them will be called, depending on the result of validation).
///
/// # Examples
///
/// ```rust
/// use spirit::{Empty, Spirit};
/// use spirit::prelude::*;
/// use spirit::validation::Action;
/// # fn create_something<T>(_cfg: T) -> Result<Empty, spirit::AnyError> { Ok(Empty {}) }
/// # fn install_something(_empty: Empty) {}
/// # let _ =
/// Spirit::<Empty, Empty>::new()
/// .config_validator(|_old_cfg, new_cfg, _opts| {
/// let something = create_something(new_cfg)?;
/// Ok(Action::new().on_success(move || install_something(something)))
/// });
/// ```
///
/// Or, if you want to only check the configuration:
///
/// ```rust
/// use spirit::{Empty, Spirit};
/// use spirit::prelude::*;
/// use spirit::validation::Action;
///
/// # fn looks_good<T>(_cfg: T) -> Result<(), spirit::AnyError> { Ok(()) }
/// # let _ =
/// Spirit::<Empty, Empty>::new()
/// .config_validator(|_old_cfg, new_cfg, _opts| {
/// looks_good(new_cfg)?;
/// Ok(Action::new())
/// });
/// ```