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
use super::Validator;
use thiserror::Error;
#[derive(Debug, Clone, Copy, Error)]
#[error("Value is not in list of expected values.")]
pub struct UnexpectedValueError;
#[derive(Clone, Copy, Debug)]
pub struct ExpectedValidator<'a, T> {
expected: &'a [T]
}
impl<'a, T> ExpectedValidator<'a, T> {
pub fn new(expected: &'a [T]) -> Self {
Self { expected }
}
}
impl<'a, D, T> Validator<D> for ExpectedValidator<'a, T>
where
D: PartialEq<T>
{
type Err = UnexpectedValueError;
fn validate(self, data: &D) -> Result<(), Self::Err> {
if !self.expected.iter().any(|expected| data == expected) {
return Err(UnexpectedValueError);
}
Ok(())
}
}