Skip to content

Commit

Permalink
Switch from failure to anyhow+thiserror (#24)
Browse files Browse the repository at this point in the history
* Switch from failure to anyhow+thiserror

* Missing NL at EOF
  • Loading branch information
maciejhirsz authored Nov 9, 2020
1 parent 780e3a4 commit e410aca
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ spanish = []
default = ["chinese-simplified", "chinese-traditional", "french", "italian", "japanese", "korean", "spanish"]

[dependencies]
failure = "0.1.8"
anyhow = "1.0.34"
thiserror = "1.0.22"
rustc-hash = "1.1.0"
sha2 = "0.9.1"
hmac = "0.8.1"
Expand Down
37 changes: 28 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
use crate::mnemonic_type::MnemonicType;
use thiserror::Error;

#[derive(Debug, Fail)]
#[derive(Debug, Error)]
pub enum ErrorKind {
#[fail(display = "invalid checksum")]
#[error("invalid checksum")]
InvalidChecksum,
#[fail(display = "invalid word in phrase")]
#[error("invalid word in phrase")]
InvalidWord,
#[fail(display = "invalid keysize: {}", _0)]
#[error("invalid keysize: {0}")]
InvalidKeysize(usize),
#[fail(display = "invalid number of words in phrase: {}", _0)]
#[error("invalid number of words in phrase: {0}")]
InvalidWordLength(usize),
#[fail(
display = "invalid entropy length {}bits for mnemonic type {:?}",
_0, _1
)]
#[error("invalid entropy length {0}bits for mnemonic type {1:?}")]
InvalidEntropyLength(usize, MnemonicType),
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn prints_correctly() {
assert_eq!(
format!("{}", ErrorKind::InvalidChecksum),
"invalid checksum",
);
assert_eq!(
format!("{}", ErrorKind::InvalidKeysize(42)),
"invalid keysize: 42",
);
assert_eq!(
format!("{}", ErrorKind::InvalidEntropyLength(42, MnemonicType::Words12)),
"invalid entropy length 42bits for mnemonic type Words12",
);
}
}
3 changes: 1 addition & 2 deletions src/language.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::error::ErrorKind;
use crate::util::{Bits, Bits11};
use failure::Error;
use rustc_hash::FxHashMap;
use zeroize::Zeroize;

Expand All @@ -13,7 +12,7 @@ pub struct WordList {
}

impl WordMap {
pub fn get_bits(&self, word: &str) -> Result<Bits11, Error> {
pub fn get_bits(&self, word: &str) -> Result<Bits11, ErrorKind> {
match self.inner.get(word) {
Some(n) => Ok(*n),
None => Err(ErrorKind::InvalidWord)?,
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
//! println!("{:X}", seed);
//! ```
//!
#[macro_use]
extern crate failure;

mod error;
mod language;
mod mnemonic;
Expand Down
2 changes: 1 addition & 1 deletion src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use failure::Error;
use anyhow::Error;
use std::mem;
use unicode_normalization::UnicodeNormalization;
use zeroize::Zeroize;
Expand Down
2 changes: 1 addition & 1 deletion src/mnemonic_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use failure::Error;
use anyhow::Error;
use crate::error::ErrorKind;

const ENTROPY_OFFSET: usize = 8;
Expand Down

0 comments on commit e410aca

Please sign in to comment.