Skip to content

Commit 917a762

Browse files
committed
feat: add common interface for hash verification
1 parent 2fb27d2 commit 917a762

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

gix-hash/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub use object_id::{decode, ObjectId};
2424
///
2525
pub mod prefix;
2626

27+
///
28+
pub mod verify;
29+
2730
/// A partial, owned hash possibly identifying an object uniquely, whose non-prefix bytes are zeroed.
2831
///
2932
/// An example would `0000000000000000000000000000000032bd3242`, where `32bd3242` is the prefix,

gix-hash/src/verify.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::{oid, ObjectId};
2+
3+
/// The error returned by [`oid::verify()`].
4+
#[derive(Debug, thiserror::Error)]
5+
#[allow(missing_docs)]
6+
#[error("Hash was {actual}, but should have been {expected}")]
7+
pub struct Error {
8+
actual: ObjectId,
9+
expected: ObjectId,
10+
}
11+
12+
impl oid {
13+
/// Verify that `self` matches the `expected` object ID.
14+
///
15+
/// Returns an [`Error`] containing both object IDs if they differ.
16+
#[inline]
17+
pub fn verify(&self, expected: &oid) -> Result<(), Error> {
18+
if self == expected {
19+
Ok(())
20+
} else {
21+
Err(Error {
22+
actual: self.to_owned(),
23+
expected: expected.to_owned(),
24+
})
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)