Skip to main content

ratio_metadata/
transaction.rs

1//! # Transaction type module
2//!
3//! ## License
4//!
5//! This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
6//! If a copy of the MPL was not distributed with this file,
7//! You can obtain one at <https://mozilla.org/MPL/2.0/>.
8//!
9//! **Code examples both in the docstrings and rendered documentation are free to use.**
10
11use std::collections::{BTreeMap, BTreeSet};
12use std::fmt::Debug;
13
14use uuid::Uuid;
15
16use crate::metadata::{AnnotationValue, Field, WeightValue};
17
18#[cfg(feature = "serde")]
19fn is_default<T: Default + PartialEq>(value: &T) -> bool {
20    value == &Default::default()
21}
22
23/// Transaction to apply to a MetadataStore.
24#[derive(Clone, Debug, PartialEq, bon::Builder)]
25#[cfg_attr(
26    feature = "serde",
27    derive(serde::Serialize, serde::Deserialize),
28    serde(default, rename_all = "camelCase")
29)]
30#[builder(on(String, into))]
31pub struct Transaction<N, K, L, W, WV, A, AV>
32where
33    N: Field,
34    K: Field,
35    L: Field,
36    W: Field,
37    WV: WeightValue,
38    A: Field,
39    AV: AnnotationValue,
40{
41    /// Transaction mode.
42    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_default"))]
43    #[builder(default)]
44    pub mode: TransactionMode,
45
46    /// Instance identifier.
47    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
48    #[builder(into)]
49    pub id: Option<Uuid>,
50
51    /// Name associated with this instance.
52    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
53    #[builder(into)]
54    pub name: Option<N>,
55
56    /// Kind of object or main category associated with this instance.
57    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
58    #[builder(into)]
59    pub kind: Option<K>,
60
61    /// Set of labels associated with this instance.
62    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
63    pub labels: Option<BTreeSet<L>>,
64
65    /// Numerical weights associated with this instance.
66    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
67    pub weights: Option<BTreeMap<W, WV>>,
68
69    /// Free-form annotations associated with this instance.
70    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
71    pub annotations: Option<BTreeMap<A, AV>>,
72}
73impl<N, K, L, W, WV, A, AV> Default for Transaction<N, K, L, W, WV, A, AV>
74where
75    N: Field,
76    K: Field,
77    L: Field,
78    W: Field,
79    WV: WeightValue,
80    A: Field,
81    AV: AnnotationValue,
82{
83    fn default() -> Self {
84        Self {
85            mode: Default::default(),
86            id: Default::default(),
87            name: Default::default(),
88            kind: Default::default(),
89            labels: Default::default(),
90            weights: Default::default(),
91            annotations: Default::default(),
92        }
93    }
94}
95
96impl<N, K, L, W, WV, A, AV> Transaction<N, K, L, W, WV, A, AV>
97where
98    N: Field,
99    K: Field,
100    L: Field,
101    W: Field,
102    WV: WeightValue,
103    A: Field,
104    AV: AnnotationValue,
105{
106    /// Strip the ID from the transaction.
107    pub fn strip_id(&mut self) -> Option<Uuid> {
108        self.id.take()
109    }
110}
111/// Metadata transaction mode.
112/// Whether to replace any existing value, or append to it where possible.
113/// This mainly changes the behavior for sets and maps.
114#[derive(Copy, Clone, Debug, Default, PartialEq)]
115#[cfg_attr(
116    feature = "serde",
117    derive(serde::Serialize, serde::Deserialize),
118    serde(rename_all = "camelCase")
119)]
120pub enum TransactionMode {
121    Replace,
122    #[default]
123    Append,
124}