keepsake_sqlx/repository/
types.rs1use chrono::{DateTime, Utc};
2use keepsake::{AuditEvent, ExpiryPolicy, Keepsake};
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6#[cfg(feature = "postgres")]
7use sqlx::{Row, postgres::PgRow};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct MembershipCursor {
12 pub subject_kind: String,
14 pub subject_id: String,
16 pub keepsake_id: Uuid,
18}
19
20impl MembershipCursor {
21 #[must_use]
23 pub fn after(keepsake: &Keepsake) -> Self {
24 Self {
25 subject_kind: keepsake.subject().kind().to_owned(),
26 subject_id: keepsake.subject().id().to_owned(),
27 keepsake_id: keepsake.id(),
28 }
29 }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct AuditEventRecord {
35 pub id: i64,
37 pub event: AuditEvent,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct AuditCursor {
44 pub occurred_at: DateTime<Utc>,
46 pub id: i64,
48}
49
50impl AuditCursor {
51 #[must_use]
53 pub const fn after(record: &AuditEventRecord) -> Self {
54 Self {
55 occurred_at: record.event.at,
56 id: record.id,
57 }
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
63pub struct AuditOutboxCursor {
64 pub id: i64,
66}
67
68impl AuditOutboxCursor {
69 #[must_use]
71 pub const fn after(record: &AuditOutboxRecord) -> Self {
72 Self { id: record.id }
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78pub struct AuditOutboxRecord {
79 pub id: i64,
81 pub audit_event_id: i64,
83 pub event_type: String,
85 pub payload: AuditEvent,
87 pub claimed_by: Option<String>,
89 pub claimed_until: Option<DateTime<Utc>>,
91 pub delivered_at: Option<DateTime<Utc>>,
93}
94
95#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
97pub struct AppliedKeepsake {
98 pub keepsake: Keepsake,
100 pub duplicate_prevented: bool,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
106#[cfg_attr(feature = "postgres", derive(sqlx::FromRow))]
107pub struct TimedExpiryCandidate {
108 pub keepsake_id: Uuid,
110 pub relation_id: Uuid,
112 pub subject_kind: String,
114 pub subject_id: String,
116 pub due_at: DateTime<Utc>,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122pub struct FulfilledExpiryCandidate {
123 pub keepsake_id: Uuid,
125 pub relation_id: Uuid,
127 pub subject_kind: String,
129 pub subject_id: String,
131 pub expiry_policy: ExpiryPolicy,
133}
134
135#[cfg(feature = "postgres")]
136impl<'row> sqlx::FromRow<'row, PgRow> for FulfilledExpiryCandidate {
137 fn from_row(row: &'row PgRow) -> Result<Self, sqlx::Error> {
138 let expiry_policy = serde_json::from_value(row.try_get("expiry_policy")?)
139 .map_err(|error| sqlx::Error::Decode(Box::new(error)))?;
140 Ok(Self {
141 keepsake_id: row.try_get("keepsake_id")?,
142 relation_id: row.try_get("relation_id")?,
143 subject_kind: row.try_get("subject_kind")?,
144 subject_id: row.try_get("subject_id")?,
145 expiry_policy,
146 })
147 }
148}