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
+14 -7
View File
@@ -19,7 +19,9 @@ impl<'a> CachedSearch<'a> {
}
pub fn save(&self) {
save_search_map(&self.search_map);
if let Err(err) = save_search_map(&self.search_map){
eprintln!("Failed saving searchmap : {:?}",err);
}
}
pub fn set_cache<S>(&mut self, app_id: u32, name: S, new_grid_id: usize)
@@ -58,16 +60,21 @@ impl<'a> CachedSearch<'a> {
fn get_search_map() -> SearchMap {
let path = get_cache_file();
if path.exists() {
let string = std::fs::read_to_string(path).unwrap();
serde_json::from_str::<SearchMap>(&string).expect("Failed to parse cache.json")
std::fs::read_to_string(path)
.ok()
.and_then(|string| {
serde_json::from_str::<SearchMap>(&string).ok()
})
.unwrap_or_default()
} else {
SearchMap::new()
}
}
fn save_search_map(search_map: &SearchMap) {
let string = serde_json::to_string(search_map).unwrap();
fn save_search_map(search_map: &SearchMap) -> eyre::Result<()> {
let string = serde_json::to_string(search_map)?;
let path = get_cache_file();
let mut file = File::create(path).unwrap();
file.write_all(string.as_bytes()).unwrap();
let mut file = File::create(path)?;
file.write_all(string.as_bytes())?;
Ok(())
}
+73 -38
View File
@@ -23,10 +23,31 @@ use crate::sync::SyncProgress;
const CONCURRENT_REQUESTS: usize = 10;
impl SearchSettings for Settings {
fn download_animated(&self) -> bool {
self.steamgrid_db.prefer_animated
}
fn download_big_picture(&self) -> bool {
self.steam.optimize_for_big_picture
}
fn allow_nsfw(&self) -> bool {
self.steamgrid_db.allow_nsfw
}
fn only_download_boilr_images(&self) -> bool {
self.steamgrid_db.only_download_boilr_images
}
fn is_image_banned(&self, image_type: &ImageType, app_id: u32) -> bool {
self.steamgrid_db.is_image_banned(image_type, app_id)
}
}
pub async fn download_images_for_users<'b>(
settings: &Settings,
users: &[SteamUsersInfo],
download_animated: bool,
sender: &mut Option<Sender<SyncProgress>>,
) {
let auth_key = &settings.steamgrid_db.auth_key;
@@ -40,24 +61,29 @@ pub async fn download_images_for_users<'b>(
if let Some(sender) = sender {
let _ = sender.send(SyncProgress::FindingImages);
}
let to_downloads = stream::iter(users)
.map(|user| {
let shortcut_info = get_shortcuts_for_user(user);
async move {
let known_images = get_users_images(user).unwrap_or_default();
let res = search_for_images_to_download(
known_images,
user.steam_user_data_folder.as_str(),
&shortcut_info.shortcuts,
search,
client,
download_animated,
settings.steam.optimize_for_big_picture,
settings,
)
.await;
res.unwrap_or_default()
}
let users_info = users.iter().filter_map(|user| {
let shortcut_info = get_shortcuts_for_user(user);
shortcut_info
.map(|shortcut_info| {
let data_folder = &user.steam_user_data_folder;
(shortcut_info, data_folder)
})
.ok()
});
let to_downloads = stream::iter(users_info)
.map(|(shortcut_info, data_folder)| async move {
let known_images = get_users_images(data_folder).unwrap_or_default();
let res = search_for_images_to_download(
known_images,
data_folder.as_str(),
&shortcut_info.shortcuts,
search,
client,
settings,
)
.await;
res.unwrap_or_default()
})
.buffer_unordered(CONCURRENT_REQUESTS)
.collect::<Vec<Vec<ToDownload>>>()
@@ -128,15 +154,21 @@ pub struct PublicGameResponse {
data: Option<PublicGameResponseData>,
}
async fn search_for_images_to_download(
pub trait SearchSettings {
fn download_animated(&self) -> bool;
fn download_big_picture(&self) -> bool;
fn allow_nsfw(&self) -> bool;
fn only_download_boilr_images(&self) -> bool;
fn is_image_banned(&self, image_type: &ImageType, app_id: u32) -> bool;
}
async fn search_for_images_to_download<T: SearchSettings>(
known_images: Vec<String>,
user_data_folder: &str,
shortcuts: &[ShortcutOwned],
search: &CachedSearch<'_>,
client: &Client,
download_animated: bool,
download_big_picture: bool,
settings: &Settings,
search_settins: &T,
) -> Result<Vec<ToDownload>, Box<dyn Error>> {
let types = {
let mut types = vec![
@@ -146,7 +178,7 @@ async fn search_for_images_to_download(
ImageType::WideGrid,
ImageType::Icon,
];
if download_big_picture {
if search_settins.download_big_picture() {
types.push(ImageType::BigPicture);
}
types
@@ -154,7 +186,7 @@ async fn search_for_images_to_download(
let shortcuts_to_search_for = shortcuts
.iter()
.filter(|s| !settings.steamgrid_db.only_download_boilr_images || s.is_boilr_shortcut())
.filter(|s| !search_settins.only_download_boilr_images() || s.is_boilr_shortcut())
.filter(|s| {
// if we are missing any of the images we need to search for them
types
@@ -171,13 +203,10 @@ async fn search_for_images_to_download(
let search_results_a = stream::iter(shortcuts_to_search_for)
.map(|s| async move {
let search_result = search.search(s.app_id, &s.app_name).await;
if search_result.is_err() {
return None;
match search_result {
Ok(Some(search_result)) => Some((s.app_id, search_result)),
_ => None,
}
let search_result = search_result.unwrap();
search_result?;
let search_result = search_result.unwrap();
Some((s.app_id, search_result))
})
.buffer_unordered(CONCURRENT_REQUESTS)
.collect::<Vec<Option<(u32, usize)>>>()
@@ -192,7 +221,7 @@ async fn search_for_images_to_download(
let images_needed = shortcuts
.iter()
.filter(|s| search_results.contains_key(&s.app_id))
.filter(|s| !settings.steamgrid_db.is_image_banned(&image_type, s.app_id))
.filter(|s| !search_settins.is_image_banned(&image_type, s.app_id))
.filter(|s| !known_images.contains(&image_type.file_name_no_extension(s.app_id)));
let image_ids: Vec<usize> = images_needed
.clone()
@@ -203,8 +232,14 @@ async fn search_for_images_to_download(
let shortcuts: Vec<&ShortcutOwned> = images_needed.collect();
for image_ids in image_ids.chunks(99) {
let image_search_result =
get_images_for_ids(client, image_ids, &image_type, download_animated, settings.steamgrid_db.allow_nsfw).await;
let image_search_result = get_images_for_ids(
client,
image_ids,
&image_type,
search_settins.download_animated(),
search_settins.allow_nsfw(),
)
.await;
match image_search_result {
Ok(images) => {
let images = images
@@ -237,7 +272,7 @@ async fn search_for_images_to_download(
to_download.extend(download_for_this_type);
}
Err(err) => println!("Error getting images: {}", err),
Err(err) => eprintln!("Error getting images: {}", err),
}
}
}
@@ -388,17 +423,17 @@ fn icon_url(steam_app_id: &str, icon_id: &str) -> String {
)
}
pub async fn download_to_download(to_download: &ToDownload) -> Result<(), Box<dyn Error>> {
pub async fn download_to_download(to_download: &ToDownload) -> eyre::Result<()> {
println!(
"Downloading {:?} for {} to {:?}",
to_download.image_type, to_download.app_name, to_download.path
);
let path = &to_download.path;
let url = &to_download.url;
let mut file = File::create(path).unwrap();
let mut file = File::create(path)?;
let response = reqwest::get(url).await?;
let content = response.bytes().await?;
file.write_all(&content).unwrap();
file.write_all(&content)?;
Ok(())
}