gix_index/extension/end_of_index_entry/
write.rs

1use crate::extension::{end_of_index_entry::SIGNATURE, Signature};
2
3/// Write this extension to out and generate a hash of `hash_kind` over all `prior_extensions` which are specified as `(signature, size)`
4/// pair. `one_past_entries` is the offset to the first byte past the entries, which is also the first byte of the signature of the
5/// first extension in `prior_extensions`. Note that `prior_extensions` must have been written prior to this one, as the name suggests,
6/// allowing this extension to be the last one in the index file.
7///
8/// Even if there are no `prior_extensions`, this extension will be written unconditionally.
9pub fn write_to(
10    mut out: impl std::io::Write,
11    hash_kind: gix_hash::Kind,
12    offset_to_extensions: u32,
13    prior_extensions: impl IntoIterator<Item = (Signature, u32)>,
14) -> Result<(), std::io::Error> {
15    out.write_all(&SIGNATURE)?;
16    let extension_size: u32 = 4 + hash_kind.len_in_bytes() as u32;
17    out.write_all(&extension_size.to_be_bytes())?;
18
19    out.write_all(&offset_to_extensions.to_be_bytes())?;
20
21    let mut hasher = gix_features::hash::hasher(hash_kind);
22    for (signature, size) in prior_extensions {
23        hasher.update(&signature);
24        hasher.update(&size.to_be_bytes());
25    }
26    out.write_all(&hasher.digest())?;
27
28    Ok(())
29}