7
7
8
8
use ab_versions:: { get_version, is_protected, strip_protection} ;
9
9
use clap:: Parser ;
10
+ use log:: error;
10
11
use rayon:: prelude:: { IntoParallelRefIterator , ParallelIterator } ;
11
12
use rfd:: FileDialog ;
13
+ use simplelog:: { CombinedLogger , Config , LevelFilter , SimpleLogger , WriteLogger } ;
12
14
use slint:: { Model , ModelRc , VecModel } ;
13
- use std:: { borrow:: BorrowMut , cell:: RefCell , collections:: HashMap , path:: PathBuf , rc:: Rc } ;
15
+ use std:: {
16
+ borrow:: BorrowMut , cell:: RefCell , collections:: HashMap , fs:: File , path:: PathBuf , rc:: Rc ,
17
+ } ;
14
18
15
19
#[ derive( Parser ) ]
16
20
#[ command( author, version, about, long_about = None ) ]
@@ -21,6 +25,24 @@ struct Args {
21
25
slint:: include_modules!( ) ;
22
26
23
27
fn main ( ) -> Result < ( ) , slint:: PlatformError > {
28
+ CombinedLogger :: init ( vec ! [
29
+ #[ cfg( feature = "termcolor" ) ]
30
+ TermLogger :: new(
31
+ LevelFilter :: Warn ,
32
+ Config :: default ( ) ,
33
+ TerminalMode :: Mixed ,
34
+ ColorChoice :: Auto ,
35
+ ) ,
36
+ #[ cfg( not( feature = "termcolor" ) ) ]
37
+ SimpleLogger :: new( LevelFilter :: Warn , Config :: default ( ) ) ,
38
+ WriteLogger :: new(
39
+ LevelFilter :: Info ,
40
+ Config :: default ( ) ,
41
+ File :: create( "my_rust_binary.log" ) . expect( "Failed to create log file" ) ,
42
+ ) ,
43
+ ] )
44
+ . expect ( "Failed to create logging infrastructure" ) ;
45
+
24
46
let args = Args :: parse_from ( wild:: args ( ) ) ;
25
47
let files = Rc :: new ( RefCell :: new ( process_paths ( args. files ) ) ) ;
26
48
@@ -29,23 +51,38 @@ fn main() -> Result<(), slint::PlatformError> {
29
51
30
52
//if files were passed on the command line process them and add them to the UI model
31
53
let info = get_file_info ( & files. borrow ( ) ) ;
32
- file_model. borrow_mut ( ) . extend ( info. into_iter ( ) ) ;
54
+ file_model. borrow_mut ( ) . extend ( info) ;
33
55
34
56
ui. set_files ( ModelRc :: from ( file_model. clone ( ) ) ) ;
35
57
36
58
let unlock_file_model = file_model. clone ( ) ;
37
59
let unlock_files = files. clone ( ) ;
38
60
ui. on_unlock ( move |file, idx| {
39
61
if let Some ( path) = unlock_files. borrow ( ) . get ( & file. to_string ( ) ) {
40
- strip_protection ( path) . unwrap ( ) ;
41
-
42
- //After attempting to unlock it update the model with the new protected status
43
- //by verifying it in the file on disk
44
- let unlock_file_model = unlock_file_model. as_ref ( ) ;
45
- if let Some ( mut row_data) = unlock_file_model. row_data ( idx as usize ) {
46
- row_data. locked = is_protected ( & path) . unwrap ( ) ;
47
- unlock_file_model. set_row_data ( idx as usize , row_data) ;
48
- } //else...hmm error updating the model?...what to do?
62
+ match strip_protection ( path) {
63
+ Ok ( _) => {
64
+ //After attempting to unlock it update the model with the new protected status
65
+ //by verifying it in the file on disk
66
+ let unlock_file_model = unlock_file_model. as_ref ( ) ;
67
+ if let Some ( mut row_data) = unlock_file_model. row_data ( idx as usize ) {
68
+ match is_protected ( & path) {
69
+ Ok ( lck) => {
70
+ row_data. locked = lck;
71
+ unlock_file_model. set_row_data ( idx as usize , row_data) ;
72
+ }
73
+ Err ( e) => {
74
+ error ! (
75
+ "Unable to confirm file {} was unlocked. Reason: {e}" ,
76
+ path. display( )
77
+ )
78
+ }
79
+ }
80
+ }
81
+ }
82
+ Err ( e) => {
83
+ error ! ( "Failed to unlock file {}. Reason: {e}" , path. display( ) )
84
+ }
85
+ }
49
86
} //else display some sort of toast message with the error?
50
87
} ) ;
51
88
@@ -69,11 +106,15 @@ fn process_paths(files: Vec<PathBuf>) -> HashMap<String, PathBuf> {
69
106
files
70
107
. into_iter ( )
71
108
. map ( |pb| {
72
- let name = pb
73
- . file_name ( )
74
- . map ( std:: ffi:: OsStr :: to_string_lossy)
75
- . unwrap ( )
76
- . to_string ( ) ;
109
+ let name = {
110
+ let tmp_name = pb. file_name ( ) . map ( std:: ffi:: OsStr :: to_string_lossy) ;
111
+
112
+ if let Some ( tmp_name) = tmp_name {
113
+ tmp_name. to_string ( )
114
+ } else {
115
+ pb. display ( ) . to_string ( )
116
+ }
117
+ } ;
77
118
( name, pb)
78
119
} )
79
120
. collect ( )
@@ -82,10 +123,28 @@ fn process_paths(files: Vec<PathBuf>) -> HashMap<String, PathBuf> {
82
123
fn get_file_info ( files : & HashMap < String , PathBuf > ) -> Vec < file_info > {
83
124
files
84
125
. par_iter ( )
85
- . map ( |( name, file) | file_info {
86
- locked : is_protected ( & file) . unwrap ( ) ,
87
- file_name : name. into ( ) ,
88
- file_ver : get_version ( & file) . unwrap ( ) . to_string ( ) . into ( ) ,
126
+ . filter_map ( |( name, file) | match is_protected ( & file) {
127
+ Ok ( lckd) => match get_version ( & file) {
128
+ Ok ( ver) => Some ( file_info {
129
+ locked : lckd,
130
+ file_name : name. into ( ) ,
131
+ file_ver : ver. to_string ( ) . into ( ) ,
132
+ } ) ,
133
+ Err ( e) => {
134
+ error ! (
135
+ "Unable to get file information from {}. Reason: {e}" ,
136
+ file. display( )
137
+ ) ;
138
+ None
139
+ }
140
+ } ,
141
+ Err ( e) => {
142
+ error ! (
143
+ "Unable to get file information from {}. Reason: {e}" ,
144
+ file. display( )
145
+ ) ;
146
+ None
147
+ }
89
148
} )
90
149
. collect ( )
91
150
}
0 commit comments