drasi_core/models/
timestamp_range.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::models::ElementTimestamp;
16
17#[derive(Debug, Clone, Hash, Eq, PartialEq)]
18pub struct TimestampRange<T> {
19    pub from: TimestampBound<T>,
20    pub to: ElementTimestamp,
21}
22
23#[derive(Debug, Clone, Hash, Eq, PartialEq)]
24pub enum TimestampBound<T> {
25    StartFromPrevious(T),
26    Included(T),
27}
28
29impl<T> TimestampBound<T> {
30    // &TimestampBound<T> -> TimestampBound<&T>
31    pub fn as_ref(&self) -> TimestampBound<&T> {
32        match *self {
33            TimestampBound::StartFromPrevious(ref t) => TimestampBound::StartFromPrevious(t),
34            TimestampBound::Included(ref t) => TimestampBound::Included(t),
35        }
36    }
37
38    // &mut TimestampBound<T> -> TimestampBound<&mut T>
39    pub fn as_mut(&mut self) -> TimestampBound<&mut T> {
40        match *self {
41            TimestampBound::StartFromPrevious(ref mut t) => TimestampBound::StartFromPrevious(t),
42            TimestampBound::Included(ref mut t) => TimestampBound::Included(t),
43        }
44    }
45
46    pub fn get_timestamp(&self) -> &T {
47        match self {
48            TimestampBound::StartFromPrevious(t) => t,
49            TimestampBound::Included(t) => t,
50        }
51    }
52}
53
54impl<T: Clone> TimestampBound<&T> {
55    pub fn cloned(self) -> TimestampBound<T> {
56        match self {
57            TimestampBound::StartFromPrevious(t) => TimestampBound::StartFromPrevious(t.clone()),
58            TimestampBound::Included(t) => TimestampBound::Included(t.clone()),
59        }
60    }
61}