Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #58 from stevenroose/hex-error
Browse files Browse the repository at this point in the history
Create a separate hex::Error type
  • Loading branch information
apoelstra committed Jul 22, 2019
2 parents 793c506 + d4894d4 commit 64e31de
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 39 deletions.
4 changes: 4 additions & 0 deletions 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`
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bitcoin_hashes"
version = "0.6.0"
version = "0.7.0"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
description = "Hash functions used by rust-bitcoin which support rustc 1.14.0"
Expand Down
22 changes: 5 additions & 17 deletions src/error.rs
Expand Up @@ -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)
}
}

4 changes: 2 additions & 2 deletions src/hash160.rs
Expand Up @@ -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<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/hex.rs
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/ripemd160.rs
Expand Up @@ -80,8 +80,8 @@ serde_impl!(Hash, 20);
borrow_slice_impl!(Hash);

impl str::FromStr for Hash {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/sha1.rs
Expand Up @@ -75,8 +75,8 @@ serde_impl!(Hash, 20);
borrow_slice_impl!(Hash);

impl str::FromStr for Hash {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/sha256.rs
Expand Up @@ -69,8 +69,8 @@ impl EngineTrait for HashEngine {
pub struct Hash([u8; 32]);

impl str::FromStr for Hash {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down Expand Up @@ -177,8 +177,8 @@ impl Midstate {
}

impl hex::FromHex for Midstate {
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
where I: Iterator<Item=Result<u8, Error>> +
fn from_byte_iter<I>(iter: I) -> Result<Self, hex::Error>
where I: Iterator<Item=Result<u8, hex::Error>> +
ExactSizeIterator +
DoubleEndedIterator,
{
Expand Down
4 changes: 2 additions & 2 deletions src/sha256d.rs
Expand Up @@ -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<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/sha512.rs
Expand Up @@ -118,8 +118,8 @@ impl hash::Hash for Hash {
}

impl str::FromStr for Hash {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/siphash24.rs
Expand Up @@ -202,8 +202,8 @@ serde_impl!(Hash, 8);
borrow_slice_impl!(Hash);

impl str::FromStr for Hash {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
type Err = ::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
::hex::FromHex::from_hex(s)
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/std_impls.rs
Expand Up @@ -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;

Expand All @@ -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(()) }

Expand Down

0 comments on commit 64e31de

Please sign in to comment.