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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Derive macros for bidirectional serialization between Rust types and
//! [Loro](https://loro.dev) CRDT containers — the Loro equivalent of
//! [autosurgeon](https://github.com/automerge/autosurgeon) for Automerge.
//!
//! `#[derive(Hydrate, Reconcile)]` generates field-level mapping between Rust
//! structs/enums and Loro containers. Only modified fields produce CRDT
//! operations, enabling efficient collaborative editing.
//!
//! # Quick Start
//!
//! ```rust
//! use loro::LoroDoc;
//! use lorosurgeon::{Hydrate, Reconcile, DocSync};
//!
//! #[derive(Debug, PartialEq, Hydrate, Reconcile)]
//! #[loro(root = "config")]
//! struct Config {
//! name: String,
//! version: i64,
//! position: Position,
//! }
//!
//! #[derive(Debug, PartialEq, Hydrate, Reconcile)]
//! struct Position { x: f64, y: f64 }
//!
//! let doc = LoroDoc::new();
//! let config = Config {
//! name: "hello".into(),
//! version: 1,
//! position: Position { x: 10.0, y: 20.0 },
//! };
//!
//! config.to_doc(&doc).unwrap();
//! doc.commit();
//!
//! let loaded = Config::from_doc(&doc).unwrap();
//! assert_eq!(loaded, config);
//! ```
//!
//! # Core Traits
//!
//! | Trait | Direction | Purpose |
//! |-------|-----------|---------|
//! | [`Hydrate`] | Loro → Rust | Read Rust types from Loro containers |
//! | [`Reconcile`] | Rust → Loro | Write Rust types into Loro containers |
//! | [`DocSync`] | Both | Root-level `to_doc()`/`from_doc()` via `#[loro(root)]` |
//!
//! Most users only need `#[derive(Hydrate, Reconcile)]` — the traits are
//! implemented automatically. Manual impls are covered in the trait docs.
//!
//! # Type Mappings
//!
//! ## Structs and Enums
//!
//! | Rust type | Loro storage |
//! |-----------|-------------|
//! | Named struct | [`LoroMap`](loro::LoroMap) — fields become keys |
//! | Newtype (`Foo(T)`) | Transparent — delegates to inner type |
//! | Newtype (`Foo(Vec<T>)`) | [`LoroList`](loro::LoroList) — special-cased |
//! | Tuple struct (`Foo(A, B)`) | [`LoroList`](loro::LoroList) — positional |
//! | Unit enum variant | `String` — variant name |
//! | Data enum variant | [`LoroMap`](loro::LoroMap) — `{ "Variant": data }` |
//!
//! ## Scalars
//!
//! | Rust | Loro |
//! |------|------|
//! | `bool` | `Bool` |
//! | `i8`–`i64`, `u8`–`u64`, `usize` | `I64` (overflow checked on hydration) |
//! | `f32`, `f64` | `Double` |
//! | `String` | `String` (scalar replace) |
//! | `String` + `#[loro(text)]` | [`LoroText`](loro::LoroText) (character-level LCS) |
//! | `Vec<u8>` | `Binary` |
//! | `Option<T>` | `Null` or `T` |
//! | `Box<T>`, `Cow<T>` | Transparent |
//! | `serde_json::Value` | deep conversion via `LoroValue` |
//!
//! ## Collections
//!
//! | Rust | Loro | Strategy |
//! |------|------|----------|
//! | `Vec<T>` | [`LoroList`](loro::LoroList) | Myers LCS diffing (requires `T: Hydrate + PartialEq`) |
//! | `Vec<T>` + `#[loro(movable)]` | [`LoroMovableList`](loro::LoroMovableList) | Key-based diffing with `mov()`/`set()` |
//! | `HashMap<String, V>` | [`LoroMap`](loro::LoroMap) | Put entries, delete stale keys |
//!
//! # Special Types
//!
//! - [`ByteArray<N>`](ByteArray) — fixed-size byte array, length-checked on hydration
//! - [`MaybeMissing<T>`](MaybeMissing) — distinguishes "key absent" from "key present" (unlike `Option`)
//! - [`VersionGuard`] — captures document version to detect stale-heads before write-back
//!
//! # Attributes
//!
//! ## Container-level
//!
//! - `#[loro(root = "key")]` — generate [`DocSync`] impl for root-level `to_doc()`/`from_doc()`
//!
//! ## Field-level
//!
//! - `#[key]` — identity key for [`LoroMovableList`](loro::LoroMovableList) diffing
//! - `#[loro(rename = "name")]` — use a different key name in Loro
//! - `#[loro(json)]` — store as JSON string via serde (coarse-grained fallback)
//! - `#[loro(text)]` — use [`LoroText`](loro::LoroText) with character-level LCS diffing (on `String` fields)
//! - `#[loro(movable)]` — use [`LoroMovableList`](loro::LoroMovableList) instead of [`LoroList`](loro::LoroList)
//! - `#[loro(default)]` — use `Default::default()` when key is absent
//! - `#[loro(default = "fn_name")]` — call a custom function when key is absent
//! - `#[loro(flatten)]` — inline nested struct fields into parent map
//! - `#[loro(with = "module")]` — custom hydrate + reconcile via `module::hydrate` / `module::reconcile`
//! - `#[loro(hydrate = "fn")]` — custom hydrate function only
//! - `#[loro(reconcile = "fn")]` — custom reconcile function only
//!
//! # Keyed List Diffing
//!
//! `#[loro(movable)]` + `#[key]` enables identity-preserving list reconciliation:
//!
//! ```rust
//! use lorosurgeon::{Hydrate, Reconcile};
//!
//! #[derive(Hydrate, Reconcile)]
//! struct Item {
//! #[key]
//! id: String,
//! value: i64,
//! }
//!
//! #[derive(Hydrate, Reconcile)]
//! struct Doc {
//! #[loro(movable)]
//! items: Vec<Item>,
//! }
//! ```
//!
//! Matched items use `set()` in-place (preserving CRDT element identity),
//! so two peers editing different fields of the same item merge correctly.
//!
//! # Concurrent Editing
//!
//! Because each struct field maps to its own Loro container key, two peers
//! can edit different fields of the same struct and merge without conflict:
//!
//! ```rust
//! use loro::LoroDoc;
//! use lorosurgeon::{Hydrate, Reconcile, DocSync};
//!
//! #[derive(Debug, PartialEq, Hydrate, Reconcile)]
//! #[loro(root = "doc")]
//! struct Document {
//! title: String,
//! version: i64,
//! }
//!
//! // Peer A
//! let doc_a = LoroDoc::new();
//! let state = Document { title: "Hello".into(), version: 1 };
//! state.to_doc(&doc_a).unwrap();
//! doc_a.commit();
//!
//! // Peer B starts from A's state
//! let doc_b = LoroDoc::new();
//! doc_b.import(&doc_a.export(loro::ExportMode::Snapshot).unwrap()).unwrap();
//!
//! // A changes title, B changes version — concurrently
//! let mut a = Document::from_doc(&doc_a).unwrap();
//! a.title = "World".into();
//! a.to_doc(&doc_a).unwrap();
//! doc_a.commit();
//!
//! let mut b = Document::from_doc(&doc_b).unwrap();
//! b.version = 2;
//! b.to_doc(&doc_b).unwrap();
//! doc_b.commit();
//!
//! // Merge — both changes preserved
//! doc_a.import(&doc_b.export(loro::ExportMode::updates(&doc_a.oplog_vv())).unwrap()).unwrap();
//! let merged = Document::from_doc(&doc_a).unwrap();
//! assert_eq!(merged, Document { title: "World".into(), version: 2 });
//! ```
//!
//! # Custom Serialization
//!
//! For fields that need custom logic, use `#[loro(with = "module")]`:
//!
//! ```rust
//! use lorosurgeon::{Hydrate, Reconcile};
//!
//! mod uppercase {
//! use loro::LoroMap;
//! use lorosurgeon::{HydrateError, ReconcileError, MapReconciler};
//!
//! pub fn hydrate(map: &LoroMap, key: &str) -> Result<String, HydrateError> {
//! lorosurgeon::hydrate_prop::<String>(map, key)
//! .map(|s| s.to_uppercase())
//! }
//!
//! pub fn reconcile(
//! value: &String,
//! m: &mut MapReconciler,
//! key: &str,
//! ) -> Result<(), ReconcileError> {
//! m.entry(key, &value.to_lowercase())
//! }
//! }
//!
//! #[derive(Hydrate, Reconcile)]
//! struct Config {
//! #[loro(with = "uppercase")]
//! name: String,
//! }
//! ```
//!
//! # Flatten
//!
//! `#[loro(flatten)]` inlines a nested struct's fields directly into the parent map:
//!
//! ```rust
//! use lorosurgeon::{Hydrate, Reconcile};
//!
//! #[derive(Hydrate, Reconcile)]
//! struct Position { x: f64, y: f64 }
//!
//! #[derive(Hydrate, Reconcile)]
//! struct Element {
//! id: String,
//! #[loro(flatten)]
//! pos: Position, // x, y written directly to Element's LoroMap
//! }
//! ```
//!
//! # Stale Heads Detection
//!
//! [`VersionGuard`] prevents write-back after concurrent modifications:
//!
//! ```rust
//! use loro::LoroDoc;
//! use lorosurgeon::{Hydrate, Reconcile, DocSync, VersionGuard};
//!
//! #[derive(Debug, PartialEq, Hydrate, Reconcile)]
//! #[loro(root = "data")]
//! struct Data { value: i64 }
//!
//! let doc = LoroDoc::new();
//! Data { value: 1 }.to_doc(&doc).unwrap();
//! doc.commit();
//!
//! let guard = VersionGuard::capture(&doc);
//! let mut state = Data::from_doc(&doc).unwrap();
//! state.value = 42;
//!
//! // If another thread modified doc here, check() would fail:
//! guard.check(&doc).unwrap();
//! state.to_doc(&doc).unwrap();
//! doc.commit();
//! ```
//!
//! # Optimizations
//!
//! - **No-op detection** — writing identical scalar values produces zero CRDT operations
//! - **LCS diffing** — `Vec<T>` uses Myers diff via [`similar`](https://docs.rs/similar)
//! for minimal insert/delete ops
//! - **Stale heads** — [`VersionGuard`] detects concurrent modifications before write-back
//!
//! # Feature Flags
//!
//! | Feature | Effect |
//! |---------|--------|
//! | `uuid` | [`Hydrate`]/[`Reconcile`] impls for [`uuid::Uuid`](https://docs.rs/uuid) (stored as 16-byte binary) |
// ── Public modules ────────────────────────────────────────────────────
// Modules with re-exported types — keep private to avoid duplicate paths in docs.
// ── Derive macros ─────────────────────────────────────────────────────
pub use ;
// ── Core traits ───────────────────────────────────────────────────────
pub use crate;
pub use crateHydrate;
pub use crate;
// ── Error types ───────────────────────────────────────────────────────
pub use crate;
// ── Special types ─────────────────────────────────────────────────────
pub use crateByteArray;
pub use crateMaybeMissing;
// ── Helpers for manual use ────────────────────────────────────────────
pub use crate;
// ── Derive macro codegen support ──────────────────────────────────────
//
// These are used by the generated code from `#[derive(Hydrate, Reconcile)]`.
// They are not part of the public API and may change without notice.
pub use crateHydrateResultExt;
pub use crate;
pub use crate;
pub use crate;
pub use cratereconcile_keyed_map;
pub use crate;