Skip to content

Commit 56b66ac

Browse files
committed
refactor
1 parent afae684 commit 56b66ac

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

git-object/src/borrowed/object.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
mod error;
2-
pub use error::Error;
1+
use bstr::BStr;
32

4-
use crate::BStr;
53
use crate::{
64
borrowed,
75
borrowed::{parse, Blob, Commit, Tag, Tree},
8-
Time,
6+
Kind, Time,
97
};
108

9+
mod error;
10+
pub use error::Error;
11+
1112
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
1213
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
1314
pub struct Signature<'a> {
@@ -33,6 +34,18 @@ pub enum Object<'a> {
3334
Blob(Blob<'a>),
3435
}
3536

37+
impl<'a> Object<'a> {
38+
pub fn from_bytes(kind: Kind, bytes: &'a [u8]) -> Result<Object<'a>, Error> {
39+
Ok(match kind {
40+
Kind::Tag => Object::Tag(Tag::from_bytes(bytes)?),
41+
Kind::Tree => Object::Tree(Tree::from_bytes(bytes)?),
42+
Kind::Commit => Object::Commit(Commit::from_bytes(bytes)?),
43+
Kind::Blob => Object::Blob(Blob { data: bytes }),
44+
})
45+
}
46+
}
47+
48+
/// Convenient access to contained objects
3649
impl<'a> Object<'a> {
3750
pub fn as_blob(&self) -> Option<&borrowed::Blob> {
3851
match self {
@@ -58,12 +71,12 @@ impl<'a> Object<'a> {
5871
_ => None,
5972
}
6073
}
61-
pub fn kind(&self) -> crate::Kind {
74+
pub fn kind(&self) -> Kind {
6275
match self {
63-
Object::Tag(_) => crate::Kind::Tag,
64-
Object::Commit(_) => crate::Kind::Commit,
65-
Object::Tree(_) => crate::Kind::Tree,
66-
Object::Blob(_) => crate::Kind::Blob,
76+
Object::Tag(_) => Kind::Tag,
77+
Object::Commit(_) => Kind::Commit,
78+
Object::Tree(_) => Kind::Tree,
79+
Object::Blob(_) => Kind::Blob,
6780
}
6881
}
6982
}

git-odb/src/loose/object/decode.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ quick_error! {
1515
from()
1616
cause(err)
1717
}
18-
ParseTag(err: borrowed::Error) {
19-
display("Could not parse tag object")
18+
Parse(err: borrowed::Error) {
19+
display("Could not parse object object")
2020
from()
2121
cause(err)
2222
}
@@ -33,12 +33,7 @@ impl loose::Object {
3333
pub fn decode(&mut self) -> Result<borrowed::Object, Error> {
3434
self.decompress_all()?;
3535
let bytes = &self.decompressed_data[self.header_size..];
36-
Ok(match self.kind {
37-
object::Kind::Tag => borrowed::Object::Tag(borrowed::Tag::from_bytes(bytes)?),
38-
object::Kind::Tree => borrowed::Object::Tree(borrowed::Tree::from_bytes(bytes)?),
39-
object::Kind::Commit => borrowed::Object::Commit(borrowed::Commit::from_bytes(bytes)?),
40-
object::Kind::Blob => borrowed::Object::Blob(borrowed::Blob { data: bytes }),
41-
})
36+
Ok(borrowed::Object::from_bytes(self.kind, bytes)?)
4237
}
4338

4439
pub fn stream(&self) -> Result<stream::Reader, Error> {

0 commit comments

Comments
 (0)