drasi_core/models/
source_change.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::interface::FutureElementRef;
16
17use super::{Element, ElementMetadata, ElementReference, ElementTimestamp};
18
19#[derive(Debug, Clone, Eq, PartialEq)]
20pub enum SourceChange {
21    Insert { element: Element },
22    Update { element: Element },
23    Delete { metadata: ElementMetadata },
24    Future { future_ref: FutureElementRef },
25}
26
27impl SourceChange {
28    pub fn get_reference(&self) -> &ElementReference {
29        match self {
30            SourceChange::Insert { element } => element.get_reference(),
31            SourceChange::Update { element } => element.get_reference(),
32            SourceChange::Delete { metadata } => &metadata.reference,
33            SourceChange::Future { future_ref } => &future_ref.element_ref,
34        }
35    }
36
37    pub fn get_transaction_time(&self) -> ElementTimestamp {
38        match self {
39            SourceChange::Insert { element } => element.get_effective_from(),
40            SourceChange::Update { element } => element.get_effective_from(),
41            SourceChange::Delete { metadata } => metadata.effective_from,
42            SourceChange::Future { future_ref } => future_ref.original_time,
43        }
44    }
45
46    pub fn get_realtime(&self) -> ElementTimestamp {
47        match self {
48            SourceChange::Insert { element } => element.get_effective_from(),
49            SourceChange::Update { element } => element.get_effective_from(),
50            SourceChange::Delete { metadata } => metadata.effective_from,
51            SourceChange::Future { future_ref } => future_ref.due_time,
52        }
53    }
54}