use git_object::mutable;
use std::io;
/// Describe the capability to write git objects into an object store.
pub trait Write {
/// The error type used for all trait methods.
///
/// _Note_ the default implementations require the `From<io::Error>` bound.
type Error: std::error::Error + From<io::Error>;
/// Write [`object`][mutable::Object] using the given kind of [`hash`][git_hash::Kind] into the database,
/// returning id to reference it in subsequent reads.
fn write(&self, object: &mutable::Object, hash: git_hash::Kind) -> Result<git_hash::ObjectId, Self::Error> {
let mut buf = Vec::with_capacity(2048);
object.write_to(&mut buf)?;
self.write_stream(object.kind(), buf.len() as u64, buf.as_slice(), hash)
}
/// As [`write`][Write::write], but takes an [`object` kind][git_object::Kind] along with its encoded bytes.
fn write_buf(
&self,
object: git_object::Kind,
from: &[u8],
hash: git_hash::Kind,
) -> Result<git_hash::ObjectId, Self::Error> {
self.write_stream(object, from.len() as u64, from, hash)
}
/// As [`write`][Write::write], but takes an input stream.
/// This is commonly used for writing blobs directly without reading them to memory first.
fn write_stream(
&self,
kind: git_object::Kind,
size: u64,
from: impl io::Read,
hash: git_hash::Kind,
) -> Result<git_hash::ObjectId, Self::Error>;
}