Skip to content

Commit 116b508

Browse files
committed
Add word information to Error::UnknownWord
1 parent 7836707 commit 116b508

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/lib.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub enum Error {
9393
/// Mnemonic has a word count that is not a multiple of 6.
9494
BadWordCount(usize),
9595
/// Mnemonic contains an unknown word.
96-
UnknownWord, //TODO(stevenroose) add back word info
96+
/// Error contains the index of the word.
97+
/// Use `mnemonic.split_whitespace().get(i)` to get the word.
98+
UnknownWord(usize),
9799
/// Entropy was not a multiple of 32 bits or between 128-256n bits in length.
98100
BadEntropyBitCount(usize),
99101
/// The mnemonic has an invalid checksum.
@@ -111,8 +113,8 @@ impl fmt::Display for Error {
111113
Error::BadWordCount(c) => write!(f,
112114
"mnemonic has a word count that is not a multiple of 6: {}", c,
113115
),
114-
Error::UnknownWord => write!(f,
115-
"mnemonic contains an unknown word",
116+
Error::UnknownWord(i) => write!(f,
117+
"mnemonic contains an unknown word (word {})", i,
116118
),
117119
Error::BadEntropyBitCount(c) => write!(f,
118120
"entropy was not between 128-256 bits or not a multiple of 32 bits: {} bits", c,
@@ -245,7 +247,7 @@ impl Mnemonic {
245247
return if lang.find_word(first_word).is_some() {
246248
Ok(lang)
247249
} else {
248-
Err(Error::UnknownWord)//TODO(stevenroose) (first_word.to_owned()))
250+
Err(Error::UnknownWord(0))
249251
};
250252
}
251253

@@ -266,7 +268,7 @@ impl Mnemonic {
266268
// Those were considered above.
267269
possible[i] = !lang.unique_words();
268270
}
269-
for word in words {
271+
for (idx, word) in words.enumerate() {
270272
// Scrap languages that don't have this word.
271273
for (i, lang) in langs.iter().enumerate() {
272274
if possible[i] {
@@ -282,7 +284,7 @@ impl Mnemonic {
282284

283285
// If all languages were eliminated, it's an invalid word.
284286
if nb_possible == 0 {
285-
return Err(Error::UnknownWord);//TODO(stevenroose) (word.to_owned()))
287+
return Err(Error::UnknownWord(idx));
286288
}
287289
}
288290

@@ -306,7 +308,7 @@ impl Mnemonic {
306308
/// Parse a mnemonic in normalized UTF8 in the given language.
307309
pub fn parse_in_normalized(language: Language, s: &str) -> Result<Mnemonic, Error> {
308310
let nb_words = s.split_whitespace().count();
309-
if nb_words < 6 || nb_words % 6 != 0 || nb_words > MAX_NB_WORDS {
311+
if nb_words < MIN_NB_WORDS || nb_words % 6 != 0 || nb_words > MAX_NB_WORDS {
310312
return Err(Error::BadWordCount(nb_words));
311313
}
312314

@@ -318,7 +320,7 @@ impl Mnemonic {
318320
let mut bits = [false; MAX_NB_WORDS * 11];
319321

320322
for (i, word) in s.split_whitespace().enumerate() {
321-
let idx = language.find_word(word).ok_or(Error::UnknownWord)?;
323+
let idx = language.find_word(word).ok_or(Error::UnknownWord(i))?;
322324

323325
words[i] = language.word_list()[idx];
324326

@@ -680,7 +682,14 @@ mod tests {
680682
Mnemonic::parse_normalized(
681683
"getter advice cage absurd amount doctor acoustic avoid letter advice cage above",
682684
),
683-
Err(Error::UnknownWord)//("getter".to_owned()))
685+
Err(Error::UnknownWord(0))
686+
);
687+
688+
assert_eq!(
689+
Mnemonic::parse_normalized(
690+
"letter advice cagex absurd amount doctor acoustic avoid letter advice cage above",
691+
),
692+
Err(Error::UnknownWord(2))
684693
);
685694

686695
assert_eq!(

0 commit comments

Comments
 (0)