Skip to content

Commit

Permalink
More refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
BezPowell committed Aug 25, 2022
1 parent 9816e9d commit a63100a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 36 deletions.
File renamed without changes.
18 changes: 15 additions & 3 deletions src/lib/dat/mod.rs → src/lib/input/dat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod game;
mod rom;

use std::io::Read;
use std::{collections::HashMap, io::Read, str::FromStr};

pub use game::Game;
pub use rom::Rom;
use serde_derive::Deserialize;
use serde_xml_rs::{from_str, Error};

use crate::lib::verify::hash::Checksum;

/// Contains a list of games,
/// and the checksums for their roms.
#[derive(Debug, Deserialize, PartialEq)]
Expand All @@ -28,8 +30,18 @@ impl Datafile {
Ok(dat)
}

pub fn games(&self) -> &Vec<Game> {
&self.games
/// Builds a Hashmap to check files against.
/// This is an intermediary type to allow
/// rapid checking.
pub fn build_test_set(&self) -> HashMap<Checksum, (&Rom, &Game)> {
let mut set = HashMap::new();
for game in &self.games {
for rom in game.roms() {
set.insert(Checksum::from_str(rom.hash()).unwrap(), (rom, game));
}
}

set
}
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/files.rs → src/lib/input/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl File {
mod tests {
use std::{fs, path::PathBuf, str::FromStr};

use crate::lib::{files::File, verify::hash::Checksum};
use crate::lib::{input::File, verify::hash::Checksum};

#[test]
fn can_read_rom() {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod dat;
mod files;

pub use dat::{Datafile, Game, Rom};
pub use files::File;
22 changes: 3 additions & 19 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use self::dat::{Datafile, Game, Rom};
use self::files::File;
use self::input::*;
use self::result::ResultSet;
use self::verify::hash::Checksum;
use std::str::FromStr;
use std::{collections::HashMap, error::Error};

mod dat;
mod files;
mod input;
mod result;
mod verify;

Expand All @@ -33,7 +31,7 @@ impl App {
/// Verifies rom files against the datafile.
///
pub fn verify(&self) -> ResultSet {
let test_set = self.build_test_set();
let test_set = self.dat.build_test_set();
let mut results = ResultSet::new();

// Verify roms
Expand All @@ -46,18 +44,4 @@ impl App {

results
}

/// Builds a Hashmap to check files against.
/// This is an intermediary type to allow
/// rapid checking.
fn build_test_set(&self) -> HashMap<Checksum, (&Rom, &Game)> {
let mut set = HashMap::new();
for game in self.dat.games() {
for rom in game.roms() {
set.insert(Checksum::from_str(rom.hash()).unwrap(), (rom, game));
}
}

set
}
}
41 changes: 41 additions & 0 deletions src/lib/result/game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::HashMap;

use crate::lib::{
input::{File, Game, Rom},
verify::VerifiedStatus,
};

#[derive(Debug)]
pub struct GameResult<'a> {
roms: HashMap<&'a str, VerifiedStatus<'a>>,
}

impl<'a> GameResult<'a> {
/// Build a new game result from the specified string.
pub fn new(game: &'a Game) -> GameResult<'a> {
let mut roms = HashMap::with_capacity(game.roms().len());
for rom in game.roms() {
roms.insert(rom.name(), VerifiedStatus::Missing);
}

GameResult { roms }
}

/// Add a file to the given
pub fn add_file(&mut self, file: &'a File, rom: &'a Rom) {
let status = if file.name().unwrap() == rom.name() {
VerifiedStatus::Verified { file }
} else {
VerifiedStatus::MatchNotName {
file: file,
correct_name: rom.name(),
}
};

self.roms.insert(rom.name(), status);
}

pub fn roms(&self) -> &HashMap<&'a str, VerifiedStatus<'a>> {
&self.roms
}
}
5 changes: 5 additions & 0 deletions src/lib/result/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod game;
mod set;

pub use game::GameResult;
pub use set::ResultSet;
19 changes: 7 additions & 12 deletions src/lib/result.rs → src/lib/result/set.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::{collections::HashMap, hash::Hash};

use super::{
dat::{Game, Rom},
files::File,
verify::VerifiedStatus,
};
use crate::lib::input::{File, Game, Rom};
use crate::lib::verify::VerifiedStatus;
use std::collections::HashMap;

#[derive(Debug)]
pub struct ResultSet<'a> {
matched: HashMap<String, GameResult<'a>>,
matched: HashMap<&'a str, GameResult<'a>>,
unmatched: Vec<&'a File>,
}

Expand All @@ -20,7 +16,7 @@ impl<'a> ResultSet<'a> {
}
}

pub fn matches(&self) -> &HashMap<String, GameResult<'a>> {
pub fn matches(&self) -> &HashMap<&str, GameResult<'a>> {
&self.matched
}

Expand All @@ -31,8 +27,7 @@ impl<'a> ResultSet<'a> {
pub fn add_match(&mut self, file: &'a File, game: &'a Game, rom: &'a Rom) {
// Create new entry if none exists.
if !self.matched.contains_key(game.name()) {
self.matched
.insert(game.name().to_string(), GameResult::new(game));
self.matched.insert(game.name(), GameResult::new(game));
}

// Update existing entry
Expand Down Expand Up @@ -77,6 +72,6 @@ impl<'a> GameResult<'a> {
}

pub fn roms(&self) -> &HashMap<String, VerifiedStatus<'a>> {
&self.roms()
&self.roms
}
}
2 changes: 1 addition & 1 deletion src/lib/verify/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::files::File;
use crate::lib::input::File;
pub mod hash;

#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit a63100a

Please sign in to comment.