Remove unwraps (#316)

Remove all unwraps and clippy warnings
This commit is contained in:
Philip Kristoffersen
2023-01-07 13:48:26 +01:00
committed by GitHub
parent 59b67a11b7
commit f6188b130d
30 changed files with 706 additions and 553 deletions
+17 -16
View File
@@ -158,7 +158,7 @@ pub fn write_collections<S: AsRef<str>>(
&vdf_collections,
);
if let Some(new_string) = new_string {
std::fs::write(path, new_string).unwrap();
std::fs::write(path, new_string)?;
}
}
}
@@ -242,22 +242,19 @@ fn get_categories<S: AsRef<str>>(
Ok(res)
}
fn open_db() -> Result<DB, Box<dyn Error>> {
let location = get_level_db_location();
fn open_db() -> eyre::Result<DB> {
use eyre::eyre;
let location = get_level_db_location().ok_or(eyre!("Collections db not found"))?;
let options = Options::default();
let open_res = DB::open(location.unwrap(), options);
if let Err(e) = &open_res {
match &e.code {
rusty_leveldb::StatusCode::LockError => {
println!("Could not lock the steam level database, make sure steam is turned off when running synchronizations");
}
rusty_leveldb::StatusCode::NotFound => {
println!("Could not find the steam level database, try to open and close steam once and synchronize again");
}
_ => {}
};
}
Ok(open_res?)
let open_res = DB::open(location, options);
open_res.map_err(|e|{
use rusty_leveldb::StatusCode::*;
match e.code{
LockError => eyre!("Could not lock the steam level database, make sure steam is turned off when running synchronizations"),
NotFound => eyre!("Could not find the steam level database, try to open and close steam once and synchronize again"),
_ => eyre!("Failed opening collections file: {}",e.err),
}
})
}
fn get_namespace_keys<S: AsRef<str>>(steamid: S, db: &mut DB) -> HashSet<String> {
@@ -394,6 +391,10 @@ pub struct VdfCollection {
#[cfg(test)]
mod tests {
//Allow unwraps in test
#![allow(clippy::unwrap_in_result)]
#![allow(clippy::get_unwrap)]
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
+13 -7
View File
@@ -66,19 +66,25 @@ fn parse_manifest_string<S: AsRef<str>>(string: S) -> Option<SteamGameInfo> {
let app_id_line = lines.find(|l| l.contains("\"appid\""));
let name_line = lines.find(|l| l.contains("\"name\""));
match (app_id_line, name_line) {
(Some(app_id_line), Some(name_line)) => Some(SteamGameInfo {
name: name_line[10..name_line.len() - 1].to_string(),
appid: app_id_line[11..app_id_line.len() - 1]
.to_string()
.parse()
.unwrap(),
}),
(Some(app_id_line), Some(name_line)) => {
let appid = app_id_line[11..app_id_line.len() - 1].to_string().parse();
match appid {
Ok(appid) => Some(SteamGameInfo {
name: name_line[10..name_line.len() - 1].to_string(),
appid,
}),
Err(_) => None,
}
}
_ => None,
}
}
#[cfg(test)]
mod tests {
//Okay to unwrap in tests
#![allow(clippy::unwrap_in_result)]
#![allow(clippy::unwrap_used)]
use super::*;
+6 -2
View File
@@ -2,16 +2,17 @@ use std::path::Path;
use nom::FindSubstring;
pub fn setup_proton_games<B: AsRef<str>>(games: &[B]) {
pub fn setup_proton_games<B: AsRef<str>>(games: &[B]) -> eyre::Result<()>{
if let Ok(home) = std::env::var("HOME") {
let config_file = Path::new(&home).join(".local/share/Steam/config/config.vdf");
if config_file.exists() {
if let Ok(config_content) = std::fs::read_to_string(&config_file) {
let new_string = enable_proton_games(config_content, games);
std::fs::write(config_file, new_string).unwrap();
std::fs::write(config_file, new_string)?;
}
}
}
Ok(())
}
fn enable_proton_games<S: AsRef<str>, B: AsRef<str>>(vdf_content: S, games: &[B]) -> String {
@@ -96,6 +97,9 @@ fn find_indexes<S: AsRef<str>>(vdf_content: S) -> Option<SectionInfo> {
#[cfg(target_family = "unix")]
mod tests {
//Okay to unwrap in tests
#![allow(clippy::unwrap_in_result)]
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
+20 -20
View File
@@ -8,14 +8,14 @@ use steam_shortcuts_util::{parse_shortcuts, shortcut::ShortcutOwned};
use super::SteamSettings;
pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> eyre::Result<ShortcutInfo> {
let mut shortcuts = vec![];
let new_path = match &user.shortcut_path {
Some(shortcut_path) => {
let content = std::fs::read(shortcut_path).unwrap();
let content = std::fs::read(shortcut_path)?;
shortcuts = parse_shortcuts(content.as_slice())
.unwrap()
.map_err(|e| eyre::format_err!("Could not parse shortcuts: {:?}", e))?
.iter()
.map(|s| s.to_owned())
.collect();
@@ -27,15 +27,15 @@ pub fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
user.steam_user_data_folder
);
let path = Path::new(&user.steam_user_data_folder).join("config");
std::fs::create_dir_all(path.clone()).unwrap();
std::fs::create_dir_all(path.clone())?;
path.join("shortcuts.vdf")
}
};
ShortcutInfo {
Ok(ShortcutInfo {
shortcuts,
path: new_path,
}
})
}
pub struct ShortcutInfo {
@@ -51,22 +51,22 @@ pub struct SteamUsersInfo {
}
/// Get the paths to the steam users shortcuts (one for each user)
pub fn get_shortcuts_paths(
settings: &SteamSettings,
) -> Result<Vec<SteamUsersInfo>, Box<dyn Error + Sync + Send>> {
pub fn get_shortcuts_paths(settings: &SteamSettings) -> eyre::Result<Vec<SteamUsersInfo>> {
let steam_path_str = get_steam_path(settings)?;
let steam_path = Path::new(&steam_path_str);
if !steam_path.exists() {
return Result::Err(Box::new(SteamFolderNotFound {
location_tried: format!("{:?}", steam_path),
}));
return Err(eyre::format_err!(
"Steam folder not found at: {:?}",
steam_path
));
}
let user_data_path = steam_path.join("userdata");
if !user_data_path.exists() {
return Result::Err(Box::new(SteamFolderNotFound {
location_tried: format!("{:?}", user_data_path),
}));
return Err(eyre::format_err!(
"Steam user data folder not found at: {:?}",
user_data_path
));
}
if !user_data_path.exists() {}
@@ -89,7 +89,7 @@ pub fn get_shortcuts_paths(
if shortcuts_path.exists() {
return SteamUsersInfo {
steam_user_data_folder: folder_string,
shortcut_path: Some(shortcuts_path.to_str().unwrap().to_string()),
shortcut_path: Some(shortcuts_path.to_string_lossy().to_string()),
user_id,
};
} else {
@@ -104,7 +104,7 @@ pub fn get_shortcuts_paths(
Ok(users_info)
}
pub fn get_steam_path(settings: &SteamSettings) -> Result<String, Box<dyn Error + Sync + Send>> {
pub fn get_steam_path(settings: &SteamSettings) -> eyre::Result<String> {
let user_location = settings.location.clone();
let steam_path_str = match user_location {
Some(location) => location,
@@ -113,7 +113,7 @@ pub fn get_steam_path(settings: &SteamSettings) -> Result<String, Box<dyn Error
Ok(steam_path_str)
}
pub fn get_default_location() -> Result<String, Box<dyn Error + Sync + Send>> {
pub fn get_default_location() -> eyre::Result<String> {
#[cfg(target_os = "windows")]
let path_string = {
let key = "PROGRAMFILES(X86)";
@@ -198,8 +198,8 @@ impl Error for SteamUsersDataEmpty {
self.location_tried.as_str()
}
}
pub fn get_users_images(user: &SteamUsersInfo) -> Result<Vec<String>, Box<dyn Error>> {
let grid_folder = Path::new(user.steam_user_data_folder.as_str()).join("config/grid");
pub fn get_users_images(data_folder: &str) -> Result<Vec<String>, Box<dyn Error>> {
let grid_folder = Path::new(data_folder).join("config/grid");
if !grid_folder.exists() {
std::fs::create_dir_all(&grid_folder)?;
}