Skip to content

Commit b8241b4

Browse files
committed
Added in Logging, and cleaned up the unwraps
Cleaned up all of the unwraps, and added in some basic simple logging. It just logs to a file in the same directory as the executable, and only logs errors that happen in place of the previous unwraps. I also updated the RFD crate to v 0.12
1 parent d41605c commit b8241b4

File tree

2 files changed

+83
-22
lines changed

2 files changed

+83
-22
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pv-unlocker"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
authors = ["Carl <carl@vadoola.com>"]
55
edition = "2021"
66
build = "build.rs"
@@ -13,7 +13,9 @@ ab_versions = {git = "https://github.com/Vadoola/ab_versions_rs.git"}
1313
clap = { version = "4.3", features = ["derive"] }
1414
rayon = "1.5"
1515
wild = "2.1"
16-
rfd = { version = "0.11.4", default-features = false, features = ["xdg-portal"]}
16+
rfd = { version = "0.12", default-features = false, features = ["xdg-portal"]}
17+
log = "0.4.20"
18+
simplelog = "0.12.1"
1719

1820
[build-dependencies]
1921
slint-build = "1.1"

src/main.rs

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
use ab_versions::{get_version, is_protected, strip_protection};
99
use clap::Parser;
10+
use log::error;
1011
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
1112
use rfd::FileDialog;
13+
use simplelog::{CombinedLogger, Config, LevelFilter, SimpleLogger, WriteLogger};
1214
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+
};
1418

1519
#[derive(Parser)]
1620
#[command(author, version, about, long_about = None)]
@@ -21,6 +25,24 @@ struct Args {
2125
slint::include_modules!();
2226

2327
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+
2446
let args = Args::parse_from(wild::args());
2547
let files = Rc::new(RefCell::new(process_paths(args.files)));
2648

@@ -29,23 +51,38 @@ fn main() -> Result<(), slint::PlatformError> {
2951

3052
//if files were passed on the command line process them and add them to the UI model
3153
let info = get_file_info(&files.borrow());
32-
file_model.borrow_mut().extend(info.into_iter());
54+
file_model.borrow_mut().extend(info);
3355

3456
ui.set_files(ModelRc::from(file_model.clone()));
3557

3658
let unlock_file_model = file_model.clone();
3759
let unlock_files = files.clone();
3860
ui.on_unlock(move |file, idx| {
3961
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+
}
4986
} //else display some sort of toast message with the error?
5087
});
5188

@@ -69,11 +106,15 @@ fn process_paths(files: Vec<PathBuf>) -> HashMap<String, PathBuf> {
69106
files
70107
.into_iter()
71108
.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+
};
77118
(name, pb)
78119
})
79120
.collect()
@@ -82,10 +123,28 @@ fn process_paths(files: Vec<PathBuf>) -> HashMap<String, PathBuf> {
82123
fn get_file_info(files: &HashMap<String, PathBuf>) -> Vec<file_info> {
83124
files
84125
.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+
}
89148
})
90149
.collect()
91150
}

0 commit comments

Comments
 (0)