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
use ic_agent::agent::{QueryBuilder, UpdateBuilder};

/// An expiry value. Either not specified (the default), a delay relative to the time the
/// call is made, or a specific date time.
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum Expiry {
    /// Unspecified. Will not try to override the Agent's value, which might itself have
    /// its own default value.
    Unspecified,

    /// A duration that will be added to the system time when the call is made.
    Delay(std::time::Duration),

    /// A specific date and time to use for the expiry of the request.
    DateTime(std::time::SystemTime),
}

impl Expiry {
    /// Create an expiry that happens after a duration.
    #[inline]
    pub fn after(d: std::time::Duration) -> Self {
        Self::Delay(d)
    }

    /// Set the expiry field to a specific date and time.
    #[inline]
    pub fn at(dt: std::time::SystemTime) -> Self {
        Self::DateTime(dt)
    }

    pub(crate) fn apply_to_update(self, u: &mut UpdateBuilder<'_>) {
        match self {
            Expiry::Unspecified => {}
            Expiry::Delay(d) => {
                u.expire_after(d);
            }
            Expiry::DateTime(dt) => {
                u.expire_at(dt);
            }
        }
    }

    pub(crate) fn apply_to_query(self, u: &mut QueryBuilder<'_>) {
        match self {
            Expiry::Unspecified => {}
            Expiry::Delay(d) => {
                u.expire_after(d);
            }
            Expiry::DateTime(dt) => {
                u.expire_at(dt);
            }
        }
    }
}

impl From<std::time::Duration> for Expiry {
    fn from(d: std::time::Duration) -> Self {
        Self::Delay(d)
    }
}

impl From<std::time::SystemTime> for Expiry {
    fn from(dt: std::time::SystemTime) -> Self {
        Self::DateTime(dt)
    }
}

impl Default for Expiry {
    fn default() -> Self {
        Expiry::Unspecified
    }
}