Skip to content

Commit

Permalink
feat: make GitOid display print URL format (#116)
Browse files Browse the repository at this point in the history
Previously the Display impl for GitOid printed some but not all of the
data associated with the URL representation. This commit changes that to
just print the URL representation, and updates the GitOid::url method
to delegate to `to_string` which delegates to that `Display` impl.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker authored Feb 21, 2024
1 parent d1283a9 commit 78c2b3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
21 changes: 11 additions & 10 deletions gitoid/src/gitoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,9 @@ where

/// Get a URL for the current `GitOid`.
pub fn url(&self) -> Url {
// PANIC SAFETY: We know that this is a valid URL.
Url::parse(&format!(
"{}:{}:{}:{}",
GITOID_URL_SCHEME,
O::NAME,
H::NAME,
self.as_hex()
))
.unwrap()
// PANIC SAFETY: We know that this is a valid URL;
// our `Display` impl is the URL representation.
Url::parse(&self.to_string()).unwrap()
}

/// Get the underlying bytes of the hash.
Expand Down Expand Up @@ -254,7 +248,14 @@ where
O: ObjectType,
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}:{}", H::NAME, self.as_hex())
write!(
f,
"{}:{}:{}:{}",
GITOID_URL_SCHEME,
O::NAME,
H::NAME,
self.as_hex()
)
}
}

Expand Down
10 changes: 5 additions & 5 deletions gitoid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn generate_sha1_gitoid_from_bytes() {

assert_eq!(
result.to_string(),
"sha1:95d09f2b10159347eece71399a7e2e907ea3df4f"
"gitoid:blob:sha1:95d09f2b10159347eece71399a7e2e907ea3df4f"
);
}

Expand All @@ -26,7 +26,7 @@ fn generate_sha1_gitoid_from_buffer() -> Result<()> {

assert_eq!(
result.to_string(),
"sha1:95d09f2b10159347eece71399a7e2e907ea3df4f"
"gitoid:blob:sha1:95d09f2b10159347eece71399a7e2e907ea3df4f"
);

Ok(())
Expand All @@ -44,7 +44,7 @@ fn generate_sha256_gitoid_from_bytes() {

assert_eq!(
result.to_string(),
"sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
"gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
);
}

Expand All @@ -60,7 +60,7 @@ fn generate_sha256_gitoid_from_buffer() -> Result<()> {

assert_eq!(
result.to_string(),
"sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
"gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
);

Ok(())
Expand All @@ -80,7 +80,7 @@ fn generate_sha256_gitoid_from_async_buffer() -> Result<()> {

assert_eq!(
result.to_string(),
"sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
"gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03"
);

Ok(())
Expand Down

0 comments on commit 78c2b3b

Please sign in to comment.