gix-object 0.65.0

Immutable and mutable git objects with decoding and encoding support
Documentation
use std::io;

use bstr::ByteSlice;

use crate::{
    Kind, Tree, TreeRef,
    encode::SPACE,
    tree::{Entry, EntryRef},
};

/// Serialization
impl crate::WriteTo for Tree {
    /// Serialize this tree to `out` in the git internal format.
    /// Invalid filename bytes are retained as `input` in the I/O error.
    /// After [wrapping](gix_error::Error::from_error()), inspect them with [metadata](gix_error::Error::metadata()).
    fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
        debug_assert_eq!(
            &self.entries,
            &{
                let mut entries_sorted = self.entries.clone();
                entries_sorted.sort();
                entries_sorted
            },
            "entries for serialization must be sorted by filename"
        );
        let mut buf = Default::default();
        for Entry { mode, filename, oid } in &self.entries {
            out.write_all(mode.as_bytes(&mut buf))?;
            out.write_all(SPACE)?;

            if filename.find_byte(0).is_some() {
                return Err(io::Error::other(
                    gix_error::validation("Nullbytes are invalid in file paths as they are separators")
                        .with("input", filename.as_bstr()),
                ));
            }
            out.write_all(filename)?;
            out.write_all(b"\0")?;

            out.write_all(oid.as_bytes())?;
        }
        Ok(())
    }

    fn kind(&self) -> Kind {
        Kind::Tree
    }

    fn size(&self) -> u64 {
        let mut buf = Default::default();
        self.entries
            .iter()
            .map(|Entry { mode, filename, oid }| {
                (mode.as_bytes(&mut buf).len() + 1 + filename.len() + 1 + oid.as_bytes().len()) as u64
            })
            .sum()
    }
}

/// Serialization
impl crate::WriteTo for TreeRef<'_> {
    /// Serialize this tree to `out` in the git internal format.
    /// Invalid filename bytes are retained as `input` in the I/O error.
    /// After [wrapping](gix_error::Error::from_error()), inspect them with [metadata](gix_error::Error::metadata()).
    fn write_to(&self, out: &mut dyn io::Write) -> io::Result<()> {
        debug_assert_eq!(
            &{
                let mut entries_sorted = self.entries.clone();
                entries_sorted.sort();
                entries_sorted
            },
            &self.entries,
            "entries for serialization must be sorted by filename"
        );
        let mut buf = Default::default();
        for EntryRef { mode, filename, oid } in &self.entries {
            out.write_all(mode.as_bytes(&mut buf))?;
            out.write_all(SPACE)?;

            if filename.find_byte(0).is_some() {
                return Err(io::Error::other(
                    gix_error::validation("Nullbytes are invalid in file paths as they are separators")
                        .with("input", *filename),
                ));
            }
            out.write_all(filename)?;
            out.write_all(b"\0")?;

            out.write_all(oid.as_bytes())?;
        }
        Ok(())
    }

    fn kind(&self) -> Kind {
        Kind::Tree
    }

    fn size(&self) -> u64 {
        let mut buf = Default::default();
        self.entries
            .iter()
            .map(|EntryRef { mode, filename, oid }| {
                (mode.as_bytes(&mut buf).len() + 1 + filename.len() + 1 + oid.as_bytes().len()) as u64
            })
            .sum()
    }
}