Deserialize a change from a reader (V3 format). Reads a complete V3 change file, validates the structure, decompresses sections, and verifies the content hash against the trailer. # Arguments `reader` - Where to read the serialized change from # Returns A tuple of `(change, hash)` where hash is the verified content hash. # Errors Returns an error if: - The file header is invalid (wrong magi
(reader: &mut R)
| 707 | /// - Any section fails to decompress or deserialize |
| 708 | /// - The content hash doesn't match the trailer |
| 709 | pub fn deserialize<R: Read>(reader: &mut R) -> Result<(Self, Hash), ChangeError> { |
| 710 | use format_v3::*; |
| 711 | |
| 712 | // Detect V2 (bincode) format and give a clear error. |
| 713 | // V2 files start with a little-endian u64 version number (1 or 2). |
| 714 | // V3 files start with b"ATOM". If the first 4 bytes look like a |
| 715 | // small integer rather than ASCII "ATOM", it's an old format. |
| 716 | // |
| 717 | // We read into a buffer first so we can pass it to ChangeReader |
| 718 | // if it IS V3 (since Read is not Seek). |
| 719 | let mut header_peek = [0u8; 4]; |
| 720 | reader.read_exact(&mut header_peek)?; |
| 721 | |
| 722 | if &header_peek != b"ATOM" { |
| 723 | // Check if it looks like a V2 version number (1 or 2 as LE u64) |
| 724 | let maybe_version = u32::from_le_bytes(header_peek); |
| 725 | if maybe_version == 1 || maybe_version == 2 { |
| 726 | return Err(ChangeError::Invalid(format!( |
| 727 | "This change file uses the legacy V2 format (version {}). \ |
| 728 | It must be re-recorded with the current version of Atomic. \ |
| 729 | Delete .atomic/ and run: atomic init && atomic add -r . && atomic record -m \"re-record\"", |
| 730 | maybe_version |
| 731 | ))); |
| 732 | } |
| 733 | return Err(ChangeError::Format(FormatError::InvalidMagic { |
| 734 | got: header_peek, |
| 735 | })); |
| 736 | } |
| 737 | |
| 738 | // It's V3 — reconstruct a reader with the magic bytes prepended. |
| 739 | // Chain the 4 bytes we already read with the rest of the stream. |
| 740 | let rest = reader; |
| 741 | let mut combined = std::io::Cursor::new(header_peek).chain(rest); |
| 742 | |
| 743 | // 1. Open the reader (reads header + hash table) |
| 744 | let mut change_reader = ChangeReader::open(&mut combined)?; |
| 745 | let file_header = *change_reader.file_header(); |
| 746 | let format_version = file_header.version; |
| 747 | let expected_codecs = if format_version == FIELD_CODEC_FORMAT_VERSION { |
| 748 | Some(codecs::CodecDescriptor::from_header(&file_header)?) |
| 749 | } else { |
| 750 | None |
| 751 | }; |
| 752 | let graph_encoding = if file_header.has_graph_fields() { |
| 753 | graph_fields::GraphEncoding::FieldsV1 |
| 754 | } else { |
| 755 | graph_fields::GraphEncoding::Raw |
| 756 | }; |
| 757 | let hash_table = change_reader.hash_table().clone(); |
| 758 | let strict_v2 = format_version != LEGACY_FORMAT_VERSION; |
| 759 | |
| 760 | // 2. Read all sections. V2 enforces the canonical section manifest; |
| 761 | // V1 keeps its historical framing and gains only semantic defaults. |
| 762 | let mut legacy_header: Option<ChangeHeader> = None; |
| 763 | let mut decoded_v2: Option<format_v3::types::envelope::DecodedChangeHeaderV2> = None; |
| 764 | let mut dependencies: Vec<Hash> = Vec::new(); |
| 765 | let mut provenance: Vec<Provenance> = Vec::new(); |
| 766 | let mut hunks: Vec<GraphOp<Option<Hash>>> = Vec::new(); |