summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2024-02-15 20:05:47 -0330
committerTim Oram <dev@mitmaro.ca>2024-02-15 20:27:06 -0330
commitd19563ff24c14108f3e14c3b7712437c86339731 (patch)
treee7afddc294bf9b10d381dff36970da413e6693f0
parent810b018339c14513072b2ff889ee9b737bf5e4ab (diff)
Cleanup Reference
-rw-r--r--src/git/reference.rs32
-rw-r--r--src/test_helpers/builders/reference.rs25
2 files changed, 34 insertions, 23 deletions
diff --git a/src/git/reference.rs b/src/git/reference.rs
index dd44cbb..3895f52 100644
--- a/src/git/reference.rs
+++ b/src/git/reference.rs
@@ -4,41 +4,56 @@ use crate::git::ReferenceKind;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Reference {
/// The object id
- pub(crate) hash: String,
+ hash: String,
/// The reference full name
- pub(crate) name: String,
+ name: String,
/// The reference shorthand name
- pub(crate) shorthand: String,
+ shorthand: String,
/// The kind of reference
- pub(crate) kind: ReferenceKind,
+ kind: ReferenceKind,
}
impl Reference {
+ pub(crate) fn new(hash: String, name: String, shorthand: String, kind: ReferenceKind) -> Self {
+ Self {
+ hash,
+ name,
+ shorthand,
+ kind,
+ }
+ }
+
/// Get the oid of the reference
#[must_use]
+ #[allow(dead_code)]
pub(crate) fn hash(&self) -> &str {
self.hash.as_str()
}
/// Get the name of the reference
#[must_use]
+ #[allow(dead_code)]
pub(crate) fn name(&self) -> &str {
self.name.as_str()
}
/// Get the shorthand name of the reference
#[must_use]
+ #[allow(dead_code)]
pub(crate) fn shortname(&self) -> &str {
self.shorthand.as_str()
}
/// Get the kind of the reference
#[must_use]
+ #[allow(dead_code)]
pub(crate) const fn kind(&self) -> ReferenceKind {
self.kind
}
+}
- pub(crate) fn from(reference: &git2::Reference<'_>) -> Self {
+impl From<&git2::Reference<'_>> for Reference {
+ fn from(reference: &git2::Reference<'_>) -> Self {
let oid = reference
.peel(git2::ObjectType::Any)
.expect("Reference peel failed")
@@ -47,12 +62,7 @@ impl Reference {
let name = String::from(reference.name().unwrap_or("InvalidRef"));
let shorthand = String::from(reference.shorthand().unwrap_or("InvalidRef"));
- Self {
- hash: format!("{oid}"),
- name,
- shorthand,
- kind,
- }
+ Self::new(format!("{oid}"), name, shorthand, kind)
}
}
diff --git a/src/test_helpers/builders/reference.rs b/src/test_helpers/builders/reference.rs
index 58ba2e9..b7cb33e 100644
--- a/src/test_helpers/builders/reference.rs
+++ b/src/test_helpers/builders/reference.rs
@@ -3,7 +3,10 @@ use crate::git::{Reference, ReferenceKind};
/// Builder for creating a new reference.
#[derive(Debug)]
pub(crate) struct ReferenceBuilder {
- reference: Reference,
+ hash: String,
+ name: String,
+ shorthand: String,
+ kind: ReferenceKind,
}
impl ReferenceBuilder {
@@ -12,36 +15,34 @@ impl ReferenceBuilder {
#[must_use]
pub(crate) fn new(hash: &str) -> Self {
Self {
- reference: Reference {
- hash: String::from(hash),
- name: String::from("refs/heads/main"),
- shorthand: String::from("main"),
- kind: ReferenceKind::Branch,
- },
+ hash: String::from(hash),
+ name: String::from("refs/heads/main"),
+ shorthand: String::from("main"),
+ kind: ReferenceKind::Branch,
}
}
/// Set the hash.
pub(crate) fn hash(&mut self, hash: &str) -> &mut Self {
- self.reference.hash = String::from(hash);
+ self.hash = String::from(hash);
self
}
/// Set the name.
pub(crate) fn name(&mut self, name: &str) -> &mut Self {
- self.reference.name = String::from(name);
+ self.name = String::from(name);
self
}
/// Set the shortname.
pub(crate) fn shorthand(&mut self, shorthand: &str) -> &mut Self {
- self.reference.shorthand = String::from(shorthand);
+ self.shorthand = String::from(shorthand);
self
}
/// Set the kind.
pub(crate) fn kind(&mut self, kind: ReferenceKind) -> &mut Self {
- self.reference.kind = kind;
+ self.kind = kind;
self
}
@@ -49,6 +50,6 @@ impl ReferenceBuilder {
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub(crate) fn build(self) -> Reference {
- self.reference
+ Reference::new(self.hash, self.name, self.shorthand, self.kind)
}
}