1+ use crate :: cli:: FilePriority ;
12use crate :: client:: TransmissionClient ;
23use crate :: error:: Error ;
34use crate :: output:: { json, table} ;
@@ -14,13 +15,74 @@ pub fn execute_info(client: &TransmissionClient, id: i64, json_output: bool) ->
1415 }
1516}
1617
17- pub fn execute_files ( client : & TransmissionClient , id : i64 , json_output : bool ) -> Result < ( ) , Error > {
18- let ( name, files) = methods:: torrent_get_files ( client, id) ?;
18+ #[ allow( clippy:: too_many_arguments) ]
19+ pub fn execute_files (
20+ client : & TransmissionClient ,
21+ id : i64 ,
22+ priority : Option < & FilePriority > ,
23+ priority_indices : Option < & [ usize ] > ,
24+ skip : Option < & [ usize ] > ,
25+ unskip : Option < & [ usize ] > ,
26+ json_output : bool ,
27+ ) -> Result < ( ) , Error > {
28+ let has_mutation = priority. is_some ( ) || skip. is_some ( ) || unskip. is_some ( ) ;
29+
30+ if has_mutation {
31+ // Pre-fetch to validate indices
32+ let ( _, files, _) = methods:: torrent_get_files ( client, id) ?;
33+ let file_count = files. len ( ) ;
34+
35+ // Validate all indices
36+ let all_indices: Vec < usize > = priority_indices
37+ . into_iter ( )
38+ . flatten ( )
39+ . chain ( skip. into_iter ( ) . flatten ( ) )
40+ . chain ( unskip. into_iter ( ) . flatten ( ) )
41+ . copied ( )
42+ . collect ( ) ;
43+
44+ for & idx in & all_indices {
45+ if idx >= file_count {
46+ return Err ( Error :: Config ( format ! (
47+ "File index {idx} out of range (torrent has {file_count} files, indices 0-{})" ,
48+ file_count. saturating_sub( 1 )
49+ ) ) ) ;
50+ }
51+ }
52+
53+ // Build priority arrays
54+ let ( p_high, p_normal, p_low) = match priority {
55+ Some ( FilePriority :: High ) => ( priority_indices, None , None ) ,
56+ Some ( FilePriority :: Normal ) => ( None , priority_indices, None ) ,
57+ Some ( FilePriority :: Low ) => ( None , None , priority_indices) ,
58+ None => ( None , None , None ) ,
59+ } ;
60+
61+ methods:: torrent_set_file_properties ( client, id, p_high, p_normal, p_low, unskip, skip) ?;
62+ }
63+
64+ // Fetch and display
65+ let ( name, files, stats) = methods:: torrent_get_files ( client, id) ?;
1966
2067 if json_output {
21- json:: print_json ( & files)
68+ let json_files: Vec < serde_json:: Value > = files
69+ . iter ( )
70+ . enumerate ( )
71+ . map ( |( i, f) | {
72+ let stat = stats. get ( i) ;
73+ serde_json:: json!( {
74+ "index" : i,
75+ "name" : f. name,
76+ "length" : f. length,
77+ "bytesCompleted" : f. bytes_completed,
78+ "priority" : table:: priority_string( stat. map( |s| s. priority) . unwrap_or( 0 ) ) ,
79+ "wanted" : stat. map( |s| s. wanted) . unwrap_or( true ) ,
80+ } )
81+ } )
82+ . collect ( ) ;
83+ json:: print_json ( & json_files)
2284 } else {
23- table:: print_torrent_files ( & name, & files) ;
85+ table:: print_torrent_files ( & name, & files, & stats ) ;
2486 Ok ( ( ) )
2587 }
2688}
0 commit comments