Skip to content

Conversation

@coreyphillips
Copy link

@coreyphillips coreyphillips commented Nov 11, 2025

This PR:

  • Adds WordCount enum to restrict word counts to valid BIP39 values (12, 15, 18, 21 & 24)
  • Updates generate(), generate_in(), and generate_in_with() to accept WordCount instead of usize
  • Updates usage in comments and tests accordingly
  • Closes Introduce enum WordCount #97
  • ⚠️ This is a breaking change
    • generate() now requires use of the WordCount enum for the word_count parameter.
    • Migration:
      // Before
      let m = Mnemonic::generate(24)?;
      
      // After
      let m = Mnemonic::generate(WordCount::Words24)?;

- Add WordCount enum to restrict word counts to valid BIP39 values (12, 15, 18, 21 & 24)
- Update generate(), generate_in(), and generate_in_with() to accept WordCount instead of usize
- Update usage in comments and tests accordingly
Copy link
Contributor

@caarloshenriq caarloshenriq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

I tested the changes locally:

  • Ran the full test suite (cargo test) and all tests passed.
  • Performed manual tests by generating mnemonics with different WordCount variants (12, 18, and 24 words) using generate, generate_in, and generate_in_with.
  • All generated mnemonics had the correct word count and the generation flow behaved as expected.

Everything worked as intended.

Copy link
Collaborator

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this! While it's a nice API improvement, it's probably not super critical so I think we want to hold off on merging this until we have another, more critical change that would require breaking API anyways.

In the meantime, here are a few comments. Please also always disclose in the PR description and commit message if you utilized AI tooling. To this end, rewriting the PR description and commit messages to replace AI slop with concise descriptions would be appreciated. Thanks!


impl WordCount {
/// Returns the word count as a usize value.
pub fn word_count(&self) -> usize {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a impl From<WordCount> for usize?

R: RngCore + CryptoRng,
{
let word_count = word_count.word_count();
if is_invalid_word_count(word_count) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this check now.


let dbg = format!("{:?}", m);

assert!(dbg.starts_with("Mnemonic {") || dbg.starts_with("Mnemonic(") || dbg.contains("Mnemonic"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes here seem unrelated? Please drop them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce enum WordCount

3 participants