Skip to content

Commit

Permalink
Added Error::description impl and c_void as c_uchar
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Apr 14, 2019
1 parent e98975a commit 4a8599b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//! Direct bindings to the underlying C library functions. These should
//! not be needed for most users.
use core::{mem, hash};
use core::ffi::c_void;
use types::*;
// use std::os::raw::{c_int, c_uchar, c_uint, c_void};

Expand Down
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,24 +519,31 @@ pub enum Error {
InvalidTweak,
}

// Passthrough Debug to Display, since errors should be user-visible
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let res = match *self {
impl Error {
fn as_str(&self) -> &str {
match *self {
Error::IncorrectSignature => "secp: signature failed verification",
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
Error::InvalidPublicKey => "secp: malformed public key",
Error::InvalidSignature => "secp: malformed signature",
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
Error::InvalidRecoveryId => "secp: bad recovery id",
Error::InvalidTweak => "secp: bad tweak",
};
f.write_str(res)
}
}
}

// Passthrough Debug to Display, since errors should be user-visible
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(self.as_str())
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl std::error::Error for Error {
fn description(&self) -> &str { self.as_str() }
}

/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
pub trait Signing {}
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
pub type c_int = i32;
pub type c_uchar = u8;
pub type c_uint = u32;
pub use core::ffi::c_void;
pub type c_void = c_uchar;

0 comments on commit 4a8599b

Please sign in to comment.