Skip to main content

stateset_embedded/
activity_logs.rs

1//! Activity log operations (append-only subject history).
2
3use stateset_core::{ActivityLogEntry, ActivityLogFilter, ActivityLogId, RecordActivity, Result};
4use stateset_db::{Database, DatabaseCapability};
5use std::sync::Arc;
6use uuid::Uuid;
7
8/// Activity log operations.
9pub struct ActivityLogs {
10    db: Arc<dyn Database>,
11}
12
13impl std::fmt::Debug for ActivityLogs {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        f.debug_struct("ActivityLogs").finish_non_exhaustive()
16    }
17}
18
19impl ActivityLogs {
20    pub(crate) fn new(db: Arc<dyn Database>) -> Self {
21        Self { db }
22    }
23
24    /// Whether activity logs are supported by the active backend.
25    #[must_use]
26    pub fn is_supported(&self) -> bool {
27        self.db.supports_capability(DatabaseCapability::ActivityLogs)
28    }
29
30    fn ensure(&self) -> Result<()> {
31        self.db.ensure_capability(DatabaseCapability::ActivityLogs)
32    }
33
34    /// Record a new activity log entry.
35    pub fn record(&self, input: RecordActivity) -> Result<ActivityLogEntry> {
36        self.ensure()?;
37        self.db.activity_logs().record(input)
38    }
39
40    /// Get an entry by ID.
41    pub fn get(&self, id: ActivityLogId) -> Result<Option<ActivityLogEntry>> {
42        self.ensure()?;
43        self.db.activity_logs().get(id)
44    }
45
46    /// List entries with filter (most recent first).
47    pub fn list(&self, filter: ActivityLogFilter) -> Result<Vec<ActivityLogEntry>> {
48        self.ensure()?;
49        self.db.activity_logs().list(filter)
50    }
51
52    /// List the full history for a single subject, most recent first.
53    pub fn history_for_subject(
54        &self,
55        subject_type: &str,
56        subject_id: Uuid,
57    ) -> Result<Vec<ActivityLogEntry>> {
58        self.ensure()?;
59        self.db.activity_logs().history_for_subject(subject_type, subject_id)
60    }
61}