Skip to content

Commit d19563f

Browse files
committed
Cleanup Reference
1 parent 810b018 commit d19563f

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/git/reference.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,56 @@ use crate::git::ReferenceKind;
44
#[derive(Debug, Clone, PartialEq, Eq)]
55
pub(crate) struct Reference {
66
/// The object id
7-
pub(crate) hash: String,
7+
hash: String,
88
/// The reference full name
9-
pub(crate) name: String,
9+
name: String,
1010
/// The reference shorthand name
11-
pub(crate) shorthand: String,
11+
shorthand: String,
1212
/// The kind of reference
13-
pub(crate) kind: ReferenceKind,
13+
kind: ReferenceKind,
1414
}
1515

1616
impl Reference {
17+
pub(crate) fn new(hash: String, name: String, shorthand: String, kind: ReferenceKind) -> Self {
18+
Self {
19+
hash,
20+
name,
21+
shorthand,
22+
kind,
23+
}
24+
}
25+
1726
/// Get the oid of the reference
1827
#[must_use]
28+
#[allow(dead_code)]
1929
pub(crate) fn hash(&self) -> &str {
2030
self.hash.as_str()
2131
}
2232

2333
/// Get the name of the reference
2434
#[must_use]
35+
#[allow(dead_code)]
2536
pub(crate) fn name(&self) -> &str {
2637
self.name.as_str()
2738
}
2839

2940
/// Get the shorthand name of the reference
3041
#[must_use]
42+
#[allow(dead_code)]
3143
pub(crate) fn shortname(&self) -> &str {
3244
self.shorthand.as_str()
3345
}
3446

3547
/// Get the kind of the reference
3648
#[must_use]
49+
#[allow(dead_code)]
3750
pub(crate) const fn kind(&self) -> ReferenceKind {
3851
self.kind
3952
}
53+
}
4054

41-
pub(crate) fn from(reference: &git2::Reference<'_>) -> Self {
55+
impl From<&git2::Reference<'_>> for Reference {
56+
fn from(reference: &git2::Reference<'_>) -> Self {
4257
let oid = reference
4358
.peel(git2::ObjectType::Any)
4459
.expect("Reference peel failed")
@@ -47,12 +62,7 @@ impl Reference {
4762
let name = String::from(reference.name().unwrap_or("InvalidRef"));
4863
let shorthand = String::from(reference.shorthand().unwrap_or("InvalidRef"));
4964

50-
Self {
51-
hash: format!("{oid}"),
52-
name,
53-
shorthand,
54-
kind,
55-
}
65+
Self::new(format!("{oid}"), name, shorthand, kind)
5666
}
5767
}
5868

src/test_helpers/builders/reference.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use crate::git::{Reference, ReferenceKind};
33
/// Builder for creating a new reference.
44
#[derive(Debug)]
55
pub(crate) struct ReferenceBuilder {
6-
reference: Reference,
6+
hash: String,
7+
name: String,
8+
shorthand: String,
9+
kind: ReferenceKind,
710
}
811

912
impl ReferenceBuilder {
@@ -12,43 +15,41 @@ impl ReferenceBuilder {
1215
#[must_use]
1316
pub(crate) fn new(hash: &str) -> Self {
1417
Self {
15-
reference: Reference {
16-
hash: String::from(hash),
17-
name: String::from("refs/heads/main"),
18-
shorthand: String::from("main"),
19-
kind: ReferenceKind::Branch,
20-
},
18+
hash: String::from(hash),
19+
name: String::from("refs/heads/main"),
20+
shorthand: String::from("main"),
21+
kind: ReferenceKind::Branch,
2122
}
2223
}
2324

2425
/// Set the hash.
2526
pub(crate) fn hash(&mut self, hash: &str) -> &mut Self {
26-
self.reference.hash = String::from(hash);
27+
self.hash = String::from(hash);
2728
self
2829
}
2930

3031
/// Set the name.
3132
pub(crate) fn name(&mut self, name: &str) -> &mut Self {
32-
self.reference.name = String::from(name);
33+
self.name = String::from(name);
3334
self
3435
}
3536

3637
/// Set the shortname.
3738
pub(crate) fn shorthand(&mut self, shorthand: &str) -> &mut Self {
38-
self.reference.shorthand = String::from(shorthand);
39+
self.shorthand = String::from(shorthand);
3940
self
4041
}
4142

4243
/// Set the kind.
4344
pub(crate) fn kind(&mut self, kind: ReferenceKind) -> &mut Self {
44-
self.reference.kind = kind;
45+
self.kind = kind;
4546
self
4647
}
4748

4849
/// Build the `Reference`.
4950
#[must_use]
5051
#[allow(clippy::missing_const_for_fn)]
5152
pub(crate) fn build(self) -> Reference {
52-
self.reference
53+
Reference::new(self.hash, self.name, self.shorthand, self.kind)
5354
}
5455
}

0 commit comments

Comments
 (0)