1use std::fmt;
2
3use gix_hash::{oid, ObjectId};
4
5use crate::{FullName, FullNameRef, Kind, Target, TargetRef};
6
7impl TargetRef<'_> {
8 pub fn kind(&self) -> Kind {
10 match self {
11 TargetRef::Symbolic(_) => Kind::Symbolic,
12 TargetRef::Object(_) => Kind::Object,
13 }
14 }
15 pub fn try_id(&self) -> Option<&oid> {
17 match self {
18 TargetRef::Symbolic(_) => None,
19 TargetRef::Object(oid) => Some(oid),
20 }
21 }
22 pub fn id(&self) -> &oid {
24 match self {
25 TargetRef::Symbolic(_) => panic!("BUG: tries to obtain object id from symbolic target"),
26 TargetRef::Object(oid) => oid,
27 }
28 }
29 pub fn try_name(&self) -> Option<&FullNameRef> {
31 match self {
32 TargetRef::Symbolic(name) => Some(name),
33 TargetRef::Object(_) => None,
34 }
35 }
36 pub fn into_owned(self) -> Target {
38 self.into()
39 }
40}
41
42impl Target {
43 pub fn kind(&self) -> Kind {
45 match self {
46 Target::Symbolic(_) => Kind::Symbolic,
47 Target::Object(_) => Kind::Object,
48 }
49 }
50
51 pub fn is_null(&self) -> bool {
53 match self {
54 Target::Object(oid) => oid.is_null(),
55 Target::Symbolic(_) => false,
56 }
57 }
58
59 pub fn to_ref(&self) -> TargetRef<'_> {
61 match self {
62 Target::Object(oid) => TargetRef::Object(oid),
63 Target::Symbolic(name) => TargetRef::Symbolic(name.as_ref()),
64 }
65 }
66
67 pub fn try_id(&self) -> Option<&oid> {
69 match self {
70 Target::Symbolic(_) => None,
71 Target::Object(oid) => Some(oid),
72 }
73 }
74 pub fn id(&self) -> &oid {
76 match self {
77 Target::Symbolic(_) => panic!("BUG: tries to obtain object id from symbolic target"),
78 Target::Object(oid) => oid,
79 }
80 }
81 pub fn into_id(self) -> ObjectId {
83 match self {
84 Target::Symbolic(_) => panic!("BUG: expected peeled reference target but found symbolic one"),
85 Target::Object(oid) => oid,
86 }
87 }
88
89 pub fn try_into_id(self) -> Result<ObjectId, Self> {
91 match self {
92 Target::Symbolic(_) => Err(self),
93 Target::Object(oid) => Ok(oid),
94 }
95 }
96 pub fn try_name(&self) -> Option<&FullNameRef> {
98 match self {
99 Target::Symbolic(name) => Some(name.as_ref()),
100 Target::Object(_) => None,
101 }
102 }
103}
104
105impl<'a> From<TargetRef<'a>> for Target {
106 fn from(src: TargetRef<'a>) -> Self {
107 match src {
108 TargetRef::Object(oid) => Target::Object(oid.to_owned()),
109 TargetRef::Symbolic(name) => Target::Symbolic(name.to_owned()),
110 }
111 }
112}
113
114impl<'a> PartialEq<TargetRef<'a>> for Target {
115 fn eq(&self, other: &TargetRef<'a>) -> bool {
116 match (self, other) {
117 (Target::Object(lhs), TargetRef::Object(rhs)) => lhs == rhs,
118 (Target::Symbolic(lhs), TargetRef::Symbolic(rhs)) => lhs.as_bstr() == rhs.as_bstr(),
119 _ => false,
120 }
121 }
122}
123
124impl From<ObjectId> for Target {
125 fn from(id: ObjectId) -> Self {
126 Target::Object(id)
127 }
128}
129
130impl TryFrom<Target> for ObjectId {
131 type Error = Target;
132
133 fn try_from(value: Target) -> Result<Self, Self::Error> {
134 match value {
135 Target::Object(id) => Ok(id),
136 Target::Symbolic(_) => Err(value),
137 }
138 }
139}
140
141impl From<FullName> for Target {
142 fn from(name: FullName) -> Self {
143 Target::Symbolic(name)
144 }
145}
146
147impl fmt::Display for Target {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 match self {
150 Target::Object(oid) => oid.fmt(f),
151 Target::Symbolic(name) => write!(f, "ref: {}", name.as_bstr()),
152 }
153 }
154}