diff --git a/Cargo.toml b/Cargo.toml index a84faf6..81a313f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/error.rs b/src/error.rs index 07333cc..e88c3e8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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", + ); + } +} diff --git a/src/language.rs b/src/language.rs index 76e738a..1d02e14 100644 --- a/src/language.rs +++ b/src/language.rs @@ -1,6 +1,5 @@ use crate::error::ErrorKind; use crate::util::{Bits, Bits11}; -use failure::Error; use rustc_hash::FxHashMap; use zeroize::Zeroize; @@ -13,7 +12,7 @@ pub struct WordList { } impl WordMap { - pub fn get_bits(&self, word: &str) -> Result { + pub fn get_bits(&self, word: &str) -> Result { match self.inner.get(word) { Some(n) => Ok(*n), None => Err(ErrorKind::InvalidWord)?, diff --git a/src/lib.rs b/src/lib.rs index d660fa9..6b19194 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,9 +27,6 @@ //! println!("{:X}", seed); //! ``` //! -#[macro_use] -extern crate failure; - mod error; mod language; mod mnemonic; diff --git a/src/mnemonic.rs b/src/mnemonic.rs index 9119278..b3ee685 100644 --- a/src/mnemonic.rs +++ b/src/mnemonic.rs @@ -1,5 +1,5 @@ use std::fmt; -use failure::Error; +use anyhow::Error; use std::mem; use unicode_normalization::UnicodeNormalization; use zeroize::Zeroize; diff --git a/src/mnemonic_type.rs b/src/mnemonic_type.rs index 3e76053..0c70b70 100644 --- a/src/mnemonic_type.rs +++ b/src/mnemonic_type.rs @@ -1,5 +1,5 @@ use std::fmt; -use failure::Error; +use anyhow::Error; use crate::error::ErrorKind; const ENTROPY_OFFSET: usize = 8;