chore(downloads): Ran cargo clippy & cargo fmt

Side note, I'm going to start using chore to declare these rather than refactor because I don't think that it actually qualifies

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-16 17:05:24 +11:00
parent 63c3cc1096
commit bd3deacf38
3 changed files with 38 additions and 36 deletions

View File

@ -207,7 +207,6 @@ impl GameDownloadAgent {
.build() .build()
.unwrap(); .unwrap();
pool.scope(move |scope| { pool.scope(move |scope| {
let contexts = self.contexts.lock().unwrap(); let contexts = self.contexts.lock().unwrap();

View File

@ -20,7 +20,7 @@ use super::{
Welcome to the download manager, the most overengineered, glorious piece of bullshit. Welcome to the download manager, the most overengineered, glorious piece of bullshit.
The download manager takes a queue of game_ids and their associated The download manager takes a queue of game_ids and their associated
GameDownloadAgents, and then, one-by-one, executes them. It provides an interface GameDownloadAgents, and then, one-by-one, executes them. It provides an interface
to interact with the currently downloading agent, and manage the queue. to interact with the currently downloading agent, and manage the queue.
@ -79,7 +79,7 @@ pub enum DownloadManagerSignal {
/// download and return /// download and return
Finish, Finish,
/// Any error which occurs in the agent /// Any error which occurs in the agent
Error(GameDownloadError) Error(GameDownloadError),
} }
pub enum DownloadManagerStatus { pub enum DownloadManagerStatus {
Downloading, Downloading,
@ -128,7 +128,6 @@ impl DownloadManager {
match signal { match signal {
DownloadManagerSignal::Go => { DownloadManagerSignal::Go => {
self.manage_go_signal(); self.manage_go_signal();
} }
DownloadManagerSignal::Stop => { DownloadManagerSignal::Stop => {
self.manage_stop_signal(); self.manage_stop_signal();
@ -140,17 +139,14 @@ impl DownloadManager {
self.manage_queue_signal(game_id, version, target_download_dir); self.manage_queue_signal(game_id, version, target_download_dir);
} }
DownloadManagerSignal::Finish => { DownloadManagerSignal::Finish => {
match self.active_control_flag { if let Some(active_control_flag) = self.active_control_flag {
Some(active_control_flag) => { active_control_flag.set(DownloadThreadControlFlag::Stop)
active_control_flag.set(DownloadThreadControlFlag::Stop)
}
None => {}
} }
return Ok(()); return Ok(());
} }
DownloadManagerSignal::Error(game_download_error) => { DownloadManagerSignal::Error(game_download_error) => {
self.manage_error_signal(game_download_error); self.manage_error_signal(game_download_error);
}, }
}; };
} }
} }
@ -171,18 +167,13 @@ impl DownloadManager {
self.download_queue.lock().unwrap().pop_front(); self.download_queue.lock().unwrap().pop_front();
self.download_agent_registry.remove(&game_id); self.download_agent_registry.remove(&game_id);
self.active_control_flag = None; self.active_control_flag = None;
*self.progress.lock().unwrap() = None; *self.progress.lock().unwrap() = None;
} }
} }
self.sender.send(DownloadManagerSignal::Go).unwrap(); self.sender.send(DownloadManagerSignal::Go).unwrap();
} }
fn manage_queue_signal( fn manage_queue_signal(&mut self, id: String, version: String, target_download_dir: usize) {
&mut self,
id: String,
version: String,
target_download_dir: usize,
) {
info!("Got signal Queue"); info!("Got signal Queue");
let download_agent = Arc::new(GameDownloadAgent::new( let download_agent = Arc::new(GameDownloadAgent::new(
id.clone(), id.clone(),
@ -196,7 +187,10 @@ impl DownloadManager {
}); });
self.download_agent_registry self.download_agent_registry
.insert(interface_data.id.clone(), download_agent); .insert(interface_data.id.clone(), download_agent);
self.download_queue.lock().unwrap().push_back(interface_data); self.download_queue
.lock()
.unwrap()
.push_back(interface_data);
} }
fn manage_go_signal(&mut self) { fn manage_go_signal(&mut self) {
@ -210,7 +204,8 @@ impl DownloadManager {
.unwrap() .unwrap()
.clone() .clone()
}; };
let download_agent_interface = Arc::new(AgentInterfaceData::from(download_agent.clone())); let download_agent_interface =
Arc::new(AgentInterfaceData::from(download_agent.clone()));
self.current_game_interface = Some(download_agent_interface); self.current_game_interface = Some(download_agent_interface);
let progress_object = download_agent.progress.clone(); let progress_object = download_agent.progress.clone();
@ -224,12 +219,8 @@ impl DownloadManager {
info!("Spawning download"); info!("Spawning download");
spawn(move || { spawn(move || {
let signal = match download_agent.download() { let signal = match download_agent.download() {
Ok(_) => { Ok(_) => DownloadManagerSignal::Completed(download_agent.id.clone()),
DownloadManagerSignal::Completed(download_agent.id.clone()) Err(e) => DownloadManagerSignal::Error(e),
},
Err(e) => {
DownloadManagerSignal::Error(e)
},
}; };
sender.send(signal).unwrap(); sender.send(signal).unwrap();
}); });

View File

@ -1,23 +1,30 @@
use std::{ use std::{
any::Any, collections::VecDeque, sync::{ any::Any,
collections::VecDeque,
sync::{
mpsc::{SendError, Sender}, mpsc::{SendError, Sender},
Arc, Mutex, MutexGuard, Arc, Mutex, MutexGuard,
}, thread::JoinHandle },
thread::JoinHandle,
}; };
use log::info; use log::info;
use super::{download_agent::GameDownloadAgent, download_manager::{DownloadManagerSignal, DownloadManagerStatus, GameDownloadStatus}, progress_object::ProgressObject}; use super::{
download_agent::GameDownloadAgent,
download_manager::{DownloadManagerSignal, GameDownloadStatus},
progress_object::ProgressObject,
};
/// Accessible front-end for the DownloadManager /// Accessible front-end for the DownloadManager
/// ///
/// The system works entirely through signals, both internally and externally, /// The system works entirely through signals, both internally and externally,
/// all of which are accessible through the DownloadManagerSignal type, but /// all of which are accessible through the DownloadManagerSignal type, but
/// should not be used directly. Rather, signals are abstracted through this /// should not be used directly. Rather, signals are abstracted through this
/// interface. /// interface.
/// ///
/// The actual download queue may be accessed through the .edit() function, /// The actual download queue may be accessed through the .edit() function,
/// which provides raw access to the underlying queue. /// which provides raw access to the underlying queue.
/// THIS EDITING IS BLOCKING!!! /// THIS EDITING IS BLOCKING!!!
pub struct DownloadManagerInterface { pub struct DownloadManagerInterface {
terminator: JoinHandle<Result<(), ()>>, terminator: JoinHandle<Result<(), ()>>,
@ -33,7 +40,7 @@ impl From<Arc<GameDownloadAgent>> for AgentInterfaceData {
fn from(value: Arc<GameDownloadAgent>) -> Self { fn from(value: Arc<GameDownloadAgent>) -> Self {
Self { Self {
id: value.id.clone(), id: value.id.clone(),
status: Mutex::from(GameDownloadStatus::Uninitialised) status: Mutex::from(GameDownloadStatus::Uninitialised),
} }
} }
} }
@ -102,15 +109,20 @@ impl DownloadManagerInterface {
pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> { pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.command_sender.send(DownloadManagerSignal::Go) self.command_sender.send(DownloadManagerSignal::Go)
} }
pub fn ensure_terminated(self) -> Result<Result<(),()>, Box<dyn Any + Send>> { pub fn ensure_terminated(self) -> Result<Result<(), ()>, Box<dyn Any + Send>> {
self.command_sender.send(DownloadManagerSignal::Finish).unwrap(); self.command_sender
.send(DownloadManagerSignal::Finish)
.unwrap();
self.terminator.join() self.terminator.join()
} }
} }
/// Takes in the locked value from .edit() and attempts to /// Takes in the locked value from .edit() and attempts to
/// get the index of whatever game_id is passed in /// get the index of whatever game_id is passed in
fn get_index_from_id(queue: &mut MutexGuard<'_, VecDeque<Arc<AgentInterfaceData>>>, id: String) -> Option<usize> { fn get_index_from_id(
queue: &mut MutexGuard<'_, VecDeque<Arc<AgentInterfaceData>>>,
id: String,
) -> Option<usize> {
queue queue
.iter() .iter()
.position(|download_agent| download_agent.id == id) .position(|download_agent| download_agent.id == id)