Did some clippy things and wad.rs is being developed

This commit is contained in:
2023-06-19 22:09:30 +01:00
parent d9b51c254c
commit 3988c13bf0
4 changed files with 23 additions and 25 deletions

View File

@@ -4,28 +4,18 @@ use std::path::PathBuf;
use toml; use toml;
use directories; use directories;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Default)]
pub struct Config { pub struct Config {
pub iwads: Option<Vec<WadInfo>>, pub iwads: Option<Vec<WadInfo>>,
pub pwads: Option<Vec<WadInfo>>, pub pwads: Option<Vec<WadInfo>>,
pub launchers: Option<Vec<LauncherInfo>>, pub launchers: Option<Vec<LauncherInfo>>,
} }
impl Default for Config {
fn default() -> Self {
Config {
iwads: None,
pwads: None,
launchers: None,
}
}
}
pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> { pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
if !path.exists() { if !path.exists() {
return Ok(Config::default()); 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)?) Ok(toml::from_str(&content)?)
} }

View File

@@ -2,7 +2,9 @@ use serde_derive::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::slice::Iter; use std::slice::Iter;
use std::cmp::Ordering;
#[derive(Default)]
pub struct CommandManager { pub struct CommandManager {
pub launcher: Option<PathBuf>, pub launcher: Option<PathBuf>,
pub iwad: Option<PathBuf>, pub iwad: Option<PathBuf>,
@@ -122,6 +124,7 @@ pub struct LauncherInfo {
pub name: String, pub name: String,
} }
#[derive(Default)]
pub struct MultiManager<T> { pub struct MultiManager<T> {
pub selectable: Vec<T>, pub selectable: Vec<T>,
pub current_selected: Vec<usize>, pub current_selected: Vec<usize>,
@@ -168,13 +171,10 @@ impl<T: Clone> MultiManager<T> {
self.selectable.remove(index); self.selectable.remove(index);
let mut new_selected: Vec<usize> = Vec::new(); let mut new_selected: Vec<usize> = Vec::new();
for s in &self.current_selected { for s in &self.current_selected {
println!("{}", s); match s.cmp(&index) {
if *s == index { Ordering::Equal => continue,
continue Ordering::Less => new_selected.push(*s),
} else if *s > index { Ordering::Greater => new_selected.push(s - 1),
new_selected.push(s - 1);
} else {
new_selected.push(*s);
} }
} }
self.current_selected = new_selected; self.current_selected = new_selected;
@@ -192,6 +192,7 @@ impl<T: Clone> MultiManager<T> {
} }
} }
#[derive(Default)]
pub struct SingleManager<T> { pub struct SingleManager<T> {
pub selectable: Vec<T>, pub selectable: Vec<T>,
pub current_selected: Option<usize>, pub current_selected: Option<usize>,

View File

@@ -71,7 +71,7 @@ impl Default for RustDoomLauncher {
impl RustDoomLauncher { impl RustDoomLauncher {
// There must be a better way than this - maybe for the RustDoomLauncher to // There must be a better way than this - maybe for the RustDoomLauncher to
// have a config thing // 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(); let config = load_config(&self.config_filename).unwrap();
if let Some(iwads) = config.iwads { if let Some(iwads) = config.iwads {
for iwad in 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() { match self.command_manager.generate_command() {
Err(e) => { Err(e) => {
let text = match e { let text = match e {
@@ -106,7 +106,12 @@ impl RustDoomLauncher {
}, },
Ok((launcher, command)) => { Ok((launcher, command)) => {
// Note to self, don't hit launch button when running from emacs // 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) { 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.get_config_file_stuff();
self.config_file_loaded = true; self.config_file_loaded = true;
} }

View File

@@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::{BufReader, Seek, SeekFrom};
use bincode; use bincode;
use serde::Deserialize; use serde::Deserialize;
@@ -14,7 +14,7 @@ pub struct WadHeader {
pub fn open_wad(path: &PathBuf) -> WadHeader { pub fn open_wad(path: &PathBuf) -> WadHeader {
let mut file = BufReader::new(File::open(path).unwrap()); let mut file = BufReader::new(File::open(path).unwrap());
let header: WadHeader = bincode::deserialize_from(&mut file).unwrap(); let header: WadHeader = bincode::deserialize_from(&mut file).unwrap();
file.seek(SeekFrom::Start(header.file_offset_to_start as u64)).unwrap();
header header
} }
@@ -26,7 +26,9 @@ mod tests {
#[test] #[test]
fn test_open_wad() { fn test_open_wad() {
let freedoom_iwad = PathBuf::from("freedoom1.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); let ow = open_wad(&freedoom_iwad);
assert_eq!(&ow.identifier, b"IWAD");
assert_eq!(std::str::from_utf8(&ow.identifier).unwrap(), "IWAD"); assert_eq!(std::str::from_utf8(&ow.identifier).unwrap(), "IWAD");
} }
} }