saa_common/types/
exp.rs

1use std::cmp::Ordering;
2use saa_schema::{saa_type, strum_macros::Display};
3use super::timestamp::Timestamp;
4use crate::AuthError;
5
6
7#[saa_type]
8#[derive(Display)]
9pub enum Expiration {
10    /// AtHeight will expire when `env.block.height` >= height
11    #[strum(to_string = "expiration height: {0}")]
12    AtHeight(u64),
13    /// AtTime will expire when `env.block.time` >= time
14    #[strum(to_string = "expiration time: {0}")]
15    AtTime(Timestamp),
16    /// Never will never expire. Used to express the empty variant
17    #[strum(to_string = "expiration: never")]
18    Never {},
19}
20
21
22/// The default (empty value) is to never expire
23impl Default for Expiration {
24    fn default() -> Self {
25        Expiration::Never {}
26    }
27}
28
29
30/// Duration is a delta of time. You can add it to a BlockInfo or Expiration to
31/// move that further in the future. Note that an height-based Duration and
32/// a time-based Expiration cannot be combined
33#[saa_type]
34#[derive(Display, Copy)]
35pub enum Duration {
36    /// Height in blocks
37    #[strum(to_string = "height: {0}")]
38    Height(u64),
39    /// Time in seconds
40    #[strum(to_string = "time: {0}")]
41    Time(u64),
42}
43
44
45
46#[cfg(feature = "wasm")]
47impl Expiration {
48    pub fn is_expired(&self, block: &crate::wasm::BlockInfo) -> bool {
49        match self {
50            Expiration::AtHeight(height) => block.height >= *height,
51            Expiration::AtTime(time) => block.time.seconds() >= time.seconds(),
52            Expiration::Never {} => false,
53        }
54    }
55}
56
57
58impl std::ops::Add<Duration> for Expiration {
59    type Output = Result<Expiration, AuthError>;
60
61    fn add(self, duration: Duration) -> Result<Expiration, AuthError> {
62        match (self, duration) {
63            (Expiration::AtTime(t), Duration::Time(delta)) => {
64                Ok(Expiration::AtTime(t.plus_seconds(delta)))
65            }
66            (Expiration::AtHeight(h), Duration::Height(delta)) => {
67                Ok(Expiration::AtHeight(h + delta))
68            }
69            (Expiration::Never {}, _) => Ok(Expiration::Never {}),
70            _ => Err(AuthError::generic("Cannot add height and time")),
71        }
72    }
73}
74
75
76impl PartialOrd for Expiration {
77    fn partial_cmp(&self, other: &Expiration) -> Option<Ordering> {
78        match (self, other) {
79            // compare if both height or both time
80            (Expiration::AtHeight(h1), Expiration::AtHeight(h2)) => Some(h1.cmp(h2)),
81            (Expiration::AtTime(t1), Expiration::AtTime(t2)) => Some(t1.cmp(t2)),
82            // if at least one is never, we can compare with anything
83            (Expiration::Never {}, Expiration::Never {}) => Some(Ordering::Equal),
84            (Expiration::Never {}, _) => Some(Ordering::Greater),
85            (_, Expiration::Never {}) => Some(Ordering::Less),
86            // if they are mis-matched finite ends, no compare possible
87            _ => None,
88        }
89    }
90}