use auths_core::witness::EventHash;
use git2::Oid;
pub fn oid_to_event_hash(oid: Oid) -> EventHash {
#[allow(clippy::expect_used)]
let bytes: [u8; 20] = oid.as_bytes().try_into().expect("git2::Oid is 20 bytes");
EventHash::from_bytes(bytes)
}
pub fn event_hash_to_oid(hash: EventHash) -> Oid {
#[allow(clippy::expect_used)]
Oid::from_bytes(hash.as_bytes()).expect("EventHash is 20 bytes")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn oid_to_event_hash_works() {
let oid = Oid::from_str("0123456789abcdef0123456789abcdef01234567").unwrap();
let hash = oid_to_event_hash(oid);
assert_eq!(hash.to_hex(), "0123456789abcdef0123456789abcdef01234567");
}
#[test]
fn event_hash_to_oid_works() {
let hash = EventHash::from_hex("0123456789abcdef0123456789abcdef01234567").unwrap();
let oid = event_hash_to_oid(hash);
assert_eq!(oid.to_string(), "0123456789abcdef0123456789abcdef01234567");
}
#[test]
fn roundtrip_oid_to_hash_to_oid() {
let original = Oid::from_str("fedcba9876543210fedcba9876543210fedcba98").unwrap();
let hash = oid_to_event_hash(original);
let back = event_hash_to_oid(hash);
assert_eq!(original, back);
}
#[test]
fn roundtrip_hash_to_oid_to_hash() {
let original = EventHash::from_hex("abcdef0123456789abcdef0123456789abcdef01").unwrap();
let oid = event_hash_to_oid(original);
let back = oid_to_event_hash(oid);
assert_eq!(original, back);
}
}