Skip to main content

write

Function write 

Source
pub fn write(
    root: &Cid,
    blocks: impl IntoIterator<Item = (Cid, Vec<u8>)>,
) -> Vec<u8> ⓘ
Expand description

Writes a CARv1 stream with one root.

See write_roots for the general form; this is the shape a repository export takes, where the one root is the signed commit.

Blocks are written in the order the iterator hands them over. Nothing in the format cares — a reader indexes by CID as it goes — but a caller that wants a byte-identical export from identical state should hand over a stable order, which is what an ordered map gives for free.

A block whose CID does not name it is not detected here. The writer is handed pairs and trusts them; the check belongs where the pair is made, and every producer in this crate makes it by hashing.

use didbot_data::Cid;
use didbot_repo::car;

let block = b"not a real block".to_vec();
let cid = Cid::of_raw(&block);
let car = car::write(&cid, [(cid.clone(), block)]);
assert!(car.len() > 60);