architect_api/utils/
half_open_range.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use chrono::{DateTime, Utc};
#[cfg(feature = "netidx")]
use derive::FromValue;
#[cfg(feature = "netidx")]
use netidx_derive::Pack;
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct HalfOpenRange<T: 'static> {
    pub from_inclusive: T,
    pub to_exclusive: T,
}

impl<T> HalfOpenRange<T> {
    pub fn new(from_inclusive: T, to_exclusive: T) -> Self {
        Self { from_inclusive, to_exclusive }
    }
}

impl<T: Copy> HalfOpenRange<T> {
    pub fn infinitesimal(at: T) -> Self {
        Self { from_inclusive: at, to_exclusive: at }
    }
}

impl<T: Ord> HalfOpenRange<T> {
    pub fn contains(&self, x: T) -> bool {
        self.from_inclusive <= x && x < self.to_exclusive
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub enum ClampSign {
    Forwards,  // fix [from_inclusive], clamp by reducing [to_exclusive]
    Backwards, // fix [to_exclusive], clamp by increasing [from_inclusive]
}

impl HalfOpenRange<DateTime<Utc>> {
    pub fn clamp(&self, max_width: chrono::Duration, sign: ClampSign) -> Self {
        match sign {
            ClampSign::Forwards => self.clamp_to(max_width),
            ClampSign::Backwards => self.clamp_from(max_width),
        }
    }

    fn clamp_from(&self, max_width: chrono::Duration) -> Self {
        Self {
            from_inclusive: std::cmp::max(
                self.to_exclusive - max_width,
                self.from_inclusive,
            ),
            ..*self
        }
    }

    fn clamp_to(&self, max_width: chrono::Duration) -> Self {
        Self {
            to_exclusive: std::cmp::min(
                self.from_inclusive + max_width,
                self.to_exclusive,
            ),
            ..*self
        }
    }
}

impl HalfOpenRange<Option<DateTime<Utc>>> {
    pub fn clamp(&self, max_width: chrono::Duration, sign: ClampSign) -> Self {
        match sign {
            ClampSign::Forwards => self.clamp_to(max_width),
            ClampSign::Backwards => self.clamp_from(max_width),
        }
    }

    fn clamp_from(&self, max_width: chrono::Duration) -> Self {
        match (self.from_inclusive, self.to_exclusive) {
            (Some(l), Some(r)) => {
                Self { from_inclusive: Some(std::cmp::max(r - max_width, l)), ..*self }
            }
            _ => *self,
        }
    }

    fn clamp_to(&self, max_width: chrono::Duration) -> Self {
        match (self.from_inclusive, self.to_exclusive) {
            (Some(l), Some(r)) => {
                Self { to_exclusive: Some(std::cmp::min(l + max_width, r)), ..*self }
            }
            _ => *self,
        }
    }
}