use quick_xml::events::Event;
use quick_xml::reader::Reader;
use std::collections::HashMap;
use std::fs;
const XML_DATABASE: &str = "softwaredb.xml";
pub fn search_in_rom_database(rom_to_search: &str) -> Result<String, String> {
let content = fs::read_to_string(XML_DATABASE).expect("something went wrong reading the file");
let xml = content.as_str();
let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(true);
let mut buf = Vec::new();
let mut dict: HashMap<String, String> = HashMap::new();
let mut is_in_rom = false;
let mut is_in_megarom = false;
let mut is_in_hash = false;
let mut is_in_type = false;
let mut d_hash = String::new();
let mut d_type = String::new();
loop {
match reader.read_event_into(&mut buf) {
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
Ok(Event::Eof) => break,
Ok(Event::Start(e)) => match e.name().as_ref() {
b"rom" => is_in_rom = true,
b"megarom" => is_in_megarom = true,
b"hash" => is_in_hash = true,
b"type" => is_in_type = true,
_ => {}
},
Ok(Event::End(e)) => match e.name().as_ref() {
b"dump" => {
dict.insert(
d_hash.clone(),
if d_type.is_empty() {
"NORMAL".to_string()
} else {
d_type.clone()
},
);
d_hash.clear();
d_type.clear();
}
b"rom" => is_in_rom = false,
b"megarom" => is_in_megarom = false,
b"hash" => is_in_hash = false,
b"type" => is_in_type = false,
_ => {}
},
Ok(Event::Text(e)) => {
if is_in_rom && is_in_hash {
d_hash = e.unescape().unwrap().into_owned();
}
if is_in_megarom {
if is_in_hash {
d_hash = e.unescape().unwrap().into_owned();
} else if is_in_type {
d_type = e.unescape().unwrap().into_owned();
}
}
}
_ => (),
}
buf.clear();
}
if dict.contains_key(rom_to_search) {
return Ok(dict.get(rom_to_search).unwrap().clone());
}
Err("Not found".to_string())
}