Skip to main content

surreal_sync_core/checkpoint/
config.rs

1//! Sync configuration for checkpoint operations.
2
3/// Checkpoint storage backend
4#[derive(Debug, Clone)]
5pub enum CheckpointStorage {
6    /// No checkpoint storage
7    Disabled,
8    /// Filesystem storage at specified directory
9    Filesystem { dir: String },
10    /// SurrealDB storage in specified table
11    SurrealDB {
12        table_name: String,
13        namespace: String,
14        database: String,
15    },
16}
17
18/// Configuration for sync operations.
19///
20/// Controls checkpoint emission and storage behavior.
21#[derive(Debug, Clone)]
22pub struct SyncConfig {
23    /// Whether to run in incremental mode.
24    ///
25    /// When `true`, the sync operation will:
26    /// - Read from a starting checkpoint
27    /// - Process only changes since that checkpoint
28    pub incremental: bool,
29
30    /// Whether to emit checkpoints during sync.
31    ///
32    /// When `true`, checkpoints are written according to `checkpoint_storage`.
33    pub emit_checkpoints: bool,
34
35    /// Checkpoint storage backend configuration
36    pub checkpoint_storage: CheckpointStorage,
37}
38
39impl Default for SyncConfig {
40    fn default() -> Self {
41        Self {
42            incremental: false,
43            emit_checkpoints: true,
44            checkpoint_storage: CheckpointStorage::Filesystem {
45                dir: ".surreal-sync-checkpoints".to_string(),
46            },
47        }
48    }
49}
50
51impl SyncConfig {
52    /// Create a new sync config with default values.
53    pub fn new() -> Self {
54        Self::default()
55    }
56
57    /// Create a config for full sync with filesystem checkpoint emission.
58    pub fn full_sync_with_checkpoints(checkpoint_dir: String) -> Self {
59        Self {
60            incremental: false,
61            emit_checkpoints: true,
62            checkpoint_storage: CheckpointStorage::Filesystem {
63                dir: checkpoint_dir,
64            },
65        }
66    }
67
68    /// Create a config for full sync with SurrealDB checkpoint emission.
69    pub fn full_sync_with_surrealdb_checkpoints(
70        table_name: String,
71        namespace: String,
72        database: String,
73    ) -> Self {
74        Self {
75            incremental: false,
76            emit_checkpoints: true,
77            checkpoint_storage: CheckpointStorage::SurrealDB {
78                table_name,
79                namespace,
80                database,
81            },
82        }
83    }
84
85    /// Create a config for incremental sync.
86    pub fn incremental() -> Self {
87        Self {
88            incremental: true,
89            emit_checkpoints: false,
90            checkpoint_storage: CheckpointStorage::Disabled,
91        }
92    }
93
94    /// Check if checkpoint emission is enabled and configured.
95    pub fn should_emit_checkpoints(&self) -> bool {
96        self.emit_checkpoints && !matches!(self.checkpoint_storage, CheckpointStorage::Disabled)
97    }
98}