Serialize this change to a writer using the V3 format. Writes the complete V3 change file (header, hash table, sections, trailer) to the given writer. The content hash is computed incrementally as sections are written. # Arguments `writer` - Where to write the serialized change # Returns The blake3 content hash of the change (its identity). # Errors Returns an error if serialization, compre
(&self, writer: &mut W)
| 384 | /// |
| 385 | /// Returns an error if serialization, compression, or I/O fails. |
| 386 | pub fn serialize<W: Write>(&self, writer: &mut W) -> Result<Hash, ChangeError> { |
| 387 | use format_v3::*; |
| 388 | |
| 389 | self.validate_v2()?; |
| 390 | |
| 391 | // 1. Build the hash dedup table from all referenced hashes |
| 392 | // Use a placeholder self-hash (we'll know the real one after finalize) |
| 393 | let placeholder_hash = [0u8; 32]; |
| 394 | let mut hash_table = HashDedupTable::new(placeholder_hash); |
| 395 | |
| 396 | // Collect all unique hashes from dependencies |
| 397 | for dep in &self.hashed.dependencies { |
| 398 | hash_table.insert(*dep.as_bytes())?; |
| 399 | } |
| 400 | for known in &self.hashed.extra_known { |
| 401 | hash_table.insert(*known.as_bytes())?; |
| 402 | } |
| 403 | if let Some(supersedes) = self.hashed.supersedes { |
| 404 | hash_table.insert(*supersedes.as_bytes())?; |
| 405 | } |
| 406 | for root in self.hashed.causal_frontier.roots() { |
| 407 | hash_table.insert(*root.as_bytes())?; |
| 408 | } |
| 409 | |
| 410 | // Collect all unique hashes from hunks |
| 411 | self.collect_hunk_hashes(&mut hash_table)?; |
| 412 | |
| 413 | // 2. Compute section counts |
| 414 | let has_provenance = !self.hashed.provenance.is_empty(); |
| 415 | let has_unhashed = self.unhashed.is_some(); |
| 416 | let has_signature = self.signature.is_some(); |
| 417 | let has_content = !self.contents.is_empty(); |
| 418 | |
| 419 | // Chunk content with FastCDC for delta transfer + parallel compression |
| 420 | let content_chunks = if has_content { |
| 421 | format_v3::chunk_content(&self.contents, &format_v3::ChunkingOptions::default()) |
| 422 | } else { |
| 423 | Vec::new() |
| 424 | }; |
| 425 | |
| 426 | // For now, we write all hunks in a single GRAPH section (per-file splitting is future work) |
| 427 | let graph_section_count = if self.hashed.hunks.is_empty() { |
| 428 | 0u32 |
| 429 | } else { |
| 430 | 1 |
| 431 | }; |
| 432 | let semantic_section_count = if self.hashed.file_ops.is_empty() { |
| 433 | 0u32 |
| 434 | } else { |
| 435 | 1 |
| 436 | }; |
| 437 | let contents_chunks_count = content_chunks.len() as u32; |
| 438 | |
| 439 | // 3. Build file header |
| 440 | let mut file_header_builder = FileHeader::builder() |
| 441 | .hash_table_entries(hash_table.len() as u32) |
| 442 | .graph_section_count(graph_section_count) |
| 443 | .semantic_section_count(semantic_section_count) |