#![allow(clippy::empty_docs)]
use gix_path::RelativePath;
use gix_ref::file::ReferenceExt;
#[must_use = "Iterators should be obtained from this iterator platform"]
pub struct Platform<'r> {
pub(crate) platform: gix_ref::file::iter::Platform<'r>,
pub repo: &'r crate::Repository,
}
pub struct Iter<'packed, 'repo> {
inner: gix_ref::file::iter::LooseThenPacked<'packed, 'repo>,
peel_with_packed: Option<gix_ref::file::packed::SharedBufferSnapshot>,
peel: bool,
repo: &'repo crate::Repository,
}
impl<'packed, 'repo> Iter<'packed, 'repo> {
fn new(repo: &'repo crate::Repository, platform: gix_ref::file::iter::LooseThenPacked<'packed, 'repo>) -> Self {
Iter {
inner: platform,
peel_with_packed: None,
peel: false,
repo,
}
}
}
impl<'repo> Platform<'repo> {
pub fn all(&self) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(self.repo, self.platform.all()?))
}
pub fn prefixed<'a>(
&self,
prefix: impl TryInto<&'a RelativePath, Error = gix_path::relative_path::Error>,
) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(self.repo, self.platform.prefixed(prefix.try_into()?)?))
}
pub fn tags(&self) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(self.repo, self.platform.prefixed(b"refs/tags/".try_into()?)?))
}
pub fn local_branches(&self) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(
self.repo,
self.platform.prefixed(b"refs/heads/".try_into()?)?,
))
}
pub fn pseudo(&self) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(self.repo, self.platform.pseudo()?))
}
pub fn remote_branches(&self) -> Result<Iter<'_, 'repo>, init::Error> {
Ok(Iter::new(
self.repo,
self.platform.prefixed(b"refs/remotes/".try_into()?)?,
))
}
}
impl Iter<'_, '_> {
pub fn peeled(mut self) -> Result<Self, gix_ref::packed::buffer::open::Error> {
self.peel_with_packed = self.repo.refs.cached_packed_buffer()?;
self.peel = true;
Ok(self)
}
}
impl<'r> Iterator for Iter<'_, 'r> {
type Item = Result<crate::Reference<'r>, Box<dyn std::error::Error + Send + Sync + 'static>>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|res| {
res.map_err(|err| Box::new(err) as Box<dyn std::error::Error + Send + Sync + 'static>)
.and_then(|mut r| {
if self.peel {
let repo = &self.repo;
r.peel_to_id_packed(&repo.refs, &repo.objects, self.peel_with_packed.as_ref().map(|p| &***p))
.map_err(|err| Box::new(err) as Box<dyn std::error::Error + Send + Sync + 'static>)
.map(|_| r)
} else {
Ok(r)
}
})
.map(|r| crate::Reference::from_ref(r, self.repo))
})
}
}
pub mod init {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
RelativePath(#[from] gix_path::relative_path::Error),
}
}
pub type Error = gix_ref::packed::buffer::open::Error;