Skip to main content

harmos_domain/state/
entry.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14pub use crate::journal::Position;
15
16pub use std::time::SystemTime as Timestamp;
17
18/* === Imports === */
19
20// Standard Library Imports
21use std::fmt;
22// Third Party Imports
23use serde::{Deserialize, Serialize};
24// Local Imports
25
26/* === Constants === */
27
28/* === Macros === */
29
30/* === Type Aliases === */
31
32/* === Structs === */
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
35#[serde(transparent)]
36pub struct CorrelationId(pub Position);
37
38#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
39#[serde(transparent)]
40pub struct CausationId(pub Position);
41
42#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
43#[serde(transparent)]
44pub struct PrincipalId(String);
45
46#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
47#[serde(transparent)]
48pub struct RequestKey(String);
49
50#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
51pub struct Metadata {
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub commit: Option<(Position, Position)>,
54
55    pub principal: Option<PrincipalId>,
56    pub correlation: CorrelationId,
57    pub causation: Option<CausationId>,
58    pub request: Option<RequestKey>,
59    pub link: Option<Link>,
60    pub at: Timestamp,
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub struct At<T> {
65    pub value: T,
66    pub position: Position,
67}
68
69#[derive(Clone, Debug, PartialEq, Eq)]
70pub struct Entry<X> {
71    pub prepared: Option<super::record::Prepared>,
72    pub position: Position,
73    pub metadata: Metadata,
74    pub value: X,
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
78#[serde(deny_unknown_fields)]
79pub struct Receipt {
80    pub frontier: Position,
81    pub position: Position,
82    pub correlation: CorrelationId,
83}
84
85/* === Enums === */
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
88pub enum Link {
89    UndoOf(Position),
90    RedoOf(Position),
91}
92
93/* === Traits === */
94
95/* === Implementations === */
96
97impl fmt::Display for CorrelationId {
98    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(formatter, "{}", self.0)
100    }
101}
102
103impl fmt::Display for CausationId {
104    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
105        write!(formatter, "{}", self.0)
106    }
107}
108
109impl PrincipalId {
110    pub fn new(id: impl Into<String>) -> Self {
111        Self(id.into())
112    }
113
114    pub fn as_str(&self) -> &str {
115        &self.0
116    }
117}
118
119impl fmt::Display for PrincipalId {
120    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
121        formatter.write_str(&self.0)
122    }
123}
124
125impl RequestKey {
126    pub fn new(key: impl Into<String>) -> Self {
127        Self(key.into())
128    }
129
130    pub fn as_str(&self) -> &str {
131        &self.0
132    }
133}
134
135impl fmt::Display for RequestKey {
136    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
137        formatter.write_str(&self.0)
138    }
139}
140
141/* === Public Functions === */
142
143/* === Private Functions === */
144
145/* === Tests === */