state_validation/dynamic/
state_filter.rs1use crate::StateFilter;
2
3pub struct DynStateFilter<State, Input, ValidOutput>(
4 for<'a> fn(&State, Input) -> Result<ValidOutput, Box<dyn std::error::Error>>,
5);
6
7impl<State, Input, ValidOutput> DynStateFilter<State, Input, ValidOutput> {
8 pub fn new<T: StateFilter<State, Input>>() -> Self
9 where
10 T::ValidOutput: Into<ValidOutput>,
11 T::Error: 'static,
12 {
13 DynStateFilter(|state, input| match T::filter(state, input) {
14 Ok(v) => Ok(v.into()),
15 Err(e) => Err(Box::new(e)),
16 })
17 }
18 pub fn filter(
19 &self,
20 state: &State,
21 input: Input,
22 ) -> Result<ValidOutput, Box<dyn std::error::Error>> {
23 (self.0)(state, input)
24 }
25}
26impl<State, Input> DynStateFilter<State, Input, Box<dyn std::any::Any>> {
27 pub fn new_with_any_output<T: StateFilter<State, Input>>() -> Self
28 where
29 T::ValidOutput: 'static,
30 T::Error: 'static,
31 {
32 DynStateFilter(|state, input| match T::filter(state, input) {
33 Ok(v) => Ok(Box::new(v)),
34 Err(e) => Err(Box::new(e)),
35 })
36 }
37}
38
39impl<State, Input, ValidOutput> Clone for DynStateFilter<State, Input, ValidOutput> {
40 fn clone(&self) -> Self {
41 DynStateFilter(self.0)
42 }
43}