Skip to content

Commit cc57ca7

Browse files
authored
139 add and resolve clippy lints to prevent unwrap and expect functions (#154)
* fix: Add lint and remove all unwraps from lib.rs Signed-off-by: quexeky <[email protected]> * chore: Remove all unwraps from util.rs and add state_lock macro Signed-off-by: quexeky <[email protected]> * chore: Add CacheError and remove unwraps from fetch_object Signed-off-by: quexeky <[email protected]> * chore: Remove unwraps from fetch_object and server_proto Signed-off-by: quexeky <[email protected]> * chore: Remove unwraps from auth.rs Signed-off-by: quexeky <[email protected]> * chore: Remove unwraps from process_handlers Signed-off-by: quexeky <[email protected]> * chore: Clippy unwrap linting Signed-off-by: quexeky <[email protected]> * chore: Remove lint Because not everything is actually resolved yet: will be resolved with a restructure of the library Signed-off-by: quexeky <[email protected]> * chore: Make the rest of clippy happy Signed-off-by: quexeky <[email protected]> * fix: Send download signal instead of triggering self.on_error Signed-off-by: quexeky <[email protected]> * fix: Corrupted state should panic Signed-off-by: quexeky <[email protected]> * fix: Use debug instead of display for specific errors Signed-off-by: quexeky <[email protected]> * fix: Settings now log error instead of panicking Signed-off-by: quexeky <[email protected]> --------- Signed-off-by: quexeky <[email protected]>
1 parent 70cecda commit cc57ca7

40 files changed

+6001
-622
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"@tauri-apps/plugin-os": "^2.3.0",
1515
"@tauri-apps/plugin-shell": "^2.3.0",
1616
"pino": "^9.7.0",
17-
"pino-pretty": "^13.1.1"
17+
"pino-pretty": "^13.1.1",
18+
"tauri": "^0.15.0"
1819
},
1920
"devDependencies": {
2021
"@tauri-apps/cli": "^2.7.1"

src-tauri/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/src/client/cleanup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use log::{debug, error};
22
use tauri::AppHandle;
33

4-
use crate::AppState;
4+
use crate::{lock, AppState};
55

66
#[tauri::command]
77
pub fn quit(app: tauri::AppHandle, state: tauri::State<'_, std::sync::Mutex<AppState<'_>>>) {
@@ -10,7 +10,7 @@ pub fn quit(app: tauri::AppHandle, state: tauri::State<'_, std::sync::Mutex<AppS
1010

1111
pub fn cleanup_and_exit(app: &AppHandle, state: &tauri::State<'_, std::sync::Mutex<AppState<'_>>>) {
1212
debug!("cleaning up and exiting application");
13-
let download_manager = state.lock().unwrap().download_manager.clone();
13+
let download_manager = lock!(state).download_manager.clone();
1414
match download_manager.ensure_terminated() {
1515
Ok(res) => match res {
1616
Ok(()) => debug!("download manager terminated correctly"),

src-tauri/src/client/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::AppState;
1+
use crate::{lock, AppState};
22

33
#[tauri::command]
44
pub fn fetch_state(
55
state: tauri::State<'_, std::sync::Mutex<AppState<'_>>>,
66
) -> Result<String, String> {
7-
let guard = state.lock().unwrap();
7+
let guard = lock!(state);
88
let cloned_state = serde_json::to_string(&guard.clone()).map_err(|e| e.to_string())?;
99
drop(guard);
1010
Ok(cloned_state)

src-tauri/src/database/commands.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ use std::{
44
path::{Path, PathBuf},
55
};
66

7+
use log::error;
78
use serde_json::Value;
89

910
use crate::{
10-
database::{db::borrow_db_mut_checked, scan::scan_install_dirs}, error::download_manager_error::DownloadManagerError,
11+
database::{db::borrow_db_mut_checked, scan::scan_install_dirs},
12+
error::download_manager_error::DownloadManagerError,
1113
};
1214

1315
use super::{
14-
db::{borrow_db_checked, DATA_ROOT_DIR},
16+
db::{DATA_ROOT_DIR, borrow_db_checked},
1517
debug::SystemData,
1618
models::data::Settings,
1719
};
@@ -67,11 +69,25 @@ pub fn add_download_dir(new_dir: PathBuf) -> Result<(), DownloadManagerError<()>
6769
#[tauri::command]
6870
pub fn update_settings(new_settings: Value) {
6971
let mut db_lock = borrow_db_mut_checked();
70-
let mut current_settings = serde_json::to_value(db_lock.settings.clone()).unwrap();
71-
for (key, value) in new_settings.as_object().unwrap() {
72+
let mut current_settings =
73+
serde_json::to_value(db_lock.settings.clone()).expect("Failed to parse existing settings");
74+
let values = match new_settings.as_object() {
75+
Some(values) => values,
76+
None => {
77+
error!("Could not parse settings values into object");
78+
return;
79+
}
80+
};
81+
for (key, value) in values {
7282
current_settings[key] = value.clone();
7383
}
74-
let new_settings: Settings = serde_json::from_value(current_settings).unwrap();
84+
let new_settings: Settings = match serde_json::from_value(current_settings) {
85+
Ok(settings) => settings,
86+
Err(e) => {
87+
error!("Could not parse settings with error {}", e);
88+
return;
89+
}
90+
};
7591
db_lock.settings = new_settings;
7692
}
7793
#[tauri::command]

src-tauri/src/database/db.rs

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ static DATA_ROOT_PREFIX: &'static str = "drop";
2121
#[cfg(debug_assertions)]
2222
static DATA_ROOT_PREFIX: &str = "drop-debug";
2323

24-
pub static DATA_ROOT_DIR: LazyLock<Arc<PathBuf>> =
25-
LazyLock::new(|| Arc::new(dirs::data_dir().unwrap().join(DATA_ROOT_PREFIX)));
24+
pub static DATA_ROOT_DIR: LazyLock<Arc<PathBuf>> = LazyLock::new(|| {
25+
Arc::new(
26+
dirs::data_dir()
27+
.expect("Failed to get data dir")
28+
.join(DATA_ROOT_PREFIX),
29+
)
30+
});
2631

2732
// Custom JSON serializer to support everything we need
2833
#[derive(Debug, Default, Clone)]
@@ -63,13 +68,49 @@ impl DatabaseImpls for DatabaseInterface {
6368
let pfx_dir = DATA_ROOT_DIR.join("pfx");
6469

6570
debug!("creating data directory at {DATA_ROOT_DIR:?}");
66-
create_dir_all(DATA_ROOT_DIR.as_path()).unwrap();
67-
create_dir_all(&games_base_dir).unwrap();
68-
create_dir_all(&logs_root_dir).unwrap();
69-
create_dir_all(&cache_dir).unwrap();
70-
create_dir_all(&pfx_dir).unwrap();
71-
72-
let exists = fs::exists(db_path.clone()).unwrap();
71+
create_dir_all(DATA_ROOT_DIR.as_path()).unwrap_or_else(|e| {
72+
panic!(
73+
"Failed to create directory {} with error {}",
74+
DATA_ROOT_DIR.display(),
75+
e
76+
)
77+
});
78+
create_dir_all(&games_base_dir).unwrap_or_else(|e| {
79+
panic!(
80+
"Failed to create directory {} with error {}",
81+
games_base_dir.display(),
82+
e
83+
)
84+
});
85+
create_dir_all(&logs_root_dir).unwrap_or_else(|e| {
86+
panic!(
87+
"Failed to create directory {} with error {}",
88+
logs_root_dir.display(),
89+
e
90+
)
91+
});
92+
create_dir_all(&cache_dir).unwrap_or_else(|e| {
93+
panic!(
94+
"Failed to create directory {} with error {}",
95+
cache_dir.display(),
96+
e
97+
)
98+
});
99+
create_dir_all(&pfx_dir).unwrap_or_else(|e| {
100+
panic!(
101+
"Failed to create directory {} with error {}",
102+
pfx_dir.display(),
103+
e
104+
)
105+
});
106+
107+
let exists = fs::exists(db_path.clone()).unwrap_or_else(|e| {
108+
panic!(
109+
"Failed to find if {} exists with error {}",
110+
db_path.display(),
111+
e
112+
)
113+
});
73114

74115
if exists {
75116
match PathDatabase::load_from_path(db_path.clone()) {
@@ -78,21 +119,19 @@ impl DatabaseImpls for DatabaseInterface {
78119
}
79120
} else {
80121
let default = Database::new(games_base_dir, None, cache_dir);
81-
debug!(
82-
"Creating database at path {}",
83-
db_path.as_os_str().to_str().unwrap()
84-
);
122+
debug!("Creating database at path {}", db_path.display());
85123
PathDatabase::create_at_path(db_path, default).expect("Database could not be created")
86124
}
87125
}
88126

89127
fn database_is_set_up(&self) -> bool {
90-
!self.borrow_data().unwrap().base_url.is_empty()
128+
!borrow_db_checked().base_url.is_empty()
91129
}
92130

93131
fn fetch_base_url(&self) -> Url {
94-
let handle = self.borrow_data().unwrap();
95-
Url::parse(&handle.base_url).unwrap()
132+
let handle = borrow_db_checked();
133+
Url::parse(&handle.base_url)
134+
.unwrap_or_else(|_| panic!("Failed to parse base url {}", handle.base_url))
96135
}
97136
}
98137

@@ -111,13 +150,16 @@ fn handle_invalid_database(
111150
base
112151
};
113152
info!("old database stored at: {}", new_path.to_string_lossy());
114-
fs::rename(&db_path, &new_path).unwrap();
115-
116-
let db = Database::new(
117-
games_base_dir.into_os_string().into_string().unwrap(),
118-
Some(new_path),
119-
cache_dir,
120-
);
153+
fs::rename(&db_path, &new_path).unwrap_or_else(|e| {
154+
panic!(
155+
"Could not rename database {} to {} with error {}",
156+
db_path.display(),
157+
new_path.display(),
158+
e
159+
)
160+
});
161+
162+
let db = Database::new(games_base_dir, Some(new_path), cache_dir);
121163

122164
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")
123165
}

src-tauri/src/database/scan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ pub fn scan_install_dirs() {
2424
if !drop_data_file.exists() {
2525
continue;
2626
}
27-
let game_id = game.file_name().into_string().unwrap();
27+
let game_id = game.file_name().display().to_string();
2828
let Ok(drop_data) = DropData::read(&game.path()) else {
2929
warn!(
3030
".dropdata exists for {}, but couldn't read it. is it corrupted?",
31-
game.file_name().into_string().unwrap()
31+
game.file_name().display()
3232
);
3333
continue;
3434
};
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::sync::Mutex;
22

3-
use crate::{database::models::data::DownloadableMetadata, AppState};
3+
use crate::{AppState, database::models::data::DownloadableMetadata, lock};
44

55
#[tauri::command]
66
pub fn pause_downloads(state: tauri::State<'_, Mutex<AppState>>) {
7-
state.lock().unwrap().download_manager.pause_downloads();
7+
lock!(state).download_manager.pause_downloads();
88
}
99

1010
#[tauri::command]
1111
pub fn resume_downloads(state: tauri::State<'_, Mutex<AppState>>) {
12-
state.lock().unwrap().download_manager.resume_downloads();
12+
lock!(state).download_manager.resume_downloads();
1313
}
1414

1515
#[tauri::command]
@@ -18,14 +18,12 @@ pub fn move_download_in_queue(
1818
old_index: usize,
1919
new_index: usize,
2020
) {
21-
state
22-
.lock()
23-
.unwrap()
21+
lock!(state)
2422
.download_manager
2523
.rearrange(old_index, new_index);
2624
}
2725

2826
#[tauri::command]
2927
pub fn cancel_game(state: tauri::State<'_, Mutex<AppState>>, meta: DownloadableMetadata) {
30-
state.lock().unwrap().download_manager.cancel(meta);
28+
lock!(state).download_manager.cancel(meta);
3129
}

0 commit comments

Comments
 (0)