MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / deserialize

Method deserialize

atomic-core/src/change/change.rs:568–843  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

566 /// - Any section fails to decompress or deserialize
567 /// - The content hash doesn't match the trailer
568 pub fn deserialize<R: Read>(reader: &mut R) -> Result<(Self, Hash), ChangeError> {
569 use format_v3::*;
570
571 // Detect V2 (bincode) format and give a clear error.
572 // V2 files start with a little-endian u64 version number (1 or 2).
573 // V3 files start with b"ATOM". If the first 4 bytes look like a
574 // small integer rather than ASCII "ATOM", it's an old format.
575 //
576 // We read into a buffer first so we can pass it to ChangeReader
577 // if it IS V3 (since Read is not Seek).
578 let mut header_peek = [0u8; 4];
579 reader.read_exact(&mut header_peek)?;
580
581 if &header_peek != b"ATOM" {
582 // Check if it looks like a V2 version number (1 or 2 as LE u64)
583 let maybe_version = u32::from_le_bytes(header_peek);
584 if maybe_version == 1 || maybe_version == 2 {
585 return Err(ChangeError::Invalid(format!(
586 "This change file uses the legacy V2 format (version {}). \
587 It must be re-recorded with the current version of Atomic. \
588 Delete .atomic/ and run: atomic init && atomic add -r . && atomic record -m \"re-record\"",
589 maybe_version
590 )));
591 }
592 return Err(ChangeError::Format(FormatError::InvalidMagic {
593 got: header_peek,
594 }));
595 }
596
597 // It's V3 — reconstruct a reader with the magic bytes prepended.
598 // Chain the 4 bytes we already read with the rest of the stream.
599 let rest = reader;
600 let mut combined = std::io::Cursor::new(header_peek).chain(rest);
601
602 // 1. Open the reader (reads header + hash table)
603 let mut change_reader = ChangeReader::open(&mut combined)?;
604 let file_header = *change_reader.file_header();
605 let format_version = file_header.version;
606 let hash_table = change_reader.hash_table().clone();
607 let strict_v2 = format_version == FORMAT_VERSION || format_version == SPAN_FORMAT_VERSION;
608
609 // 2. Read all sections. V2 enforces the canonical section manifest;
610 // V1 keeps its historical framing and gains only semantic defaults.
611 let mut legacy_header: Option<ChangeHeader> = None;
612 let mut decoded_v2: Option<format_v3::types::envelope::DecodedChangeHeaderV2> = None;
613 let mut dependencies: Vec<Hash> = Vec::new();
614 let mut provenance: Vec<Provenance> = Vec::new();
615 let mut hunks: Vec<GraphOp<Option<Hash>>> = Vec::new();
616 let mut span = None;
617 let mut file_ops: Vec<FileOps> = Vec::new();
618 let mut contents: Vec<u8> = Vec::new();
619 let mut unhashed: Option<serde_json::Value> = None;
620 let mut last_ordering = None;
621 let mut header_count = 0u32;
622 let mut deps_count = 0u32;
623 let mut provenance_count = 0u32;
624 let mut graph_count = 0u32;
625 let mut semantic_count = 0u32;

Calls 15

FormatEnum · 0.85
openFunction · 0.85
decode_change_header_v2Function · 0.85
decode_hashFunction · 0.85
deserialize_postcardFunction · 0.85
file_headerMethod · 0.80
hash_tableMethod · 0.80
next_sectionMethod · 0.80
orderingMethod · 0.80
expand_graph_opMethod · 0.80
extendMethod · 0.80