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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::{
immutable,
immutable::{Blob, Commit, Tag, Tree},
Kind,
};
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Object<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
Tree(Tree<'a>),
Blob(Blob<'a>),
Commit(Commit<'a>),
Tag(Tag<'a>),
}
impl<'a> Object<'a> {
pub fn from_bytes(kind: Kind, data: &'a [u8]) -> Result<Object<'a>, decode::Error> {
Ok(match kind {
Kind::Tree => Object::Tree(Tree::from_bytes(data)?),
Kind::Blob => Object::Blob(Blob { data }),
Kind::Commit => Object::Commit(Commit::from_bytes(data)?),
Kind::Tag => Object::Tag(Tag::from_bytes(data)?),
})
}
pub fn into_mutable(self) -> crate::mutable::Object {
self.into()
}
pub fn to_mutable(&self) -> crate::mutable::Object {
self.clone().into()
}
}
impl<'a> Object<'a> {
pub fn as_blob(&self) -> Option<&immutable::Blob<'a>> {
match self {
Object::Blob(v) => Some(v),
_ => None,
}
}
pub fn into_blob(self) -> Option<immutable::Blob<'a>> {
match self {
Object::Blob(v) => Some(v),
_ => None,
}
}
pub fn as_commit(&self) -> Option<&immutable::Commit<'a>> {
match self {
Object::Commit(v) => Some(v),
_ => None,
}
}
pub fn into_commit(self) -> Option<immutable::Commit<'a>> {
match self {
Object::Commit(v) => Some(v),
_ => None,
}
}
pub fn as_tree(&self) -> Option<&immutable::Tree<'a>> {
match self {
Object::Tree(v) => Some(v),
_ => None,
}
}
pub fn into_tree(self) -> Option<immutable::Tree<'a>> {
match self {
Object::Tree(v) => Some(v),
_ => None,
}
}
pub fn as_tag(&self) -> Option<&immutable::Tag<'a>> {
match self {
Object::Tag(v) => Some(v),
_ => None,
}
}
pub fn into_tag(self) -> Option<immutable::Tag<'a>> {
match self {
Object::Tag(v) => Some(v),
_ => None,
}
}
pub fn kind(&self) -> Kind {
match self {
Object::Tree(_) => Kind::Tree,
Object::Blob(_) => Kind::Blob,
Object::Commit(_) => Kind::Commit,
Object::Tag(_) => Kind::Tag,
}
}
}
mod convert {
use std::convert::TryFrom;
use crate::immutable::{Blob, Commit, Object, Tag, Tree};
impl<'a> From<Tag<'a>> for Object<'a> {
fn from(v: Tag<'a>) -> Self {
Object::Tag(v)
}
}
impl<'a> From<Commit<'a>> for Object<'a> {
fn from(v: Commit<'a>) -> Self {
Object::Commit(v)
}
}
impl<'a> From<Tree<'a>> for Object<'a> {
fn from(v: Tree<'a>) -> Self {
Object::Tree(v)
}
}
impl<'a> From<Blob<'a>> for Object<'a> {
fn from(v: Blob<'a>) -> Self {
Object::Blob(v)
}
}
impl<'a> TryFrom<Object<'a>> for Tag<'a> {
type Error = Object<'a>;
fn try_from(value: Object<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Object::Tag(v) => v,
_ => return Err(value),
})
}
}
impl<'a> TryFrom<Object<'a>> for Commit<'a> {
type Error = Object<'a>;
fn try_from(value: Object<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Object::Commit(v) => v,
_ => return Err(value),
})
}
}
impl<'a> TryFrom<Object<'a>> for Tree<'a> {
type Error = Object<'a>;
fn try_from(value: Object<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Object::Tree(v) => v,
_ => return Err(value),
})
}
}
impl<'a> TryFrom<Object<'a>> for Blob<'a> {
type Error = Object<'a>;
fn try_from(value: Object<'a>) -> Result<Self, Self::Error> {
Ok(match value {
Object::Blob(v) => v,
_ => return Err(value),
})
}
}
}
#[cfg(feature = "verbose-object-parsing-errors")]
pub mod decode {
use crate::bstr::{BString, ByteSlice};
pub type ParseError<'a> = nom::error::VerboseError<&'a [u8]>;
pub type ParseErrorOwned = nom::error::VerboseError<BString>;
#[derive(Debug, Clone)]
pub struct Error {
pub inner: ParseErrorOwned,
}
impl<'a> From<nom::Err<ParseError<'a>>> for Error {
fn from(v: nom::Err<ParseError<'a>>) -> Self {
Error {
inner: match v {
nom::Err::Error(err) | nom::Err::Failure(err) => nom::error::VerboseError {
errors: err
.errors
.into_iter()
.map(|(i, v)| (i.as_bstr().to_owned(), v))
.collect(),
},
nom::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for Error {}
}
#[cfg(not(feature = "verbose-object-parsing-errors"))]
pub mod decode {
pub type ParseError<'a> = ();
pub type ParseErrorOwned = ();
#[derive(Debug, Clone)]
pub struct Error {
pub inner: ParseErrorOwned,
}
impl<'a> From<nom::Err<ParseError<'a>>> for Error {
fn from(v: nom::Err<ParseError<'a>>) -> Self {
Error {
inner: match v {
nom::Err::Error(err) | nom::Err::Failure(err) => err,
nom::Err::Incomplete(_) => unreachable!("we don't have streaming parsers"),
},
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
impl std::error::Error for Error {}
}