11mod args;
22mod dice;
3+ mod dat;
34
45use args:: Args ;
56use dice:: roll;
@@ -14,14 +15,14 @@ fn main() {
1415 password. push_str ( & match result {
1516 Roll4 ( v) => format ! (
1617 "{} " ,
17- short_word( v) . unwrap_or_else( |e| {
18+ short_word( v, & args . wordlist ) . unwrap_or_else( |e| {
1819 eprintln!( "Error getting word: {}" , e) ;
1920 std:: process:: exit( 128 )
2021 } )
2122 ) ,
2223 Roll5 ( v) => format ! (
2324 "{} " ,
24- long_word( v) . unwrap_or_else( |e| {
25+ long_word( v, & args . wordlist ) . unwrap_or_else( |e| {
2526 eprintln!( "Error getting word: {}" , e) ;
2627 std:: process:: exit( 128 )
2728 } )
@@ -42,22 +43,27 @@ fn main() {
4243 )
4344}
4445
45- fn short_word ( dice : [ DiceFace ; 4 ] ) -> std:: io:: Result < String > {
46+ fn short_word ( dice : [ DiceFace ; 4 ] , wordlist : & Option < String > ) -> std:: io:: Result < String > {
4647 let match_str = format ! ( "{}{}{}{}" , dice[ 0 ] , dice[ 1 ] , dice[ 2 ] , dice[ 3 ] ) ;
47- word ( & match_str, "list4.txt" )
48+ word ( & match_str, wordlist , dat :: LIST4 )
4849}
4950
50- fn long_word ( dice : [ DiceFace ; 5 ] ) -> std:: io:: Result < String > {
51+ fn long_word ( dice : [ DiceFace ; 5 ] , wordlist : & Option < String > ) -> std:: io:: Result < String > {
5152 let match_str = format ! ( "{}{}{}{}{}" , dice[ 0 ] , dice[ 1 ] , dice[ 2 ] , dice[ 3 ] , dice[ 4 ] ) ;
52- word ( & match_str, "list5.txt" )
53+ word ( & match_str, wordlist , dat :: LIST5 )
5354}
5455
55- fn word ( match_str : & str , filename : & str ) -> std:: io:: Result < String > {
56+ fn word ( match_str : & str , filename : & Option < String > , fblist : & str ) -> std:: io:: Result < String > {
5657 use std:: io:: BufRead ;
5758 use std:: { fs, io} ;
5859
59- let wordlist = fs:: File :: open ( filename) ?;
60- let wordlist = io:: BufReader :: new ( wordlist) ;
60+ let wordlist : Box < dyn BufRead > ;
61+ if let Some ( f) = filename {
62+ let w = fs:: File :: open ( f) ?;
63+ wordlist = Box :: new ( io:: BufReader :: new ( w) ) ;
64+ } else {
65+ wordlist = Box :: new ( io:: BufReader :: new ( fblist. as_bytes ( ) ) )
66+ }
6167 let mut result = String :: new ( ) ;
6268
6369 for line in wordlist. lines ( ) {
0 commit comments