Skip to content

Commit

Permalink
Consistent style, version bump. (openethereum#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw authored Sep 28, 2017
1 parent 9f8fe9c commit ca22d31
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true

[*.rs]
indent_style=tab
indent_size=tab
tab_width=4
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parity-wordlist"
version = "1.1.0"
version = "1.2.0"
description = "Word list used to generate brain wallets for Parity."
license = "GPL-3.0"
authors = ["Parity Technologies <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Parity Brain Wallets wordlist library
# Cargo.toml

[dependencies]
parity-wordlist = "1.1"
parity-wordlist = "1.2"
```

```rust
Expand Down
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,33 @@ pub fn random_phrase(no_of_words: usize) -> String {
/// Phrase Validation Error
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
/// Phrase is shorter than it was expected.
PhraseTooShort(usize),
/// Phrase contains a word that doesn't come from our dictionary.
WordNotFromDictionary(String),
/// Phrase is shorter than it was expected.
PhraseTooShort(usize),
/// Phrase contains a word that doesn't come from our dictionary.
WordNotFromDictionary(String),
}

/// 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.
pub fn validate_phrase(phrase: &str, expected_no_of_words: usize) -> Result<(), Error> {
lazy_static! {
static ref WORD_SET: HashSet<&'static str> = WORDS.iter().cloned().collect();
}

let mut len = 0;
for word in phrase.split_whitespace() {
len += 1;
if !WORD_SET.contains(word) {
return Err(Error::WordNotFromDictionary(word.into()));
}
}

if len < expected_no_of_words {
return Err(Error::PhraseTooShort(len));
}

return Ok(());
static ref WORD_SET: HashSet<&'static str> = WORDS.iter().cloned().collect();
}

let mut len = 0;
for word in phrase.split_whitespace() {
len += 1;
if !WORD_SET.contains(word) {
return Err(Error::WordNotFromDictionary(word.into()));
}
}

if len < expected_no_of_words {
return Err(Error::PhraseTooShort(len));
}

return Ok(());
}

#[cfg(test)]
Expand All @@ -89,13 +89,13 @@ mod tests {
assert!(!p.contains('\r'), "Carriage return should be trimmed.");
}

#[test]
fn should_validate_the_phrase() {
let p = random_phrase(10);
#[test]
fn should_validate_the_phrase() {
let p = random_phrase(10);

assert_eq!(validate_phrase(&p, 10), Ok(()));
assert_eq!(validate_phrase(&p, 12), Err(Error::PhraseTooShort(10)));
assert_eq!(validate_phrase("xxx", 0), Err(Error::WordNotFromDictionary("xxx".into())));
}
assert_eq!(validate_phrase(&p, 10), Ok(()));
assert_eq!(validate_phrase(&p, 12), Err(Error::PhraseTooShort(10)));
assert_eq!(validate_phrase("xxx", 0), Err(Error::WordNotFromDictionary("xxx".into())));
}
}

0 comments on commit ca22d31

Please sign in to comment.