Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose words and add Display to Error. #10

Merged
merged 3 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Parity Brain Wallets wordlist library


[Rust Documentation](https://docs.rs/parity-wordlist/1.1.0/parity_wordlist/)
[Rust Documentation](https://docs.rs/parity-wordlist/)


# RUST
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ extern crate lazy_static;
extern crate itertools;
extern crate rand;

use std::fmt;
use std::collections::HashSet;
use itertools::Itertools;
use rand::{Rng, OsRng};

/// The list of dictionary words.
// the wordlist JSON also happens to be valid Rust syntax for an array constant.
const WORDS: &'static [&'static str] = &include!("../res/wordlist.json");
pub const WORDS: &'static [&'static str] = &include!("../res/wordlist.json");

/// Generate a string which is a random phrase of a number of lowercase words.
///
Expand All @@ -50,6 +52,15 @@ pub enum Error {
WordNotFromDictionary(String),
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::PhraseTooShort(len) => writeln!(fmt, "The phrase is too short ({})", len),
Error::WordNotFromDictionary(ref word) => writeln!(fmt, "The word '{}' does not come from the dictionary.", word),
}
}
}

/// Validates given phrase and checks if:
/// 1. All the words are coming from the dictionary.
/// 2. There are at least `expected_no_of_words` in the phrase.
Expand Down