1use std::time::{Duration, SystemTime};
2
3use ic_agent::agent::{QueryBuilder, UpdateBuilder};
4use time::OffsetDateTime;
5
6#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Default)]
9pub enum Expiry {
10 #[default]
13 Unspecified,
14
15 Delay(Duration),
17
18 DateTime(OffsetDateTime),
20}
21
22impl Expiry {
23 #[inline]
25 pub fn after(d: Duration) -> Self {
26 Self::Delay(d)
27 }
28
29 #[inline]
31 pub fn at(dt: impl Into<OffsetDateTime>) -> Self {
32 Self::DateTime(dt.into())
33 }
34
35 pub(crate) fn apply_to_update(self, u: UpdateBuilder<'_>) -> UpdateBuilder<'_> {
36 match self {
37 Expiry::Unspecified => u,
38 Expiry::Delay(d) => u.expire_after(d),
39 Expiry::DateTime(dt) => u.expire_at(dt),
40 }
41 }
42
43 pub(crate) fn apply_to_query(self, u: QueryBuilder<'_>) -> QueryBuilder<'_> {
44 match self {
45 Expiry::Unspecified => u,
46 Expiry::Delay(d) => u.expire_after(d),
47 Expiry::DateTime(dt) => u.expire_at(dt),
48 }
49 }
50}
51
52impl From<Duration> for Expiry {
53 fn from(d: Duration) -> Self {
54 Self::Delay(d)
55 }
56}
57
58impl From<SystemTime> for Expiry {
59 fn from(dt: SystemTime) -> Self {
60 Self::DateTime(dt.into())
61 }
62}
63
64impl From<OffsetDateTime> for Expiry {
65 fn from(dt: OffsetDateTime) -> Self {
66 Self::DateTime(dt)
67 }
68}