prost_validate/errors/
duration.rs

1use thiserror::Error;
2
3#[derive(Debug, Clone, Error)]
4pub enum Error {
5    #[error("must be equal to {0:?}")]
6    Const(time::Duration),
7    #[error("must be less than {0:?}")]
8    Lt(time::Duration),
9    #[error("must be less than or equal to {0:?}")]
10    Lte(time::Duration),
11    #[error("must be greater than {0:?}")]
12    Gt(time::Duration),
13    #[error("must be greater than or equal to {0:?}")]
14    Gte(time::Duration),
15    #[error("must be in range {0}{1:?}, {2:?}{3}")]
16    InRange(String, time::Duration, time::Duration, String),
17    #[error("must not be in range {0}{1:?}, {2:?}{3}")]
18    NotInRange(String, time::Duration, time::Duration, String),
19    #[error("must be in {0:?}")]
20    In(Vec<time::Duration>),
21    #[error("must not be in {0:?}")]
22    NotIn(Vec<time::Duration>),
23}
24
25impl Error {
26    pub fn in_range(
27        start_inclusive: bool,
28        start: time::Duration,
29        end: time::Duration,
30        end_inclusive: bool,
31    ) -> Self {
32        Self::InRange(
33            if start_inclusive { "[" } else { "(" }.to_string(),
34            start,
35            end,
36            if end_inclusive { "]" } else { ")" }.to_string(),
37        )
38    }
39    pub fn not_in_range(
40        start_inclusive: bool,
41        start: time::Duration,
42        end: time::Duration,
43        end_inclusive: bool,
44    ) -> Self {
45        Self::NotInRange(
46            if start_inclusive { "[" } else { "(" }.to_string(),
47            start,
48            end,
49            if end_inclusive { "]" } else { ")" }.to_string(),
50        )
51    }
52}
53
54impl From<Error> for super::Error {
55    fn from(value: Error) -> Self {
56        Self::Duration(value)
57    }
58}