Skip to main content

ave_common/
sink.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::SchemaType;
5
6#[cfg(feature = "typescript")]
7use ts_rs::TS;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[cfg_attr(feature = "typescript", derive(TS))]
11#[cfg_attr(feature = "typescript", ts(export))]
12pub struct DataToSink {
13    pub event: DataToSinkEvent,
14    pub public_key: String,
15    pub event_request_timestamp: u64,
16    pub event_ledger_timestamp: u64,
17    pub sink_timestamp: u64,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[cfg_attr(feature = "typescript", derive(TS))]
22#[cfg_attr(feature = "typescript", ts(export))]
23#[serde(tag = "event", content = "data", rename_all = "snake_case")]
24pub enum DataToSinkEvent {
25    Create {
26        governance_id: Option<String>,
27        subject_id: String,
28        owner: String,
29        schema_id: SchemaType,
30        namespace: String,
31        sn: u64,
32    },
33    Fact {
34        governance_id: Option<String>,
35        subject_id: String,
36        schema_id: SchemaType,
37        issuer: String,
38        owner: String,
39        payload: Value,
40        sn: u64,
41    },
42    Transfer {
43        governance_id: Option<String>,
44        subject_id: String,
45        schema_id: SchemaType,
46        owner: String,
47        new_owner: String,
48        sn: u64,
49    },
50    Confirm {
51        governance_id: Option<String>,
52        subject_id: String,
53        schema_id: SchemaType,
54        sn: u64,
55    },
56    Reject {
57        governance_id: Option<String>,
58        subject_id: String,
59        schema_id: SchemaType,
60        sn: u64,
61    },
62    Eol {
63        governance_id: Option<String>,
64        subject_id: String,
65        schema_id: SchemaType,
66        sn: u64,
67    },
68}
69
70impl DataToSinkEvent {
71    pub fn get_subject_schema(&self) -> (String, String) {
72        match self {
73            DataToSinkEvent::Create {
74                subject_id,
75                schema_id,
76                ..
77            }
78            | DataToSinkEvent::Fact {
79                subject_id,
80                schema_id,
81                ..
82            }
83            | DataToSinkEvent::Transfer {
84                subject_id,
85                schema_id,
86                ..
87            }
88            | DataToSinkEvent::Confirm {
89                subject_id,
90                schema_id,
91                ..
92            }
93            | DataToSinkEvent::Reject {
94                subject_id,
95                schema_id,
96                ..
97            }
98            | DataToSinkEvent::Eol {
99                subject_id,
100                schema_id,
101                ..
102            } => (subject_id.clone(), schema_id.to_string()),
103        }
104    }
105}