Refactor code for readability

This commit is contained in:
Philip Kristoffersen
2021-10-06 21:50:09 +02:00
parent 95053a9d49
commit fc42cf6a1f
6 changed files with 141 additions and 112 deletions
+9 -7
View File
@@ -19,24 +19,26 @@ impl<'a> CachedSearch<'a> {
save_search_map(&self.search_map);
}
pub async fn search(
pub async fn search<S>(
&mut self,
app_id: u32,
query: &str,
) -> Result<Option<usize>, Box<dyn std::error::Error>> {
query: S,
) -> Result<Option<usize>, Box<dyn std::error::Error>>
where
S: AsRef<str> + Into<String>,
{
let cached_result = self.search_map.get(&app_id);
if let Some(result) = cached_result {
return Ok(Some(result.1));
}
println!("Searching for {}", query);
let search = self.client.search(query).await?;
println!("Searching for {}", query.as_ref());
let search = self.client.search(query.as_ref()).await?;
if search.is_empty() {
return Ok(None);
}
let first_item = &search[0];
let assumed_id = first_item.id;
self.search_map
.insert(app_id, (query.to_owned(), assumed_id));
self.search_map.insert(app_id, (query.into(), assumed_id));
Ok(Some(assumed_id))
}
+4 -4
View File
@@ -3,17 +3,17 @@ use std::io::Write;
use std::{collections::HashMap, path::Path};
use std::error::Error;
use steam_shortcuts_util::Shortcut;
use steam_shortcuts_util::shortcut::ShortcutOwned;
use steamgriddb_api::Client;
use crate::steamgriddb::ImageType;
use super::CachedSearch;
pub async fn download_images<'a, 'b>(
pub async fn download_images<'b>(
known_images: Vec<String>,
user_data_folder: &str,
shortcuts: Vec<Shortcut<'a>>,
shortcuts: &Vec<ShortcutOwned>,
search: &mut CachedSearch<'b>,
client: &Client,
) -> Result<(), Box<dyn Error>> {
@@ -31,7 +31,7 @@ pub async fn download_images<'a, 'b>(
}
let mut search_results = HashMap::new();
for s in shortcuts_to_search_for {
let search = search.search(s.app_id, s.app_name).await?;
let search = search.search(s.app_id, &s.app_name).await?;
if let Some(search) = search {
search_results.insert(s.app_id, search);
}