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
use git_hash::{oid, ObjectId};

use crate::{
    easy,
    easy::{object::find, Object, ObjectRef, Oid},
};

impl<'repo, A, B> PartialEq<Oid<'repo, A>> for Oid<'repo, B> {
    fn eq(&self, other: &Oid<'repo, A>) -> bool {
        self.id == other.id
    }
}

impl<'repo, A> PartialEq<ObjectId> for Oid<'repo, A> {
    fn eq(&self, other: &ObjectId) -> bool {
        &self.id == other
    }
}

impl<'repo, A> PartialEq<oid> for Oid<'repo, A> {
    fn eq(&self, other: &oid) -> bool {
        self.id == other
    }
}

impl<'repo, A, B> PartialEq<ObjectRef<'repo, A>> for Oid<'repo, B> {
    fn eq(&self, other: &ObjectRef<'repo, A>) -> bool {
        self.id == other.id
    }
}

impl<'repo, A> PartialEq<Object> for Oid<'repo, A> {
    fn eq(&self, other: &Object) -> bool {
        self.id == other.id
    }
}

impl<'repo, A> std::fmt::Debug for Oid<'repo, A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.id.fmt(f)
    }
}

impl<'repo, A> AsRef<oid> for Oid<'repo, A> {
    fn as_ref(&self) -> &oid {
        &self.id
    }
}

impl<'repo, A> From<Oid<'repo, A>> for ObjectId {
    fn from(v: Oid<'repo, A>) -> Self {
        v.id
    }
}

impl<'repo, A> Oid<'repo, A>
where
    A: easy::Access + Sized,
{
    // NOTE: Can't access other object data that is attached to the same cache.
    /// Find the [`ObjectRef`] associated with this object id, and assume it exists.
    pub fn object(&self) -> Result<ObjectRef<'repo, A>, find::existing::Error> {
        crate::easy::ext::object::find_object(self.access, self.id)
    }

    // NOTE: Can't access other object data that is attached to the same cache.
    /// Try find the [`ObjectRef`] associated with this object id, it might not be available locally.
    pub fn try_object(&self) -> Result<Option<ObjectRef<'repo, A>>, find::Error> {
        crate::easy::ext::object::try_find_object(self.access, self.id)
    }
}

impl<'repo, A> Oid<'repo, A>
where
    A: easy::Access + Sized,
{
    pub(crate) fn from_id(id: impl Into<ObjectId>, access: &'repo A) -> Self {
        Oid { id: id.into(), access }
    }

    /// Turn this instance into its bare [ObjectId].
    pub fn detach(self) -> ObjectId {
        self.id
    }
}