Add FromStr for Sha256dHash

The FromStr implementation just delegates to the `from_hex` method for
the actual parsing.
This commit is contained in:
Thomas Eizinger 2018-09-20 11:09:55 +10:00
parent 5adf985eae
commit 50e3a4abf2
1 changed files with 19 additions and 0 deletions

View File

@ -34,6 +34,7 @@ use util::uint::Uint256;
#[cfg(feature="fuzztarget")] use util::sha2::Sha256; #[cfg(feature="fuzztarget")] use util::sha2::Sha256;
#[cfg(not(feature="fuzztarget"))] use crypto::sha2::Sha256; #[cfg(not(feature="fuzztarget"))] use crypto::sha2::Sha256;
use std::str::FromStr;
/// Hex deserialization error /// Hex deserialization error
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
@ -432,6 +433,13 @@ impl fmt::UpperHex for Sha256dHash {
} }
} }
impl FromStr for Sha256dHash {
type Err = HexError;
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
Sha256dHash::from_hex(s)
}
}
/// Any collection of objects for which a merkle root makes sense to calculate /// Any collection of objects for which a merkle root makes sense to calculate
pub trait MerkleRoot { pub trait MerkleRoot {
@ -505,6 +513,17 @@ mod tests {
"56944C5D3F98413EF45CF54545538103CC9F298E0575820AD3591376E2E0F65D"); "56944C5D3F98413EF45CF54545538103CC9F298E0575820AD3591376E2E0F65D");
} }
#[test]
fn sha256d_from_str_parses_from_human_readable_hex() {
let human_readable_hex_tx_id = "56944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d";
let from_hex = Sha256dHash::from_hex(human_readable_hex_tx_id).unwrap();
let from_str = human_readable_hex_tx_id.parse().unwrap();
assert_eq!(from_hex, from_str)
}
#[test] #[test]
fn test_sha256d_data() { fn test_sha256d_data() {
assert_eq!( assert_eq!(