1- //! Build script to generate BIP-0039 wordlists and PHF indices from `words/*.txt`.
1+ //! Build script to generate BIP-0039 wordlists and PHF macro input from `words/*.txt`.
22//!
33//! This script generates one Rust source file per language into `OUT_DIR`:
44//! - `bip0039_wordlist_<lang>.rs`
@@ -77,18 +77,6 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
7777 words. len( )
7878 ) ;
7979
80- // Create PHF map (word -> index).
81- //
82- // `phf_codegen::Map::entry` stores borrowed `&str` values internally while we build it.
83- // So we must ensure backing strings live long enough and are not moved/reallocated
84- // during insertion. We do that by precomputing all value strings first, then inserting.
85- let index_values: Vec < String > = ( 0 ..words. len ( ) ) . map ( |i| format ! ( "{i}u16" ) ) . collect ( ) ;
86-
87- let mut index_map = phf_codegen:: Map :: new ( ) ;
88- for ( w, v) in words. iter ( ) . zip ( index_values. iter ( ) ) {
89- index_map. entry ( w, v. as_str ( ) ) ;
90- }
91-
9280 let out_name = format ! ( "bip0039_wordlist_{lang}.rs" ) ;
9381 let out_path = out_dir. join ( & out_name) ;
9482
@@ -113,8 +101,12 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
113101 writeln ! ( f, "];\n " ) . context ( "failed writing WORDS footer" ) ?;
114102
115103 // INDEX
116- writeln ! ( f, "pub static INDEX: ::phf::Map<&'static str, u16> = {};\n " , index_map. build( ) )
117- . context ( "failed writing INDEX" ) ?;
104+ writeln ! ( f, "pub static INDEX: ::phf::Map<&'static str, u16> = ::phf::phf_map! {{" )
105+ . context ( "failed writing INDEX header" ) ?;
106+ for ( i, w) in words. iter ( ) . enumerate ( ) {
107+ writeln ! ( f, " {:?} => {i}u16," , w) . context ( "failed writing an INDEX entry" ) ?;
108+ }
109+ writeln ! ( f, "}};\n " ) . context ( "failed writing INDEX footer" ) ?;
118110
119111 // WORDLIST
120112 writeln ! (
0 commit comments