Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unwraps #316

Merged
merged 32 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d0e0132
Setup clippy to disalow unwraps
PhilipK Jan 6, 2023
d39044e
Remove unwrap on image search
PhilipK Jan 6, 2023
37eb931
Error handling on image download
PhilipK Jan 6, 2023
d8dac5b
Remove unwrap from game_image_button
PhilipK Jan 6, 2023
551dc1d
Remove unwrap when deleting old images
PhilipK Jan 6, 2023
654f75b
Remove unwrap when picking new image
PhilipK Jan 6, 2023
c29675e
Remove unwrap when rendering page shortcut
PhilipK Jan 6, 2023
d92432b
Remove unwrap on shortcut selected
PhilipK Jan 6, 2023
bc22269
Remove unwrap in shortcut images overview
PhilipK Jan 6, 2023
6e66d90
Remove unwrap when getting key
PhilipK Jan 6, 2023
27ffa9d
Remove unwrap in image type clear
PhilipK Jan 6, 2023
d369cc9
Remove unwrap in image clear
PhilipK Jan 6, 2023
b4b516d
Remove unwrap on app start
PhilipK Jan 6, 2023
8e8b6f7
Remove unwrap when syncing
PhilipK Jan 6, 2023
a48d51e
Remove unwrapts in get_shortcut_paths
PhilipK Jan 6, 2023
304cbea
Remove unwrap in sync_shortcuts
PhilipK Jan 6, 2023
ad31175
Remove unwrap in backup shortcuts
PhilipK Jan 6, 2023
fe6da1d
Remove more unwraps
PhilipK Jan 6, 2023
61e457e
More unwrap removal
PhilipK Jan 7, 2023
8306881
Remove unwrap when getting search map
PhilipK Jan 7, 2023
c758dca
Remove last unwraps
PhilipK Jan 7, 2023
a5c33dd
Fixing clippy
PhilipK Jan 7, 2023
2fd202b
Automatic clippy fixes
PhilipK Jan 7, 2023
35aed59
Using all results
PhilipK Jan 7, 2023
5a9ec2e
Remove more unwraps
PhilipK Jan 7, 2023
3d7a126
Remove more unwraps
PhilipK Jan 7, 2023
29f407f
Seperate image into struct
PhilipK Jan 7, 2023
c57248a
Refactor game image button
PhilipK Jan 7, 2023
36c641f
Rename Game Button
PhilipK Jan 7, 2023
3adb42d
Rename game button
PhilipK Jan 7, 2023
b2670a4
Fix typo
PhilipK Jan 7, 2023
99e16bd
Box shortcuts
PhilipK Jan 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![deny(clippy::unwrap_in_result)]
#![deny(clippy::get_unwrap)]
#![deny(clippy::unwrap_used)]

mod config;
mod migration;
mod platforms;
Expand All @@ -16,9 +20,9 @@ fn main() -> Result<()> {

let args: Vec<String> = std::env::args().collect();
if args.contains(&"--no-ui".to_string()) {
ui::run_sync();
ui::run_sync()?;
} else {
ui::run_ui(args);
ui::run_ui(args)?;
}
Ok(())
}
Expand Down
19 changes: 11 additions & 8 deletions src/steamgriddb/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,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)>>>()
Expand All @@ -203,8 +200,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,
download_animated,
settings.steamgrid_db.allow_nsfw,
)
.await;
match image_search_result {
Ok(images) => {
let images = images
Expand Down
101 changes: 69 additions & 32 deletions src/ui/components/game_image_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,45 @@ use crate::steamgriddb::{ImageType, ToDownload};
use crate::ui::images::{clamp_to_width, ImageHandles, TextureDownloadState};
use crate::ui::ui_images::load_image_from_path;


pub fn render_image_from_path(
ui: &mut egui::Ui,
image_handles: &ImageHandles,
path: &Path,
max_width: f32,
text: &str,
) -> bool {
render_possible_image(ui, image_handles, path, max_width, text, &ImageType::Grid, None, None)
render_possible_image(
ui,
image_handles,
path,
max_width,
text,
&ImageType::Grid,
None,
None,
)
}

pub fn render_image_from_path_image_type(
ui: &mut egui::Ui,
image_handles: &ImageHandles,
path: &Path,
max_width: f32,
text: &str,
text: &str,
image_type: &ImageType,
) -> bool {
render_possible_image(ui, image_handles, path, max_width, text, image_type, None, None)
render_possible_image(
ui,
image_handles,
path,
max_width,
text,
image_type,
None,
None,
)
}


pub fn render_image_from_path_or_url(
ui: &mut egui::Ui,
image_handles: &ImageHandles,
Expand Down Expand Up @@ -104,41 +120,62 @@ fn render_possible_image(
}
}
TextureDownloadState::Failed => {
let button =
ui.add_sized([max_width, max_width * image_type.ratio()], Button::new(text).wrap(true));
let button = ui.add_sized(
[max_width, max_width * image_type.ratio()],
Button::new(text).wrap(true),
);
if button.clicked() {
return true;
}
}
}
}
None => {
if !path.exists() && url.is_none() {
image_handles.insert(image_key, TextureDownloadState::Failed);
} else {
//We need to start a download
//Redownload if file is too small
if !path.exists()
|| std::fs::metadata(path).map(|m| m.len()).unwrap_or_default() < 2
{
image_handles.insert(image_key.clone(), TextureDownloadState::Downloading);
let to_download = ToDownload {
path: path.to_path_buf(),
url: url.unwrap().to_string(),
app_name: "Thumbnail".to_string(),
image_type: *image_type,
};
let image_handles = image_handles.clone();
let image_key = image_key.clone();
if let Some(rt) = rt {
rt.spawn_blocking(move || {
block_on(crate::steamgriddb::download_to_download(&to_download))
.unwrap();
image_handles.insert(image_key, TextureDownloadState::Downloaded);
});
match url {
Some(url) => {
//We need to start a download
//Redownload if file is too small
if !path.exists()
|| std::fs::metadata(path).map(|m| m.len()).unwrap_or_default() < 2
{
image_handles.insert(image_key.clone(), TextureDownloadState::Downloading);
let to_download = ToDownload {
path: path.to_path_buf(),
url: url.to_string(),
app_name: "Thumbnail".to_string(),
image_type: *image_type,
};
let image_handles = image_handles.clone();
let image_key = image_key.clone();
if let Some(rt) = rt {
rt.spawn_blocking(move || {
match block_on(crate::steamgriddb::download_to_download(
&to_download,
)) {
Ok(_) => {
image_handles
.insert(image_key, TextureDownloadState::Downloaded);
}
Err(err) => {
println!(
"Failed downloading image {} error: {:?}",
to_download.url, err
);
image_handles
.insert(image_key, TextureDownloadState::Failed);
}
}
});
}
} else {
image_handles.insert(image_key.clone(), TextureDownloadState::Downloaded);
}
}
None => {
//Not possible to download
if !path.exists() {
image_handles.insert(image_key, TextureDownloadState::Failed);
}
} else {
image_handles.insert(image_key.clone(), TextureDownloadState::Downloaded);
}
}
}
Expand Down
36 changes: 15 additions & 21 deletions src/ui/images/hasimagekey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ use std::path::{Path, PathBuf};

use steam_shortcuts_util::shortcut::ShortcutOwned;

use crate::{steamgriddb::ImageType, steam::SteamGameInfo};

use super::{gametype::GameType, constants::POSSIBLE_EXTENSIONS};
use crate::{steam::SteamGameInfo, steamgriddb::ImageType};

use super::{constants::POSSIBLE_EXTENSIONS, gametype::GameType};

pub trait HasImageKey {

///Gives a unique key to an image given its type and user path
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String);

}


impl HasImageKey for GameType {
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
match self {
Expand All @@ -24,31 +20,29 @@ impl HasImageKey for GameType {
}
}


impl HasImageKey for SteamGameInfo {
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
let mut keys = POSSIBLE_EXTENSIONS
.iter()
.map(|ext| key_from_extension(self.appid, image_type, user_path, ext));
let first = keys.next().unwrap();
let other = keys.find(|(exsists, _, _)| *exsists);
let (_, path, key) = other.unwrap_or(first);
(path, key)
let app_id = self.appid;
key(app_id, image_type, user_path)
}
}

impl HasImageKey for ShortcutOwned {
fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
let mut keys = POSSIBLE_EXTENSIONS
.iter()
.map(|ext| key_from_extension(self.app_id, image_type, user_path, ext));
let first = keys.next().unwrap();
let other = keys.find(|(exsists, _, _)| *exsists);
let (_, path, key) = other.unwrap_or(first);
(path, key)
let app_id = self.app_id;
key(app_id, image_type, user_path)
}
}

fn key(app_id: u32, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
let ext = |ext| key_from_extension(app_id, image_type, user_path, ext);
let keys = POSSIBLE_EXTENSIONS.map(ext);
let other = keys.iter().find(|(exsists, _, _)| *exsists);
let first = ext(POSSIBLE_EXTENSIONS[0]);
let (_, path, key) = other.unwrap_or_else(|| &first);
Fixed Show fixed Hide fixed
(path.to_path_buf(), key.to_string())
}

fn key_from_extension(
app_id: u32,
image_type: &ImageType,
Expand Down
Loading