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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{borrow::Cow, convert::TryFrom, fmt, path::Path};
use bstr::{BStr, BString, ByteSlice};
use git_hash::{oid, ObjectId};
use crate::Kind;
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct FullName(pub(crate) BString);
impl TryFrom<&str> for FullName {
type Error = git_validate::refname::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(FullName(git_validate::refname(value.as_bytes().as_bstr())?.into()))
}
}
impl TryFrom<String> for FullName {
type Error = git_validate::refname::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
git_validate::refname(value.as_bytes().as_bstr())?;
Ok(FullName(value.into()))
}
}
impl TryFrom<&BStr> for FullName {
type Error = git_validate::refname::Error;
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
Ok(FullName(git_validate::refname(value)?.into()))
}
}
impl TryFrom<BString> for FullName {
type Error = git_validate::refname::Error;
fn try_from(value: BString) -> Result<Self, Self::Error> {
git_validate::refname(value.as_ref())?;
Ok(FullName(value))
}
}
impl<'a> From<crate::FullName<'a>> for FullName {
fn from(value: crate::FullName<'a>) -> Self {
FullName(value.as_bstr().into())
}
}
impl FullName {
pub fn to_partial(&self) -> crate::PartialName<'_> {
crate::PartialName(self.0.as_bstr())
}
pub fn borrow(&self) -> crate::FullName<'_> {
crate::FullName(self.0.as_bstr())
}
pub fn to_path(&self) -> Cow<'_, Path> {
self.0.to_path().expect("UTF-8 conversion always succeeds").into()
}
pub fn into_inner(self) -> BString {
self.0
}
pub fn as_bstr(&self) -> &BStr {
self.0.as_bstr()
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub enum Target {
Peeled(ObjectId),
Symbolic(FullName),
}
impl Target {
pub fn kind(&self) -> Kind {
match self {
Target::Symbolic(_) => Kind::Symbolic,
Target::Peeled(_) => Kind::Peeled,
}
}
pub fn is_null(&self) -> bool {
match self {
Target::Peeled(oid) => oid.is_null(),
Target::Symbolic(_) => false,
}
}
pub fn borrow(&self) -> crate::Target<'_> {
match self {
Target::Peeled(oid) => crate::Target::Peeled(oid),
Target::Symbolic(name) => crate::Target::Symbolic(name.0.as_bstr()),
}
}
pub fn as_id(&self) -> Option<&oid> {
match self {
Target::Symbolic(_) => None,
Target::Peeled(oid) => Some(oid),
}
}
pub fn as_name(&self) -> Option<&BStr> {
match self {
Target::Symbolic(name) => Some(name.as_bstr()),
Target::Peeled(_) => None,
}
}
pub fn must_exist() -> Self {
Target::Peeled(ObjectId::null_sha1())
}
}
impl<'a> From<crate::Target<'a>> for Target {
fn from(src: crate::Target<'a>) -> Self {
match src {
crate::Target::Peeled(oid) => Target::Peeled(oid.to_owned()),
crate::Target::Symbolic(name) => Target::Symbolic(FullName(name.to_owned())),
}
}
}
impl<'a> PartialEq<crate::Target<'a>> for Target {
fn eq(&self, other: &crate::Target<'a>) -> bool {
match (self, other) {
(Target::Peeled(lhs), crate::Target::Peeled(rhs)) => lhs == rhs,
(Target::Symbolic(lhs), crate::Target::Symbolic(rhs)) => lhs.as_bstr() == *rhs,
_ => false,
}
}
}
impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Target::Peeled(oid) => oid.fmt(f),
Target::Symbolic(name) => write!(f, "ref: {}", name.as_bstr()),
}
}
}