chore: General cleanup

- Changed some info!() statements to debug!() and warn!()
- Removed most Turbofish syntax cases
- Removed InvalidCodeError and replaced it with InvalidResponse

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-06 20:04:34 +11:00
parent 50f37fd022
commit 182361e598
10 changed files with 47 additions and 55 deletions

View File

@ -1,7 +1,7 @@
use std::{env, sync::Mutex}; use std::{env, sync::Mutex};
use chrono::Utc; use chrono::Utc;
use log::{info, warn}; use log::{debug, error, info, warn};
use openssl::{ec::EcKey, hash::MessageDigest, pkey::PKey, sign::Signer}; use openssl::{ec::EcKey, hash::MessageDigest, pkey::PKey, sign::Signer};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tauri::{AppHandle, Emitter, Manager}; use tauri::{AppHandle, Emitter, Manager};
@ -74,17 +74,17 @@ pub fn fetch_user() -> Result<User, RemoteAccessError> {
.send()?; .send()?;
if response.status() != 200 { if response.status() != 200 {
let data = response.json::<DropServerError>()?; let err: DropServerError = response.json().unwrap();
info!("Could not fetch user: {}", data.status_message); warn!("{:?}", err);
if data.status_message == "Nonce expired" { if err.status_message == "Nonce expired" {
return Err(RemoteAccessError::OutOfSync); return Err(RemoteAccessError::OutOfSync);
} }
return Err(RemoteAccessError::InvalidCodeError(0)); return Err(RemoteAccessError::InvalidResponse(err));
} }
let user = response.json::<User>()?; let user = response.json()?;
Ok(user) Ok(user)
} }
@ -113,8 +113,8 @@ fn recieve_handshake_logic(app: &AppHandle, path: String) -> Result<(), RemoteAc
let endpoint = base_url.join("/api/v1/client/auth/handshake")?; let endpoint = base_url.join("/api/v1/client/auth/handshake")?;
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let response = client.post(endpoint).json(&body).send()?; let response = client.post(endpoint).json(&body).send()?;
info!("{}", response.status().as_u16()); debug!("Handshake responsded with {}", response.status().as_u16());
let response_struct = response.json::<HandshakeResponse>()?; let response_struct: HandshakeResponse = response.json()?;
{ {
let mut handle = DB.borrow_data_mut().unwrap(); let mut handle = DB.borrow_data_mut().unwrap();
@ -173,8 +173,8 @@ fn auth_initiate_wrapper() -> Result<(), RemoteAccessError> {
let response = client.post(endpoint.to_string()).json(&body).send()?; let response = client.post(endpoint.to_string()).json(&body).send()?;
if response.status() != 200 { if response.status() != 200 {
let data = response.json::<DropServerError>()?; let data: DropServerError = response.json()?;
info!("Could not start handshake: {}", data.status_message); error!("Could not start handshake: {}", data.status_message);
return Err(RemoteAccessError::HandshakeFailed(data.status_message)); return Err(RemoteAccessError::HandshakeFailed(data.status_message));
} }
@ -182,7 +182,7 @@ fn auth_initiate_wrapper() -> Result<(), RemoteAccessError> {
let redir_url = response.text()?; let redir_url = response.text()?;
let complete_redir_url = base_url.join(&redir_url)?; let complete_redir_url = base_url.join(&redir_url)?;
info!("opening web browser to continue authentication"); debug!("opening web browser to continue authentication");
webbrowser::open(complete_redir_url.as_ref()).unwrap(); webbrowser::open(complete_redir_url.as_ref()).unwrap();
Ok(()) Ok(())
@ -235,8 +235,6 @@ pub fn setup() -> Result<(AppStatus, Option<User>), ()> {
#[tauri::command] #[tauri::command]
pub fn sign_out(app: AppHandle) -> Result<(), String> { pub fn sign_out(app: AppHandle) -> Result<(), String> {
info!("Signing out user");
// Clear auth from database // Clear auth from database
{ {
let mut handle = DB.borrow_data_mut().unwrap(); let mut handle = DB.borrow_data_mut().unwrap();

View File

@ -1,5 +1,5 @@
use crate::DB; use crate::DB;
use log::info; use log::debug;
use tauri::AppHandle; use tauri::AppHandle;
use tauri_plugin_autostart::ManagerExt; use tauri_plugin_autostart::ManagerExt;
@ -8,10 +8,10 @@ pub async fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), Strin
let manager = app.autolaunch(); let manager = app.autolaunch();
if enabled { if enabled {
manager.enable().map_err(|e| e.to_string())?; manager.enable().map_err(|e| e.to_string())?;
info!("Enabled autostart"); debug!("Enabled autostart");
} else { } else {
manager.disable().map_err(|e| e.to_string())?; manager.disable().map_err(|e| e.to_string())?;
info!("Disabled autostart"); debug!("Disabled autostart");
} }
// Store the state in DB // Store the state in DB
@ -58,10 +58,10 @@ pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
if current_state != should_be_enabled { if current_state != should_be_enabled {
if should_be_enabled { if should_be_enabled {
manager.enable().map_err(|e| e.to_string())?; manager.enable().map_err(|e| e.to_string())?;
info!("Synced autostart: enabled"); debug!("Synced autostart: enabled");
} else { } else {
manager.disable().map_err(|e| e.to_string())?; manager.disable().map_err(|e| e.to_string())?;
info!("Synced autostart: disabled"); debug!("Synced autostart: disabled");
} }
} }

View File

@ -1,4 +1,4 @@
use log::info; use log::{debug, info};
use tauri::AppHandle; use tauri::AppHandle;
#[tauri::command] #[tauri::command]
@ -7,7 +7,7 @@ pub fn quit(app: tauri::AppHandle) {
} }
pub fn cleanup_and_exit(app: &AppHandle) { pub fn cleanup_and_exit(app: &AppHandle) {
info!("exiting drop application..."); debug!("Cleaning up and exiting application");
app.exit(0); app.exit(0);
} }

View File

@ -126,9 +126,7 @@ impl DatabaseImpls for DatabaseInterface {
debug!("Creating data directory at {:?}", data_root_dir); debug!("Creating data directory at {:?}", data_root_dir);
create_dir_all(data_root_dir.clone()).unwrap(); create_dir_all(data_root_dir.clone()).unwrap();
debug!("Creating games directory");
create_dir_all(games_base_dir.clone()).unwrap(); create_dir_all(games_base_dir.clone()).unwrap();
debug!("Creating logs directory");
create_dir_all(logs_root_dir.clone()).unwrap(); create_dir_all(logs_root_dir.clone()).unwrap();
let exists = fs::exists(db_path.clone()).unwrap(); let exists = fs::exists(db_path.clone()).unwrap();

View File

@ -15,6 +15,7 @@ use crate::DB;
use log::{debug, error, info}; use log::{debug, error, info};
use rayon::ThreadPoolBuilder; use rayon::ThreadPoolBuilder;
use slice_deque::SliceDeque; use slice_deque::SliceDeque;
use std::collections::HashMap;
use std::fs::{create_dir_all, File}; use std::fs::{create_dir_all, File};
use std::path::Path; use std::path::Path;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -157,7 +158,7 @@ impl GameDownloadAgent {
)); ));
} }
let manifest_download = response.json::<DropManifest>().unwrap(); let manifest_download: DropManifest = response.json().unwrap();
if let Ok(mut manifest) = self.manifest.lock() { if let Ok(mut manifest) = self.manifest.lock() {
*manifest = Some(manifest_download); *manifest = Some(manifest_download);

View File

@ -134,9 +134,10 @@ pub fn download_game_chunk(
.map_err(|e| ApplicationDownloadError::Communication(e.into()))?; .map_err(|e| ApplicationDownloadError::Communication(e.into()))?;
if response.status() != 200 { if response.status() != 200 {
warn!("{}", response.text().unwrap()); let err = response.json().unwrap();
warn!("{:?}", err);
return Err(ApplicationDownloadError::Communication( return Err(ApplicationDownloadError::Communication(
RemoteAccessError::InvalidCodeError(400), RemoteAccessError::InvalidResponse(err),
)); ));
} }
@ -150,7 +151,7 @@ pub fn download_game_chunk(
let content_length = response.content_length(); let content_length = response.content_length();
if content_length.is_none() { if content_length.is_none() {
error!("Recieved 0 length content from server"); warn!("Recieved 0 length content from server");
return Err(ApplicationDownloadError::Communication( return Err(ApplicationDownloadError::Communication(
RemoteAccessError::InvalidResponse(response.json().unwrap()), RemoteAccessError::InvalidResponse(response.json().unwrap()),
)); ));

View File

@ -5,7 +5,7 @@ use std::{
sync::Mutex, sync::Mutex,
}; };
use log::error; use log::{error, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_binary::binary_stream::Endian; use serde_binary::binary_stream::Endian;
@ -46,7 +46,7 @@ impl StoredManifest {
match serde_binary::from_vec::<StoredManifest>(s, Endian::Little) { match serde_binary::from_vec::<StoredManifest>(s, Endian::Little) {
Ok(manifest) => manifest, Ok(manifest) => manifest,
Err(e) => { Err(e) => {
error!("{}", e); warn!("{}", e);
StoredManifest::new(game_id, game_version, base_path) StoredManifest::new(game_id, game_version, base_path)
} }
} }

View File

@ -2,7 +2,7 @@ use std::fs::remove_dir_all;
use std::sync::Mutex; use std::sync::Mutex;
use std::thread::spawn; use std::thread::spawn;
use log::{error, info}; use log::{error, info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tauri::Emitter; use tauri::Emitter;
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
@ -91,10 +91,12 @@ fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
.send()?; .send()?;
if response.status() != 200 { if response.status() != 200 {
return Err(response.status().as_u16().into()); let err = response.json().unwrap();
warn!("{:?}", err);
return Err(RemoteAccessError::InvalidResponse(err));
} }
let games: Vec<Game> = response.json::<Vec<Game>>()?; let games: Vec<Game> = response.json()?;
let state = app.state::<Mutex<AppState>>(); let state = app.state::<Mutex<AppState>>();
let mut handle = state.lock().unwrap(); let mut handle = state.lock().unwrap();
@ -155,12 +157,12 @@ fn fetch_game_logic(
return Err(RemoteAccessError::GameNotFound); return Err(RemoteAccessError::GameNotFound);
} }
if response.status() != 200 { if response.status() != 200 {
return Err(RemoteAccessError::InvalidCodeError( let err = response.json().unwrap();
response.status().into(), warn!("{:?}", err);
)); return Err(RemoteAccessError::InvalidResponse(err));
} }
let game = response.json::<Game>()?; let game: Game = response.json()?;
state_handle.games.insert(id.clone(), game.clone()); state_handle.games.insert(id.clone(), game.clone());
let mut db_handle = DB.borrow_data_mut().unwrap(); let mut db_handle = DB.borrow_data_mut().unwrap();
@ -217,19 +219,19 @@ fn fetch_game_verion_options_logic(
.send()?; .send()?;
if response.status() != 200 { if response.status() != 200 {
return Err(RemoteAccessError::InvalidCodeError( let err = response.json().unwrap();
response.status().into(), warn!("{:?}", err);
)); return Err(RemoteAccessError::InvalidResponse(err));
} }
let data = response.json::<Vec<GameVersionOption>>()?; let data: Vec<GameVersionOption> = response.json()?;
let state_lock = state.lock().unwrap(); let state_lock = state.lock().unwrap();
let process_manager_lock = state_lock.process_manager.lock().unwrap(); let process_manager_lock = state_lock.process_manager.lock().unwrap();
let data = data let data: Vec<GameVersionOption> = data
.into_iter() .into_iter()
.filter(|v| process_manager_lock.valid_platform(&v.platform).unwrap()) .filter(|v| process_manager_lock.valid_platform(&v.platform).unwrap())
.collect::<Vec<GameVersionOption>>(); .collect();
drop(process_manager_lock); drop(process_manager_lock);
drop(state_lock); drop(state_lock);
@ -363,7 +365,7 @@ pub fn on_game_complete(
.header("Authorization", header) .header("Authorization", header)
.send()?; .send()?;
let data = response.json::<GameVersion>()?; let data: GameVersion = response.json()?;
let mut handle = DB.borrow_data_mut().unwrap(); let mut handle = DB.borrow_data_mut().unwrap();
handle handle

View File

@ -69,7 +69,6 @@ impl ProcessManager<'_> {
// I think if we just join the install_dir to whatever the user provides us, we'll be alright // I think if we just join the install_dir to whatever the user provides us, we'll be alright
// In future, we should have a separate field for executable name and it's arguments // In future, we should have a separate field for executable name and it's arguments
fn process_command(&self, install_dir: &String, raw_command: String) -> (PathBuf, Vec<String>) { fn process_command(&self, install_dir: &String, raw_command: String) -> (PathBuf, Vec<String>) {
// let command_components = raw_command.split(" ").collect::<Vec<&str>>();
let root = raw_command; let root = raw_command;
let install_dir = Path::new(install_dir); let install_dir = Path::new(install_dir);

View File

@ -15,7 +15,6 @@ use crate::{AppState, AppStatus, DB};
pub enum RemoteAccessError { pub enum RemoteAccessError {
FetchError(Arc<reqwest::Error>), FetchError(Arc<reqwest::Error>),
ParsingError(ParseError), ParsingError(ParseError),
InvalidCodeError(u16),
InvalidEndpoint, InvalidEndpoint,
HandshakeFailed(String), HandshakeFailed(String),
GameNotFound, GameNotFound,
@ -42,7 +41,6 @@ impl Display for RemoteAccessError {
RemoteAccessError::ParsingError(parse_error) => { RemoteAccessError::ParsingError(parse_error) => {
write!(f, "{}", parse_error) write!(f, "{}", parse_error)
} }
RemoteAccessError::InvalidCodeError(error) => write!(f, "Invalid HTTP code {}", error),
RemoteAccessError::InvalidEndpoint => write!(f, "Invalid drop endpoint"), RemoteAccessError::InvalidEndpoint => write!(f, "Invalid drop endpoint"),
RemoteAccessError::HandshakeFailed(message) => write!(f, "Failed to complete handshake: {}", message), RemoteAccessError::HandshakeFailed(message) => write!(f, "Failed to complete handshake: {}", message),
RemoteAccessError::GameNotFound => write!(f, "Could not find game on server"), RemoteAccessError::GameNotFound => write!(f, "Could not find game on server"),
@ -69,11 +67,6 @@ impl From<ParseError> for RemoteAccessError {
RemoteAccessError::ParsingError(err) RemoteAccessError::ParsingError(err)
} }
} }
impl From<u16> for RemoteAccessError {
fn from(err: u16) -> Self {
RemoteAccessError::InvalidCodeError(err)
}
}
impl std::error::Error for RemoteAccessError {} impl std::error::Error for RemoteAccessError {}
@ -92,7 +85,7 @@ struct DropHealthcheck {
app_name: String, app_name: String,
} }
async fn use_remote_logic<'a>( fn use_remote_logic<'a>(
url: String, url: String,
state: tauri::State<'_, Mutex<AppState<'a>>>, state: tauri::State<'_, Mutex<AppState<'a>>>,
) -> Result<(), RemoteAccessError> { ) -> Result<(), RemoteAccessError> {
@ -101,9 +94,9 @@ async fn use_remote_logic<'a>(
// Test Drop url // Test Drop url
let test_endpoint = base_url.join("/api/v1")?; let test_endpoint = base_url.join("/api/v1")?;
let response = reqwest::get(test_endpoint.to_string()).await?; let response = reqwest::blocking::get(test_endpoint.to_string())?;
let result = response.json::<DropHealthcheck>().await?; let result: DropHealthcheck = response.json()?;
if result.app_name != "Drop" { if result.app_name != "Drop" {
warn!("user entered drop endpoint that connected, but wasn't identified as Drop"); warn!("user entered drop endpoint that connected, but wasn't identified as Drop");
@ -124,11 +117,11 @@ async fn use_remote_logic<'a>(
} }
#[tauri::command] #[tauri::command]
pub async fn use_remote<'a>( pub fn use_remote<'a>(
url: String, url: String,
state: tauri::State<'_, Mutex<AppState<'a>>>, state: tauri::State<'_, Mutex<AppState<'a>>>,
) -> Result<(), String> { ) -> Result<(), String> {
let result = use_remote_logic(url, state).await; let result = use_remote_logic(url, state);
if result.is_err() { if result.is_err() {
return Err(result.err().unwrap().to_string()); return Err(result.err().unwrap().to_string());