Skip to main content

peat_protocol/sync/
mod.rs

1//! Data Synchronization Abstraction Layer
2//!
3//! This module provides a unified interface for CRDT-based data synchronization,
4//! enabling Peat Protocol to work with multiple sync engines without changing
5//! business logic.
6//!
7//! ## Architecture
8//!
9//! The abstraction consists of four core traits:
10//!
11//! 1. **`DocumentStore`** - CRUD operations, queries, and live observers
12//! 2. **`PeerDiscovery`** - Finding and connecting to other nodes
13//! 3. **`SyncEngine`** - Controlling synchronization behavior
14//! 4. **`DataSyncBackend`** - Lifecycle management and trait composition
15//!
16//! ## Supported Backends
17//!
18//! - **Automerge + Iroh** - Open-source CRDT with QUIC-based P2P transport
19//!
20//! ## Usage Example
21//!
22//! ```rust,ignore
23//! use peat_protocol::sync::{DataSyncBackend, BackendConfig};
24//! use peat_protocol::storage::AutomergeBackend;
25//!
26//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
27//! // Create backend
28//! let backend = AutomergeBackend::new();
29//!
30//! // Initialize with config
31//! let config = BackendConfig {
32//!     app_id: "my-app".to_string(),
33//!     persistence_dir: "/tmp/data".into(),
34//!     // ...
35//! };
36//! backend.initialize(config).await?;
37//!
38//! // Start peer discovery and sync
39//! backend.peer_discovery().start().await?;
40//! backend.sync_engine().start_sync().await?;
41//!
42//! // Use document store
43//! let doc_store = backend.document_store();
44//! let doc = Document::new(fields);
45//! doc_store.upsert("collection", doc).await?;
46//!
47//! // Cleanup
48//! backend.shutdown().await?;
49//! # Ok(())
50//! # }
51//! ```
52//!
53//! ## Testing
54//!
55//! The abstraction enables testing without real backends:
56//!
57//! ```rust,ignore
58//! use peat_protocol::sync::mock::MockBackend;
59//!
60//! # async fn test_example() {
61//! let backend = MockBackend::new();
62//! // Test Peat protocol logic with mock backend
63//! # }
64//! ```
65//!
66//! ## Design Rationale
67//!
68//! See ADR-005 for full context on why this abstraction exists:
69//! - Eliminate vendor lock-in with proprietary sync engines
70//! - Enable open-source alternatives for DoD/NATO deployments
71//! - Support multiple sync strategies (Ditto, Automerge, custom)
72//! - Simplify testing (mocks, no real Ditto instances needed)
73
74pub mod traits;
75pub mod types;
76
77// Backend implementations
78#[cfg(feature = "automerge-backend")]
79pub mod automerge; // Automerge CRDT backend (E8 evaluation)
80
81// BLE translation layer (ADR-041, #557)
82#[cfg(feature = "bluetooth")]
83pub mod ble_translation;
84
85// Re-export core types and traits for convenience
86pub use traits::*;
87pub use types::*;
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_module_compiles() {
95        // Sanity check that all types and traits are accessible
96        let _: Option<Document> = None;
97        let _: Option<Query> = None;
98        let _: Option<BackendConfig> = None;
99    }
100}