1use 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#[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 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_default"))]
43 #[builder(default)]
44 pub mode: TransactionMode,
45
46 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
48 #[builder(into)]
49 pub id: Option<Uuid>,
50
51 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
53 #[builder(into)]
54 pub name: Option<N>,
55
56 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
58 #[builder(into)]
59 pub kind: Option<K>,
60
61 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
63 pub labels: Option<BTreeSet<L>>,
64
65 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
67 pub weights: Option<BTreeMap<W, WV>>,
68
69 #[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 pub fn strip_id(&mut self) -> Option<Uuid> {
108 self.id.take()
109 }
110}
111#[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}