use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RomEntry {
pub path: PathBuf,
pub display_name: String,
pub search_key: String,
pub mapper_label: String,
pub mapper: Option<u16>,
pub hardware: Option<String>,
pub crc: Option<String>,
pub recording_duration: Option<std::time::Duration>,
}
impl RomEntry {
pub fn hardware_label(&self) -> &str {
self.hardware.as_deref().unwrap_or("-")
}
pub fn crc_label(&self) -> &str {
self.crc.as_deref().unwrap_or("-")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_entry(mapper: Option<u16>, hardware: Option<&str>, crc: Option<&str>) -> RomEntry {
let display_name = "Test ROM".to_string();
let search_key = display_name.to_lowercase();
let mapper_label = mapper.map_or_else(|| "-".to_string(), |m| m.to_string());
RomEntry {
path: PathBuf::from("/roms/test.nes"),
display_name,
search_key,
mapper_label,
mapper,
hardware: hardware.map(str::to_string),
crc: crc.map(str::to_string),
recording_duration: None,
}
}
#[test]
fn test_hardware_label_present() {
let entry = make_entry(None, Some("NES NTSC"), None);
assert_eq!(entry.hardware_label(), "NES NTSC");
}
#[test]
fn test_hardware_label_absent() {
let entry = make_entry(None, None, None);
assert_eq!(entry.hardware_label(), "-");
}
#[test]
fn test_mapper_label_present() {
let entry = make_entry(Some(4), None, None);
assert_eq!(entry.mapper_label, "4");
}
#[test]
fn test_mapper_label_absent() {
let entry = make_entry(None, None, None);
assert_eq!(entry.mapper_label, "-");
}
#[test]
fn test_crc_label_present() {
let entry = make_entry(None, None, Some("DEADBEEF"));
assert_eq!(entry.crc_label(), "DEADBEEF");
}
#[test]
fn test_crc_label_absent() {
let entry = make_entry(None, None, None);
assert_eq!(entry.crc_label(), "-");
}
#[test]
fn test_recording_duration_none_by_default() {
let entry = make_entry(None, None, None);
assert!(entry.recording_duration.is_none());
}
#[test]
fn test_search_key_is_lowercase_display_name() {
let entry = make_entry(None, None, None);
assert_eq!(entry.search_key, "test rom");
}
}