gix_index/extension/tree/
write.rs

1use crate::extension::{tree, Tree};
2
3impl Tree {
4    /// Serialize this instance to `out`.
5    pub fn write_to(&self, mut out: impl std::io::Write) -> Result<(), std::io::Error> {
6        fn tree_entry(out: &mut impl std::io::Write, tree: &Tree) -> Result<(), std::io::Error> {
7            let mut buf = itoa::Buffer::new();
8            let num_entries = match tree.num_entries {
9                Some(num_entries) => buf.format(num_entries),
10                None => buf.format(-1),
11            };
12
13            out.write_all(tree.name.as_slice())?;
14            out.write_all(b"\0")?;
15            out.write_all(num_entries.as_bytes())?;
16            out.write_all(b" ")?;
17            let num_children = buf.format(tree.children.len());
18            out.write_all(num_children.as_bytes())?;
19            out.write_all(b"\n")?;
20            if tree.num_entries.is_some() {
21                out.write_all(tree.id.as_bytes())?;
22            }
23
24            for child in &tree.children {
25                tree_entry(out, child)?;
26            }
27
28            Ok(())
29        }
30
31        let signature = tree::SIGNATURE;
32
33        let estimated_size = self.num_entries.unwrap_or(0) * (300 + 3 + 1 + 3 + 1 + 20);
34        let mut entries: Vec<u8> = Vec::with_capacity(estimated_size as usize);
35        tree_entry(&mut entries, self)?;
36
37        out.write_all(&signature)?;
38        out.write_all(&(u32::try_from(entries.len()).expect("less than 4GB tree extension")).to_be_bytes())?;
39        out.write_all(&entries)?;
40
41        Ok(())
42    }
43}