From 3988c13bf08963a8ac5d554d6849a6ce3d1b77e2 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Mon, 19 Jun 2023 22:09:30 +0100 Subject: [PATCH] Did some clippy things and wad.rs is being developed --- src/config.rs | 14 ++------------ src/infos.rs | 15 ++++++++------- src/main.rs | 13 +++++++++---- src/wad.rs | 6 ++++-- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0131d17..d9188da 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,28 +4,18 @@ use std::path::PathBuf; use toml; use directories; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] pub struct Config { pub iwads: Option>, pub pwads: Option>, pub launchers: Option>, } -impl Default for Config { - fn default() -> Self { - Config { - iwads: None, - pwads: None, - launchers: None, - } - } -} - pub fn load_config(path: &PathBuf) -> Result> { if !path.exists() { return Ok(Config::default()); } - let content = std::fs::read_to_string(&path)?; + let content = std::fs::read_to_string(path)?; Ok(toml::from_str(&content)?) } diff --git a/src/infos.rs b/src/infos.rs index f371214..fed5844 100644 --- a/src/infos.rs +++ b/src/infos.rs @@ -2,7 +2,9 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; use std::path::PathBuf; use std::slice::Iter; +use std::cmp::Ordering; +#[derive(Default)] pub struct CommandManager { pub launcher: Option, pub iwad: Option, @@ -122,6 +124,7 @@ pub struct LauncherInfo { pub name: String, } +#[derive(Default)] pub struct MultiManager { pub selectable: Vec, pub current_selected: Vec, @@ -168,13 +171,10 @@ impl MultiManager { self.selectable.remove(index); let mut new_selected: Vec = Vec::new(); for s in &self.current_selected { - println!("{}", s); - if *s == index { - continue - } else if *s > index { - new_selected.push(s - 1); - } else { - new_selected.push(*s); + match s.cmp(&index) { + Ordering::Equal => continue, + Ordering::Less => new_selected.push(*s), + Ordering::Greater => new_selected.push(s - 1), } } self.current_selected = new_selected; @@ -192,6 +192,7 @@ impl MultiManager { } } +#[derive(Default)] pub struct SingleManager { pub selectable: Vec, pub current_selected: Option, diff --git a/src/main.rs b/src/main.rs index 3e3db7b..0255a29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,7 @@ impl Default for RustDoomLauncher { impl RustDoomLauncher { // There must be a better way than this - maybe for the RustDoomLauncher to // have a config thing - fn get_config_file_stuff(&mut self) -> () { + fn get_config_file_stuff(&mut self) { let config = load_config(&self.config_filename).unwrap(); if let Some(iwads) = config.iwads { for iwad in iwads { @@ -90,7 +90,7 @@ impl RustDoomLauncher { } } - fn launch_doom(&self) -> () { + fn launch_doom(&self) { match self.command_manager.generate_command() { Err(e) => { let text = match e { @@ -106,7 +106,12 @@ impl RustDoomLauncher { }, Ok((launcher, command)) => { // Note to self, don't hit launch button when running from emacs - Command::new(launcher).args(command).spawn(); + let result = Command::new(launcher).args(command).spawn(); + match result { + Err(e) => panic!("{}", e), + Ok(_o) => () + } + } } } @@ -140,7 +145,7 @@ impl eframe::App for RustDoomLauncher { } fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { - if self.config_file_loaded == false { + if !self.config_file_loaded { self.get_config_file_stuff(); self.config_file_loaded = true; } diff --git a/src/wad.rs b/src/wad.rs index a96b8c2..cab704c 100644 --- a/src/wad.rs +++ b/src/wad.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; use std::fs::File; -use std::io::BufReader; +use std::io::{BufReader, Seek, SeekFrom}; use bincode; use serde::Deserialize; @@ -14,7 +14,7 @@ pub struct WadHeader { pub fn open_wad(path: &PathBuf) -> WadHeader { let mut file = BufReader::new(File::open(path).unwrap()); let header: WadHeader = bincode::deserialize_from(&mut file).unwrap(); - + file.seek(SeekFrom::Start(header.file_offset_to_start as u64)).unwrap(); header } @@ -26,7 +26,9 @@ mod tests { #[test] fn test_open_wad() { let freedoom_iwad = PathBuf::from("freedoom1.wad"); + assert!(freedoom_iwad.exists(), "Needs freedoom1.wad - get it from here https://freedoom.github.io"); let ow = open_wad(&freedoom_iwad); + assert_eq!(&ow.identifier, b"IWAD"); assert_eq!(std::str::from_utf8(&ow.identifier).unwrap(), "IWAD"); } }