Implement custom Debug for Sha256dChecksum

Relates to #63
This commit is contained in:
Deirdre Connolly 2019-10-15 18:59:55 -04:00 committed by Henry de Valence
parent 539a16979b
commit 65d988471a
2 changed files with 21 additions and 2 deletions

View File

@ -161,7 +161,7 @@ mod tests {
use crate::sha256d_writer::Sha256dWriter;
#[test]
fn test_blockheaderhash_debug() {
fn blockheaderhash_debug() {
let preimage = b"foo bar baz";
let mut sha_writer = Sha256dWriter::default();
let _ = sha_writer.write_all(preimage);

View File

@ -1,7 +1,10 @@
//! Newtype wrappers for primitive data types with semantic meaning.
use hex;
use std::fmt;
/// A 4-byte checksum using truncated double-SHA256 (two rounds of SHA256).
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Sha256dChecksum(pub [u8; 4]);
impl<'a> From<&'a [u8]> for Sha256dChecksum {
@ -15,6 +18,14 @@ impl<'a> From<&'a [u8]> for Sha256dChecksum {
}
}
impl fmt::Debug for Sha256dChecksum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Sha256dChecksum")
.field(&hex::encode(&self.0))
.finish()
}
}
/// A u32 which represents a block height value.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct BlockHeight(pub u32);
@ -31,4 +42,12 @@ mod tests {
let expected = Sha256dChecksum([0x95, 0x95, 0xc9, 0xdf]);
assert_eq!(checksum, expected);
}
#[test]
fn sha256d_checksum_debug() {
let input = b"hello";
let checksum = Sha256dChecksum::from(&input[..]);
assert_eq!(format!("{:?}", checksum), "Sha256dChecksum(\"9595c9df\")");
}
}