Skip to main content

keepsake_sqlx/repository/
types.rs

1use 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/// Keyset cursor for active relation membership scans.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct MembershipCursor {
12    /// Last seen subject kind.
13    pub subject_kind: String,
14    /// Last seen subject id.
15    pub subject_id: String,
16    /// Last seen keepsake id.
17    pub keepsake_id: Uuid,
18}
19
20impl MembershipCursor {
21    /// Creates a cursor positioned after a returned keepsake.
22    #[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/// A persisted audit event together with its stable storage id.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct AuditEventRecord {
35    /// Monotonic audit row id, stable for keyset pagination.
36    pub id: i64,
37    /// Reconstructed audit event.
38    pub event: AuditEvent,
39}
40
41/// Keyset cursor for audit event reads in stable `(occurred_at, id)` order.
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct AuditCursor {
44    /// Last seen occurrence time.
45    pub occurred_at: DateTime<Utc>,
46    /// Last seen audit row id.
47    pub id: i64,
48}
49
50impl AuditCursor {
51    /// Creates a cursor positioned after a returned audit event.
52    #[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/// Result of an apply operation.
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
63pub struct AppliedKeepsake {
64    /// Created keepsake, or the existing active keepsake for duplicate applies.
65    pub keepsake: Keepsake,
66    /// Whether a duplicate active keepsake was prevented.
67    pub duplicate_prevented: bool,
68}
69
70/// Due timed expiry candidate.
71#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72#[cfg_attr(feature = "postgres", derive(sqlx::FromRow))]
73pub struct TimedExpiryCandidate {
74    /// Keepsake id.
75    pub keepsake_id: Uuid,
76    /// Relation id.
77    pub relation_id: Uuid,
78    /// Subject kind.
79    pub subject_kind: String,
80    /// Subject id.
81    pub subject_id: String,
82    /// Due timestamp.
83    pub due_at: DateTime<Utc>,
84}
85
86/// Due fulfillment expiry candidate.
87#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
88pub struct FulfilledExpiryCandidate {
89    /// Keepsake id.
90    pub keepsake_id: Uuid,
91    /// Relation id.
92    pub relation_id: Uuid,
93    /// Subject kind.
94    pub subject_kind: String,
95    /// Subject id.
96    pub subject_id: String,
97    /// Copied expiry policy.
98    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}