1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//!
use crate::{
    easy,
    easy::{
        ext::ObjectAccessExt,
        object,
        object::{peel, Kind},
        ObjectRef,
    },
};

///
pub mod to_kind {
    mod error {

        use crate::easy::object;

        /// The error returned by [`Object::peel_to_kind()`][crate::easy::ObjectRef::peel_to_kind()].
        #[derive(Debug, thiserror::Error)]
        #[allow(missing_docs)]
        pub enum Error {
            #[error(transparent)]
            FindExistingObject(#[from] object::find::existing::Error),
            #[error("Last encountered object kind was {} while trying to peel to {}", .actual, .expected)]
            NotFound {
                actual: object::Kind,
                expected: object::Kind,
            },
        }
    }
    pub use error::Error;
}

impl<'repo, A> ObjectRef<'repo, A>
where
    A: easy::Access + Sized,
{
    // TODO: tests
    /// Follow tags to their target and commits to trees until the given `kind` of object is encountered.
    ///
    /// Note that this object doesn't necessarily have to be the end of the chain.
    /// Typical values are [`Kind::Commit`] or [`Kind::Tree`].
    pub fn peel_to_kind(mut self, kind: Kind) -> Result<Self, peel::to_kind::Error> {
        loop {
            match self.kind {
                any_kind if kind == any_kind => {
                    return Ok(self);
                }
                Kind::Commit => {
                    let tree_id = self
                        .try_to_commit_iter()
                        .expect("commit")
                        .tree_id()
                        .expect("valid commit");
                    let access = self.access;
                    drop(self);
                    self = access.find_object(tree_id)?;
                }
                Kind::Tag => {
                    let target_id = self.to_tag_iter().target_id().expect("valid tag");
                    let access = self.access;
                    drop(self);
                    self = access.find_object(target_id)?;
                }
                Kind::Tree | Kind::Blob => {
                    return Err(peel::to_kind::Error::NotFound {
                        actual: self.kind,
                        expected: kind,
                    })
                }
            }
        }
    }

    // TODO: tests
    /// Follow all tag object targets until a commit, tree or blob is reached.
    ///
    /// Note that this method is different from [`peel_to_kind(…)`][ObjectRef::peel_to_kind()] as it won't
    /// peel commits to their tree, but handles tags only.
    pub fn peel_tags_to_end(mut self) -> Result<Self, object::find::existing::Error> {
        loop {
            match self.kind {
                Kind::Commit | Kind::Tree | Kind::Blob => break Ok(self),
                Kind::Tag => {
                    let target_id = self.to_tag_iter().target_id().expect("valid tag");
                    let access = self.access;
                    drop(self);
                    self = access.find_object(target_id)?;
                }
            }
        }
    }
}