prost_validate/errors/
timestamp.rs

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
use thiserror::Error;

#[derive(Debug, Clone, Error)]
pub enum Error {
    #[error("must be equal to {0:?}")]
    Const(time::OffsetDateTime),
    #[error("must be less than {0:?}")]
    Lt(time::OffsetDateTime),
    #[error("must be less than or equal to {0:?}")]
    Lte(time::OffsetDateTime),
    #[error("must be greater than {0:?}")]
    Gt(time::OffsetDateTime),
    #[error("must be greater than or equal to {0:?}")]
    Gte(time::OffsetDateTime),
    #[error("must be in range {0}{1:?}, {2:?}{3}")]
    InRange(String, time::OffsetDateTime, time::OffsetDateTime, String),
    #[error("must not be in range {0}{1:?}, {2:?}{3}")]
    NotInRange(String, time::OffsetDateTime, time::OffsetDateTime, String),
    #[error("must less than current time")]
    LtNow,
    #[error("must be less than now or within {0} from now")]
    LtNowWithin(time::Duration),
    #[error("must be greater than current time")]
    GtNow,
    #[error("must be greater than now or within {0} from now")]
    GtNowWithin(time::Duration),
    #[error("must be within {0:?} from current time")]
    Within(time::Duration),
}

impl Error {
    pub fn in_range(
        start_inclusive: bool,
        start: time::OffsetDateTime,
        end: time::OffsetDateTime,
        end_inclusive: bool,
    ) -> Self {
        Self::InRange(
            if start_inclusive { "[" } else { "(" }.to_string(),
            start,
            end,
            if end_inclusive { "]" } else { ")" }.to_string(),
        )
    }
    pub fn not_in_range(
        start_inclusive: bool,
        start: time::OffsetDateTime,
        end: time::OffsetDateTime,
        end_inclusive: bool,
    ) -> Self {
        Self::NotInRange(
            if start_inclusive { "[" } else { "(" }.to_string(),
            start,
            end,
            if end_inclusive { "]" } else { ")" }.to_string(),
        )
    }
}

impl From<Error> for super::Error {
    fn from(value: Error) -> Self {
        Self::Timestamp(value)
    }
}