Skip to main content

es_entity/operation/
with_time.rs

1use crate::{clock::ClockHandle, db};
2
3use super::{AtomicOperation, hooks};
4
5pub trait AtomicOperationWithTime: AtomicOperation {
6    fn now(&self) -> chrono::DateTime<chrono::Utc>;
7}
8
9/// Wrapper that guarantees time is available, borrowing the underlying operation.
10pub struct OpWithTime<'a, Op: AtomicOperation + ?Sized> {
11    inner: &'a mut Op,
12    now: chrono::DateTime<chrono::Utc>,
13}
14
15impl<'a, Op: AtomicOperation + ?Sized> AtomicOperationWithTime for OpWithTime<'a, Op> {
16    fn now(&self) -> chrono::DateTime<chrono::Utc> {
17        self.now
18    }
19}
20
21impl<'a, Op: AtomicOperation + ?Sized> OpWithTime<'a, Op> {
22    /// Wraps an operation, using existing time if present, otherwise fetching from DB.
23    ///
24    /// Priority order:
25    /// 1. Cached time from operation
26    /// 2. Manual clock time if the operation's clock is manual
27    /// 3. Database time via `SELECT NOW()`
28    pub async fn cached_or_db_time(op: &'a mut Op) -> Result<Self, sqlx::Error> {
29        let now = if let Some(time) = op.maybe_now() {
30            time
31        } else if let Some(manual_time) = op.clock().manual_now() {
32            manual_time
33        } else {
34            db::database_now(op.as_executor()).await?
35        };
36        Ok(Self { inner: op, now })
37    }
38
39    /// Wraps with a specific time (uses existing if present).
40    pub fn cached_or_time(op: &'a mut Op, time: chrono::DateTime<chrono::Utc>) -> Self {
41        let now = op.maybe_now().unwrap_or(time);
42        Self { inner: op, now }
43    }
44
45    /// Wraps using system time (uses existing if present).
46    ///
47    /// Uses cached time if present, otherwise uses the operation's clock.
48    pub fn cached_or_clock_time(op: &'a mut Op) -> Self {
49        let now = op.maybe_now().unwrap_or_else(|| op.clock().now());
50        Self { inner: op, now }
51    }
52
53    pub fn now(&self) -> chrono::DateTime<chrono::Utc> {
54        self.now
55    }
56}
57
58impl<'a, Op: AtomicOperation + ?Sized> AtomicOperation for OpWithTime<'a, Op> {
59    fn maybe_now(&self) -> Option<chrono::DateTime<chrono::Utc>> {
60        Some(self.now)
61    }
62
63    fn clock(&self) -> &ClockHandle {
64        self.inner.clock()
65    }
66
67    fn as_executor(&mut self) -> &mut db::Connection {
68        self.inner.as_executor()
69    }
70
71    fn add_commit_hook<H: hooks::CommitHook>(&mut self, hook: H) -> Result<(), H> {
72        self.inner.add_commit_hook(hook)
73    }
74}