use crate::{data, pack};
use git_object::mutable;
use std::io;
pub trait Write {
type Error: std::error::Error + From<io::Error>;
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)
}
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)
}
fn write_stream(
&self,
kind: git_object::Kind,
size: u64,
from: impl io::Read,
hash: git_hash::Kind,
) -> Result<git_hash::ObjectId, Self::Error>;
}
pub trait Locate {
type Error: std::error::Error + 'static;
fn locate<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::pack::cache::DecodeEntry,
) -> Result<Option<data::Object<'a>>, Self::Error>;
fn pack_entry(&self, object: &data::Object<'_>) -> Option<PackEntry<'_>>;
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)] pub struct PackEntry<'a> {
pub data: &'a [u8],
pub crc32: Option<u32>,
pub version: pack::data::Version,
}
mod locate_impls {
use crate::{data, data::Object, pack, PackEntry};
use git_hash::oid;
use std::ops::Deref;
impl<T> super::Locate for std::sync::Arc<T>
where
T: super::Locate,
{
type Error = T::Error;
fn locate<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
self.deref().locate(id, buffer, pack_cache)
}
fn pack_entry(&self, object: &data::Object<'_>) -> Option<PackEntry<'_>> {
self.deref().pack_entry(object)
}
}
impl<T> super::Locate for Box<T>
where
T: super::Locate,
{
type Error = T::Error;
fn locate<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
self.deref().locate(id, buffer, pack_cache)
}
fn pack_entry(&self, object: &data::Object<'_>) -> Option<PackEntry<'_>> {
self.deref().pack_entry(object)
}
}
}