spatio_types/
config.rs

1use serde::{Deserialize, Serialize};
2use std::time::{Duration, SystemTime};
3
4/// Synchronization policy for persistence.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
6#[serde(rename_all = "snake_case")]
7pub enum SyncPolicy {
8    Never,
9    #[default]
10    EverySecond,
11    Always,
12}
13
14/// File synchronization strategy (fsync vs fdatasync).
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
16#[serde(rename_all = "snake_case")]
17pub enum SyncMode {
18    #[default]
19    All,
20    Data,
21}
22
23/// Options for setting values with TTL.
24#[derive(Debug, Clone, Default, Serialize, Deserialize)]
25pub struct SetOptions {
26    /// Time-to-live for this item
27    pub ttl: Option<Duration>,
28    /// Absolute expiration time (takes precedence over TTL)
29    pub expires_at: Option<SystemTime>,
30    /// Optional timestamp for the update (defaults to now if None)
31    pub timestamp: Option<SystemTime>,
32}
33
34impl SetOptions {
35    pub fn with_ttl(ttl: Duration) -> Self {
36        Self {
37            ttl: Some(ttl),
38            expires_at: None,
39            timestamp: None,
40        }
41    }
42
43    pub fn with_expiration(expires_at: SystemTime) -> Self {
44        Self {
45            ttl: None,
46            expires_at: Some(expires_at),
47            timestamp: None,
48        }
49    }
50
51    pub fn with_timestamp(timestamp: SystemTime) -> Self {
52        Self {
53            ttl: None,
54            expires_at: None,
55            timestamp: Some(timestamp),
56        }
57    }
58
59    pub fn effective_expires_at(&self) -> Option<SystemTime> {
60        self.expires_at
61            .or_else(|| self.ttl.map(|ttl| SystemTime::now() + ttl))
62    }
63}