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.clone(),
26 subject_id: keepsake.subject().id.clone(),
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 AppliedKeepsake {
64 pub keepsake: Keepsake,
66 pub duplicate_prevented: bool,
68}
69
70#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72#[cfg_attr(feature = "postgres", derive(sqlx::FromRow))]
73pub struct TimedExpiryCandidate {
74 pub keepsake_id: Uuid,
76 pub relation_id: Uuid,
78 pub subject_kind: String,
80 pub subject_id: String,
82 pub due_at: DateTime<Utc>,
84}
85
86#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
88pub struct FulfilledExpiryCandidate {
89 pub keepsake_id: Uuid,
91 pub relation_id: Uuid,
93 pub subject_kind: String,
95 pub subject_id: String,
97 pub expiry_policy: ExpiryPolicy,
99}
100
101#[cfg(feature = "postgres")]
102impl<'row> sqlx::FromRow<'row, PgRow> for FulfilledExpiryCandidate {
103 fn from_row(row: &'row PgRow) -> Result<Self, sqlx::Error> {
104 let expiry_policy = serde_json::from_value(row.try_get("expiry_policy")?)
105 .map_err(|error| sqlx::Error::Decode(Box::new(error)))?;
106 Ok(Self {
107 keepsake_id: row.try_get("keepsake_id")?,
108 relation_id: row.try_get("relation_id")?,
109 subject_kind: row.try_get("subject_kind")?,
110 subject_id: row.try_get("subject_id")?,
111 expiry_policy,
112 })
113 }
114}