Skip to content

Commit

Permalink
Remove anyhow (#46)
Browse files Browse the repository at this point in the history
Resolves #27.

Not only should anyhow generally not be used in libraries, it adds no value
here (it arguably makes error handling from this library worse actually).

Bumps version to 2.0 as this is a breaking API change :/

Co-authored-by: Maciej Hirsz <[email protected]>
  • Loading branch information
kayabaNerve and maciejhirsz authored Oct 7, 2024
1 parent 2175c62 commit a2e3459
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiny-bip39"
version = "1.0.1"
version = "2.0.0"
authors = [
"Stephen Oliver <[email protected]>",
"Maciej Hirsz <[email protected]>",
Expand Down Expand Up @@ -32,7 +32,6 @@ default-langs = ["chinese-simplified", "chinese-traditional", "french", "italian
default = ["default-langs", "rand"]

[dependencies]
anyhow = "1.0.57"
thiserror = "1.0.31"
rustc-hash = "1.1.0"
sha2 = "0.10.2"
Expand Down
9 changes: 4 additions & 5 deletions src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::error::ErrorKind;
use crate::language::Language;
use crate::mnemonic_type::MnemonicType;
use crate::util::{checksum, BitWriter, IterExt};
use anyhow::Error;
use std::fmt;
use std::mem;
use unicode_normalization::UnicodeNormalization;
Expand Down Expand Up @@ -85,7 +84,7 @@ impl Mnemonic {
/// ```
///
/// [Mnemonic]: ../mnemonic/struct.Mnemonic.html
pub fn from_entropy(entropy: &[u8], lang: Language) -> Result<Mnemonic, Error> {
pub fn from_entropy(entropy: &[u8], lang: Language) -> Result<Mnemonic, ErrorKind> {
// Validate entropy size
MnemonicType::for_key_size(entropy.len() * 8)?;

Expand Down Expand Up @@ -143,7 +142,7 @@ impl Mnemonic {
/// ```
///
/// [Mnemonic]: ../mnemonic/struct.Mnemonic.html
pub fn from_phrase(phrase: &str, lang: Language) -> Result<Mnemonic, Error> {
pub fn from_phrase(phrase: &str, lang: Language) -> Result<Mnemonic, ErrorKind> {
let phrase = Zeroizing::new(
phrase
.split_whitespace()
Expand Down Expand Up @@ -179,7 +178,7 @@ impl Mnemonic {
///
/// assert!(Mnemonic::validate(test_mnemonic, Language::English).is_ok());
/// ```
pub fn validate(phrase: &str, lang: Language) -> Result<(), Error> {
pub fn validate(phrase: &str, lang: Language) -> Result<(), ErrorKind> {
Mnemonic::phrase_to_entropy(phrase, lang)?;

Ok(())
Expand All @@ -190,7 +189,7 @@ impl Mnemonic {
/// Only intended for internal use, as returning a `Vec<u8>` that looks a bit like it could be
/// used as the seed is likely to cause problems for someone eventually. All the other functions
/// that return something like that are explicit about what it is and what to use it for.
fn phrase_to_entropy(phrase: &str, lang: Language) -> Result<Vec<u8>, Error> {
fn phrase_to_entropy(phrase: &str, lang: Language) -> Result<Vec<u8>, ErrorKind> {
let wordmap = lang.wordmap();

// Preallocate enough space for the longest possible word list
Expand Down
7 changes: 3 additions & 4 deletions src/mnemonic_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt;
use anyhow::Error;
use crate::error::ErrorKind;

const ENTROPY_OFFSET: usize = 8;
Expand Down Expand Up @@ -51,7 +50,7 @@ impl MnemonicType {
///
/// let mnemonic_type = MnemonicType::for_word_count(12).unwrap();
/// ```
pub fn for_word_count(size: usize) -> Result<MnemonicType, Error> {
pub fn for_word_count(size: usize) -> Result<MnemonicType, ErrorKind> {
let mnemonic_type = match size {
12 => MnemonicType::Words12,
15 => MnemonicType::Words15,
Expand All @@ -75,7 +74,7 @@ impl MnemonicType {
///
/// let mnemonic_type = MnemonicType::for_key_size(128).unwrap();
/// ```
pub fn for_key_size(size: usize) -> Result<MnemonicType, Error> {
pub fn for_key_size(size: usize) -> Result<MnemonicType, ErrorKind> {
let mnemonic_type = match size {
128 => MnemonicType::Words12,
160 => MnemonicType::Words15,
Expand Down Expand Up @@ -109,7 +108,7 @@ impl MnemonicType {
/// ```
///
/// [MnemonicType::entropy_bits()]: ./enum.MnemonicType.html#method.entropy_bits
pub fn for_phrase(phrase: &str) -> Result<MnemonicType, Error> {
pub fn for_phrase(phrase: &str) -> Result<MnemonicType, ErrorKind> {
let word_count = phrase.split(" ").count();

Self::for_word_count(word_count)
Expand Down

0 comments on commit a2e3459

Please sign in to comment.