1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! CRDT primitive types that expose Automerge's collaborative semantics.
//!
//! This module provides types that leverage Automerge's CRDT (Conflict-free Replicated Data Type)
//! capabilities directly, rather than using last-writer-wins semantics.
//!
//! # Why Use These Types?
//!
//! Regular Rust types in Automorph use **last-writer-wins (LWW)** semantics:
//! - If User A and User B both change a `String` field concurrently, one wins
//! - If User A and User B both change a `u64` field concurrently, one wins
//!
//! The types in this module use **CRDT semantics**:
//! - `Counter`: Concurrent increments/decrements all merge correctly
//! - `Text`: Concurrent character insertions/deletions merge at character level
//!
//! # Example
//!
//! ```rust
//! use automorph::{Automorph, crdt::{Counter, Text}};
//! use automerge::{AutoCommit, ROOT};
//!
//! #[derive(Automorph, Default)]
//! struct WikiPage {
//! // LWW fields - simple values where "latest wins" is acceptable
//! title: String,
//! author: String,
//!
//! // CRDT fields - values that need proper concurrent merge
//! content: Text, // Collaborative text editing
//! view_count: Counter, // Concurrent increments merge
//! }
//!
//! let mut doc = AutoCommit::new();
//! let page = WikiPage {
//! title: "Home".to_string(),
//! author: "Alice".to_string(),
//! content: Text::new("Hello world"),
//! view_count: Counter::new(0),
//! };
//! page.save(&mut doc, &ROOT, "page").unwrap();
//! ```
//!
//! # When to Use Which
//!
//! | Scenario | Type | Why |
//! |----------|------|-----|
//! | Document title | `String` | Rarely edited concurrently, LWW is fine |
//! | Document body | `Text` | Frequently edited, need character-level merge |
//! | View counter | `Counter` | Multiple users viewing = concurrent increments |
//! | Like count | `Counter` | Multiple users liking = concurrent increments |
//! | User name | `String` | User controls their own name, no conflicts |
//! | Inventory qty | `Counter` | Multiple sources adding/removing stock |
//!
//! # See Also
//!
//! - [Automerge documentation](https://automerge.org/docs/) for more on CRDT semantics
//! - [`crate::Automorph`] trait for the general synchronization API
pub use Counter;
pub use Text;