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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Defines the `ParcodeVisitor` trait for graph construction.
//!
//! This module provides the core abstraction that allows types to define how they should
//! be decomposed into nodes in the serialization graph. The visitor pattern is central to
//! Parcode's architecture, enabling flexible and extensible serialization strategies.
//!
//! ## The Visitor Pattern in Parcode
//!
//! The [`ParcodeVisitor`] trait is inspired by the classic Visitor pattern but adapted for
//! graph construction rather than traversal. When you call `object.visit(graph, ...)`, the
//! object:
//!
//! 1. Creates a node for itself in the graph
//! 2. Recursively visits its children (fields marked with `#[parcode(chunkable)]` or `#[parcode(map)]`)
//! 3. Links child nodes to its own node, establishing the dependency graph
//!
//! ## Visitor vs. Serialize
//!
//! While `serde::Serialize` converts objects to bytes, `ParcodeVisitor` converts objects to
//! graph nodes:
//!
//! - **`Serialize`:** `Object → Bytes`
//! - **`ParcodeVisitor`:** `Object → Graph Nodes + Dependencies`
//!
//! The actual serialization to bytes happens later, during graph execution, using the
//! `SerializationJob` trait.
//!
//! ## Two Modes of Visiting
//!
//! The trait defines two visiting modes:
//!
//! ### 1. Standard Visit ([`visit`](ParcodeVisitor::visit))
//!
//! Used when the object is the root or a direct child of another node. The object creates
//! its own node in the graph.
//!
//! ```text
//! Parent Node
//! ↓
//! [visit creates a new node]
//! ↓
//! Child Node (this object)
//! ↓
//! [recursively visits its own children]
//! ```
//!
//! ### 2. Inlined Visit ([`visit_inlined`](ParcodeVisitor::visit_inlined))
//!
//! Used when the object is part of a collection (e.g., an element in `Vec<T>`). The object's
//! local data is already serialized in the parent's payload, so it doesn't create a new node.
//! However, it must still visit its remote children (chunkable fields) and link them to the
//! parent.
//!
//! ```text
//! Vec<MyStruct> Node
//! ↓
//! [MyStruct instances are inlined in the Vec's payload]
//! ↓
//! [visit_inlined links remote children to the Vec node]
//! ↓
//! Remote Child Nodes
//! ```
//!
//! ## Serialization Methods
//!
//! The trait also provides methods for serializing data:
//!
//! - **[`serialize_shallow`](ParcodeVisitor::serialize_shallow):** Serializes only local fields
//! (ignoring chunkable/map fields). Used when the object is inlined in a collection.
//!
//! - **[`serialize_slice`](ParcodeVisitor::serialize_slice):** Serializes a slice of objects.
//! The default implementation uses bincode for the entire slice, but types with chunkable
//! fields should override this to call `serialize_shallow` for each element.
//!
//! ## Derive Macro
//!
//! The `#[derive(ParcodeObject)]` macro automatically implements this trait based on field
//! attributes:
//!
//! - **No attribute:** Field is serialized inline (part of the node's payload)
//! - **`#[parcode(chunkable)]`:** Field becomes a child node
//! - **`#[parcode(map)]`:** Field becomes a sharded map (multiple child nodes)
//!
//! ## Example Implementation
//!
//! ```rust
//! use parcode::visitor::ParcodeVisitor;
//! use parcode::graph::{TaskGraph, ChunkId, JobConfig, SerializationJob};
//! use parcode::format::ChildRef;
//! use parcode::error::Result;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct MyStruct {
//! local_field: i32, // Inlined
//! remote_field: Vec<u8>, // Chunkable (separate node)
//! }
//!
//! #[derive(Clone)]
//! struct MyStructJob<'a> { data: &'a MyStruct }
//! impl<'a> SerializationJob<'a> for MyStructJob<'a> {
//! fn execute(&self, _: &[ChildRef]) -> Result<Vec<u8>> {
//! Ok(parcode::internal::bincode::serde::encode_to_vec(&self.data.local_field, parcode::internal::bincode::config::standard()).map_err(|e| parcode::ParcodeError::Serialization(e.to_string()))?)
//! }
//! fn estimated_size(&self) -> usize { 4 }
//! }
//!
//! impl ParcodeVisitor for MyStruct {
//! fn visit<'a>(&'a self, graph: &mut TaskGraph<'a>, parent_id: Option<ChunkId>, config: Option<JobConfig>) {
//! // 1. Create a node for this struct
//! let my_id = graph.add_node(self.create_job(config));
//!
//! // 2. Link to parent if present
//! if let Some(pid) = parent_id {
//! graph.link_parent_child(pid, my_id);
//! }
//!
//! // 3. Visit remote children
//! self.remote_field.visit(graph, Some(my_id), None);
//! }
//!
//! fn create_job<'a>(&'a self, config: Option<JobConfig>) -> Box<dyn SerializationJob<'a> + 'a> {
//! // Return a job that serializes local_field
//! Box::new(MyStructJob { data: self })
//! }
//! }
//! ```
use crate;
/// A trait for types that can be structurally visited to build a Parcode [`TaskGraph`].
///
/// This trait is the core abstraction for graph construction in Parcode. It allows types to
/// define how they should be decomposed into nodes and how dependencies between nodes should
/// be established.
///
/// ## Relationship to Other Traits
///
/// - **`serde::Serialize`:** Required for actual byte serialization (used by `SerializationJob`)
/// - **`ParcodeVisitor`:** Defines graph structure (this trait)
/// - **`SerializationJob`:** Executes the actual serialization of a node's payload
/// - **`ParcodeNative`:** Defines deserialization strategy (reader-side)
///
/// ## Automatic Implementation
///
/// This trait is typically implemented automatically via `#[derive(ParcodeObject)]`. Manual
/// implementation is only needed for custom serialization strategies.
///
/// ## Thread Safety
///
/// Implementations must be `Sync` to support parallel graph execution. This is automatically
/// satisfied for most types.