@@ -13,31 +13,53 @@ use inquire::{Confirm, Select, Text};
13
13
use crate :: {
14
14
compression:: { decompress, CompressionFormat } ,
15
15
hash:: { parse_hash_input, FileHashInfo , HashAlg , Hashing } ,
16
- hashfile:: find_hash ,
16
+ hashfile:: { find_hash_in_standard_files , find_hash_in_user_file } ,
17
17
ui:: cli:: { BurnArgs , HashArg , HashOf } ,
18
18
} ;
19
19
20
20
#[ tracing:: instrument( skip_all, fields( cf) ) ]
21
21
pub fn ask_hash ( args : & BurnArgs , cf : CompressionFormat ) -> anyhow:: Result < Option < FileHashInfo > > {
22
- let hash_params = match & args. hash {
23
- HashArg :: Skip => None ,
24
- HashArg :: Ask => {
25
- match find_hash ( & args. input ) {
26
- Some ( ( alg, expected_hashfile, expected_hash) )
27
- if Confirm :: new ( & format ! (
28
- "Detected hash file {expected_hashfile} in the directory. Do you want to use it?"
29
- ) )
30
- . with_default ( true )
31
- . prompt ( ) ? =>
22
+ let hash_params = match ( & args. hash , & args. hash_file ) {
23
+ ( _, Some ( hash_file) ) => {
24
+ let Some ( ( algs, _, expected_hash) ) = find_hash_in_user_file ( & args. input , hash_file)
25
+ else {
26
+ eprintln ! (
27
+ "Could not parse {} as a valid hash file!" ,
28
+ hash_file. to_string_lossy( )
29
+ ) ;
30
+ exit ( -1 ) ;
31
+ } ;
32
+
33
+ eprintln ! (
34
+ "Using user-provided hash file: {}" ,
35
+ hash_file. to_string_lossy( )
36
+ ) ;
37
+ Some ( BeginHashParams {
38
+ expected_hash,
39
+ alg : ask_alg ( & algs) ?,
40
+ hasher_compression : ask_hasher_compression ( cf, args. hash_of ) ?,
41
+ } )
42
+ }
43
+ ( HashArg :: Skip , _) => None ,
44
+ ( HashArg :: Ask , _) => {
45
+ match find_hash_in_standard_files ( & args. input ) {
46
+ Some ( ( algs, expected_hashfile, expected_hash) )
47
+ if Confirm :: new ( & format ! (
48
+ "Detected hash file {expected_hashfile} in the directory. Do you want to use it?"
49
+ ) )
50
+ . with_default ( true )
51
+ . prompt ( ) ? =>
52
+ {
32
53
Some ( BeginHashParams {
33
54
expected_hash,
34
- alg,
55
+ alg : ask_alg ( & algs ) ? ,
35
56
hasher_compression : ask_hasher_compression ( cf, args. hash_of ) ?,
36
- } ) ,
37
- _ => ask_hash_loop ( cf) ?
57
+ } )
58
+ }
59
+ _ => ask_hash_loop ( cf) ?,
38
60
}
39
61
}
40
- HashArg :: Hash { alg, expected_hash } => Some ( BeginHashParams {
62
+ ( HashArg :: Hash { alg, expected_hash } , _ ) => Some ( BeginHashParams {
41
63
expected_hash : expected_hash. clone ( ) ,
42
64
alg : * alg,
43
65
hasher_compression : ask_hasher_compression ( cf, args. hash_of ) ?,
@@ -109,32 +131,37 @@ fn ask_hash_once(cf: CompressionFormat) -> anyhow::Result<BeginHashParams> {
109
131
} ,
110
132
} ;
111
133
112
- let alg = match & algs[ ..] {
134
+ let alg = ask_alg ( & algs) ?;
135
+
136
+ let hasher_compression = ask_hasher_compression ( cf, None ) ?;
137
+
138
+ Ok ( BeginHashParams {
139
+ expected_hash : hash,
140
+ alg,
141
+ hasher_compression,
142
+ } )
143
+ }
144
+
145
+ #[ tracing:: instrument]
146
+ fn ask_alg ( algs : & [ HashAlg ] ) -> anyhow:: Result < HashAlg > {
147
+ match algs {
113
148
& [ ] => {
114
149
eprintln ! ( "Could not detect the hash algorithm from your hash!" ) ;
115
150
Err ( Recoverable :: AskAgain ) ?
116
151
}
117
152
& [ only_alg] => {
118
153
eprintln ! ( "Detected {}" , only_alg) ;
119
- only_alg
154
+ Ok ( only_alg)
120
155
}
121
156
multiple => {
122
157
let ans = Select :: new ( "Which algorithm is it?" , multiple. into ( ) ) . prompt_skippable ( ) ?;
123
158
if let Some ( alg) = ans {
124
- alg
159
+ Ok ( alg)
125
160
} else {
126
161
Err ( Recoverable :: AskAgain ) ?
127
162
}
128
163
}
129
- } ;
130
-
131
- let hasher_compression = ask_hasher_compression ( cf, None ) ?;
132
-
133
- Ok ( BeginHashParams {
134
- expected_hash : hash,
135
- alg,
136
- hasher_compression,
137
- } )
164
+ }
138
165
}
139
166
140
167
#[ tracing:: instrument]
0 commit comments