From aa6564c3390e2eded71d039cb50696b63211ff05 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 15 Jul 2019 20:14:25 +0200 Subject: [PATCH 1/2] Create a separate hex::Error type --- src/error.rs | 22 +++++----------------- src/hash160.rs | 4 ++-- src/hex.rs | 28 ++++++++++++++++++++++++---- src/ripemd160.rs | 4 ++-- src/sha1.rs | 4 ++-- src/sha256.rs | 8 ++++---- src/sha256d.rs | 4 ++-- src/sha512.rs | 4 ++-- src/siphash24.rs | 4 ++-- src/std_impls.rs | 7 ++++++- 10 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/error.rs b/src/error.rs index da8d587..2d03329 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,30 +17,18 @@ use core::fmt; -/// Hex decoding error -#[derive(Copy, Clone, PartialEq, Eq)] +/// [bitcoin_hashes] error. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Error { - /// non-hexadecimal character - InvalidChar(u8), - /// purported hex string had odd length - OddLengthString(usize), - /// tried to parse fixed-length hash from a string with the wrong type (expected, got) + /// Tried to create a fixed-length hash from a slice with the wrong size (expected, got). InvalidLength(usize, usize), } -impl fmt::Debug for Error { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::InvalidChar(ch) => write!(f, "invalid hex character {}", ch), - Error::OddLengthString(ell) => write!(f, "odd hex string length {}", ell), - Error::InvalidLength(ell, ell2) => write!(f, "bad hex string length {} (expected {})", ell2, ell), + Error::InvalidLength(ell, ell2) => write!(f, "bad slice length {} (expected {})", ell2, ell), } } } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(self, f) - } -} - diff --git a/src/hash160.rs b/src/hash160.rs index 95c2b7b..c1a73c1 100644 --- a/src/hash160.rs +++ b/src/hash160.rs @@ -38,8 +38,8 @@ serde_impl!(Hash, 20); borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/hex.rs b/src/hex.rs index e325a89..ba7cf7e 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -16,7 +16,28 @@ //! use core::{fmt, str}; -use {Error, Hash}; +use Hash; + +/// Hex decoding error +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum Error { + /// non-hexadecimal character + InvalidChar(u8), + /// purported hex string had odd length + OddLengthString(usize), + /// tried to parse fixed-length hash from a string with the wrong type (expected, got) + InvalidLength(usize, usize), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Error::InvalidChar(ch) => write!(f, "invalid hex character {}", ch), + Error::OddLengthString(ell) => write!(f, "odd hex string length {}", ell), + Error::InvalidLength(ell, ell2) => write!(f, "bad hex string length {} (expected {})", ell2, ell), + } + } +} /// Trait for objects that can be serialized as hex strings #[cfg(any(test, feature = "std"))] @@ -220,10 +241,9 @@ impl_fromhex_array!(512); #[cfg(test)] mod tests { - use core::fmt; + use super::*; - use super::{format_hex, format_hex_reverse, FromHex, ToHex}; - use Error; + use core::fmt; #[test] fn hex_roundtrip() { diff --git a/src/ripemd160.rs b/src/ripemd160.rs index a398250..8d8104b 100644 --- a/src/ripemd160.rs +++ b/src/ripemd160.rs @@ -79,8 +79,8 @@ serde_impl!(Hash, 20); borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/sha1.rs b/src/sha1.rs index 57e7152..9954d4d 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -74,8 +74,8 @@ serde_impl!(Hash, 20); borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/sha256.rs b/src/sha256.rs index c1205db..30c3581 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -68,8 +68,8 @@ impl EngineTrait for HashEngine { pub struct Hash([u8; 32]); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } @@ -184,8 +184,8 @@ impl Midstate { } impl hex::FromHex for Midstate { - fn from_byte_iter(iter: I) -> Result - where I: Iterator> + + fn from_byte_iter(iter: I) -> Result + where I: Iterator> + ExactSizeIterator + DoubleEndedIterator, { diff --git a/src/sha256d.rs b/src/sha256d.rs index 66e2f94..2d3e9d6 100644 --- a/src/sha256d.rs +++ b/src/sha256d.rs @@ -32,8 +32,8 @@ serde_impl!(Hash, 32); borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/sha512.rs b/src/sha512.rs index 22b2fc1..e2f61bd 100644 --- a/src/sha512.rs +++ b/src/sha512.rs @@ -114,8 +114,8 @@ impl hash::Hash for Hash { } impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/siphash24.rs b/src/siphash24.rs index 83bb81b..dca492b 100644 --- a/src/siphash24.rs +++ b/src/siphash24.rs @@ -196,8 +196,8 @@ serde_impl!(Hash, 8); borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = Error; - fn from_str(s: &str) -> Result { + type Err = ::hex::Error; + fn from_str(s: &str) -> Result { ::hex::FromHex::from_hex(s) } } diff --git a/src/std_impls.rs b/src/std_impls.rs index c03f8fb..aa87c30 100644 --- a/src/std_impls.rs +++ b/src/std_impls.rs @@ -18,7 +18,7 @@ use std::{error, io}; -use {sha1, sha256, sha512, ripemd160, siphash24}; +use {hex, sha1, sha256, sha512, ripemd160, siphash24}; use HashEngine; use Error; @@ -27,6 +27,11 @@ impl error::Error for Error { fn description(&self) -> &str { "`std::error::description` is deprecated" } } +impl error::Error for hex::Error { + fn cause(&self) -> Option<&error::Error> { None } + fn description(&self) -> &str { "`std::error::description` is deprecated" } +} + impl io::Write for sha1::HashEngine { fn flush(&mut self) -> io::Result<()> { Ok(()) } From d4894d458416b24b74651f72a07479340e4a4e9e Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Fri, 19 Jul 2019 11:27:45 +0200 Subject: [PATCH 2/2] Bump version to 0.7.0 and add changelog entry --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367320d..9c7fe81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.7.0 - 2019-07-19 + +* Add `hex::Error` type for errors generated by the `hex` module. + # 0.6.0 - 2019-07-10 * Add `no_std` support, rearrange traits to not depend on `io::Write` diff --git a/Cargo.toml b/Cargo.toml index 98ac2de..f550f95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitcoin_hashes" -version = "0.6.0" +version = "0.7.0" authors = ["Andrew Poelstra "] license = "CC0-1.0" description = "Hash functions used by rust-bitcoin which support rustc 1.14.0"