Skip to content

Commit

Permalink
fmt, clippy (#47)
Browse files Browse the repository at this point in the history
* fmt, clippy

* Remove anyhow reference

---------

Co-authored-by: Maciej Hirsz <[email protected]>
  • Loading branch information
kayabaNerve and maciejhirsz authored Oct 7, 2024
1 parent a2e3459 commit 9288d31
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
20 changes: 7 additions & 13 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ impl WordList {
}

pub fn get_words_by_prefix(&self, prefix: &str) -> &[&'static str] {
let start = self.inner
.binary_search(&prefix)
.unwrap_or_else(|idx| idx);
let count = self.inner[start..].iter()
let start = self.inner.binary_search(&prefix).unwrap_or_else(|idx| idx);
let count = self.inner[start..]
.iter()
.take_while(|word| word.starts_with(prefix))
.count();

Expand Down Expand Up @@ -108,8 +107,9 @@ mod lazy {
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
/// [Seed]: ./seed/struct.Seed.html
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Language {
#[default]
English,
#[cfg(feature = "chinese-simplified")]
ChineseSimplified,
Expand Down Expand Up @@ -197,12 +197,6 @@ impl Language {
}
}

impl Default for Language {
fn default() -> Language {
Language::English
}
}

#[cfg(test)]
mod test {
use super::lazy;
Expand All @@ -216,7 +210,7 @@ mod test {
fn words_by_prefix() {
let wl = &lazy::WORDLIST_ENGLISH;
let res = wl.get_words_by_prefix("woo");
assert_eq!(res, ["wood","wool"]);
assert_eq!(res, ["wood", "wool"]);
}

#[cfg_attr(all(target_arch = "wasm32"), wasm_bindgen_test)]
Expand All @@ -242,7 +236,7 @@ mod test {
return false;
}
}
return true;
true
}

#[cfg_attr(all(target_arch = "wasm32"), wasm_bindgen_test)]
Expand Down
5 changes: 2 additions & 3 deletions src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Mnemonic {
///
/// println!("phrase: {}", phrase);
///
/// assert_eq!(phrase.split(" ").count(), 12);
/// assert_eq!(phrase.split(' ').count(), 12);
/// ```
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
Expand Down Expand Up @@ -232,9 +232,8 @@ impl Mnemonic {

/// Consume the `Mnemonic` and return the phrase as a `String`.
pub fn into_phrase(mut self) -> String {
// Create an empty string and swap values with the mnemonic's phrase.
// This allows `Mnemonic` to implement `Drop`, while still returning the phrase.
mem::replace(&mut self.phrase, String::new())
mem::take(&mut self.phrase)
}

/// Get the original entropy value of the mnemonic phrase as a slice.
Expand Down
13 changes: 4 additions & 9 deletions src/mnemonic_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use crate::error::ErrorKind;
use std::fmt;

const ENTROPY_OFFSET: usize = 8;

Expand Down Expand Up @@ -28,9 +28,10 @@ const ENTROPY_OFFSET: usize = 8;
/// [Mnemonic]: ../mnemonic/struct.Mnemonic.html
/// [Seed]: ../seed/struct.Seed.html
///
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Default)]
pub enum MnemonicType {
// ... = (entropy_bits << ...) | checksum_bits
#[default]
Words12 = (128 << ENTROPY_OFFSET) | 4,
Words15 = (160 << ENTROPY_OFFSET) | 5,
Words18 = (192 << ENTROPY_OFFSET) | 6,
Expand Down Expand Up @@ -109,7 +110,7 @@ impl MnemonicType {
///
/// [MnemonicType::entropy_bits()]: ./enum.MnemonicType.html#method.entropy_bits
pub fn for_phrase(phrase: &str) -> Result<MnemonicType, ErrorKind> {
let word_count = phrase.split(" ").count();
let word_count = phrase.split(' ').count();

Self::for_word_count(word_count)
}
Expand Down Expand Up @@ -181,12 +182,6 @@ impl MnemonicType {
}
}

impl Default for MnemonicType {
fn default() -> MnemonicType {
MnemonicType::Words12
}
}

impl fmt::Display for MnemonicType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
Expand Down
11 changes: 8 additions & 3 deletions src/seed.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::crypto::pbkdf2;
use crate::mnemonic::Mnemonic;
use std::fmt;
use unicode_normalization::UnicodeNormalization;
use zeroize::{Zeroize, Zeroizing};
use crate::crypto::pbkdf2;
use crate::mnemonic::Mnemonic;

/// The secret value used to derive HD wallet addresses from a [`Mnemonic`][Mnemonic] phrase.
///
Expand Down Expand Up @@ -108,7 +108,12 @@ mod test {
assert_eq!(format!("{:#X}", seed), "0x0BDE96F14C35A66235478E0C16C152FCAF6301E4D9A81D3FEBC50879FE7E5438E6A8DD3E39BDF3AB7B12D6B44218710E17D7A2844EE9633FAB0E03D9A6C8569B");
}

fn test_unicode_normalization(lang: Language, phrase: &str, password: &str, expected_seed_hex: &str) {
fn test_unicode_normalization(
lang: Language,
phrase: &str,
password: &str,
expected_seed_hex: &str,
) {
let mnemonic = Mnemonic::from_phrase(phrase, lang).unwrap();
let seed = Seed::new(&mnemonic, password);
assert_eq!(format!("{:x}", seed), expected_seed_hex);
Expand Down
4 changes: 2 additions & 2 deletions tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn test_word_count(expected_word_count: usize) {
let mnemonic_type = MnemonicType::for_word_count(expected_word_count).unwrap();

let mnemonic = Mnemonic::new(mnemonic_type, Language::English);
let actual_word_count = mnemonic.phrase().split(" ").count();
let actual_word_count = mnemonic.phrase().split(' ').count();

assert_eq!(actual_word_count, expected_word_count);
assert_eq!(mnemonic_type.word_count(), expected_word_count);
Expand All @@ -24,7 +24,7 @@ macro_rules! test_maybe_wasm {
fn $name() {
$body
}
}
};
}

test_maybe_wasm!(generate_12_english, {
Expand Down

0 comments on commit 9288d31

Please sign in to comment.