diff --git a/bip0039/Cargo.toml b/bip0039/Cargo.toml index 9913f01..0e585e3 100644 --- a/bip0039/Cargo.toml +++ b/bip0039/Cargo.toml @@ -65,7 +65,7 @@ spanish = [] [dependencies] hmac = { version = "0.13", default-features = false } pbkdf2 = { version = "0.13", default-features = false } -phf = { version = "0.13", default-features = false } +phf = { version = "0.13", default-features = false, features = ["macros"] } rand = { version = "0.10", optional = true } sha2 = { version = "0.11", default-features = false } unicode-normalization = { version = "0.1", default-features = false } @@ -73,7 +73,6 @@ zeroize = { version = "1.8", default-features = false, features = ["alloc"] } [build-dependencies] anyhow = "1.0" -phf_codegen = "0.13" [dev-dependencies] const-hex = "1.17" diff --git a/bip0039/build.rs b/bip0039/build.rs index 19b7c26..6279faa 100644 --- a/bip0039/build.rs +++ b/bip0039/build.rs @@ -1,4 +1,4 @@ -//! Build script to generate BIP-0039 wordlists and PHF indices from `words/*.txt`. +//! Build script to generate BIP-0039 wordlists and PHF macro input from `words/*.txt`. //! //! This script generates one Rust source file per language into `OUT_DIR`: //! - `bip0039_wordlist_.rs` @@ -77,18 +77,6 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> { words.len() ); - // Create PHF map (word -> index). - // - // `phf_codegen::Map::entry` stores borrowed `&str` values internally while we build it. - // So we must ensure backing strings live long enough and are not moved/reallocated - // during insertion. We do that by precomputing all value strings first, then inserting. - let index_values: Vec = (0..words.len()).map(|i| format!("{i}u16")).collect(); - - let mut index_map = phf_codegen::Map::new(); - for (w, v) in words.iter().zip(index_values.iter()) { - index_map.entry(w, v.as_str()); - } - let out_name = format!("bip0039_wordlist_{lang}.rs"); let out_path = out_dir.join(&out_name); @@ -113,8 +101,12 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> { writeln!(f, "];\n").context("failed writing WORDS footer")?; // INDEX - writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = {};\n", index_map.build()) - .context("failed writing INDEX")?; + writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = ::phf::phf_map! {{") + .context("failed writing INDEX header")?; + for (i, w) in words.iter().enumerate() { + writeln!(f, " {:?} => {i}u16,", w).context("failed writing an INDEX entry")?; + } + writeln!(f, "}};\n").context("failed writing INDEX footer")?; // WORDLIST writeln!(