use std::{collections::HashMap, fs, path::PathBuf};
use crate::{Result, types::ComponentType};
use super::{registry_path, utils, xml};
#[derive(Debug, Clone)]
pub(crate) struct RegistryEntry {
pub(crate) name: String,
pub(crate) version: String,
pub(crate) installed_path: PathBuf,
pub(crate) release_date: String,
}
pub(crate) struct RegistryManager {
file_path: PathBuf,
}
impl RegistryManager {
pub(crate) fn for_component_type(component_type: ComponentType) -> Option<Self> {
registry_path(component_type).map(|file_path| Self { file_path })
}
pub(crate) fn read_entries(&self) -> Result<Vec<RegistryEntry>> {
if !self.file_path.exists() {
return Ok(Vec::new());
}
let content = fs::read_to_string(&self.file_path)?;
Ok(xml::parse_registry_entries(&content))
}
pub(crate) fn load_entry_map(&self) -> HashMap<String, RegistryEntry> {
let Ok(entries) = self.read_entries() else {
return HashMap::new();
};
entries
.into_iter()
.filter_map(|e| {
let dir_name = utils::extract_directory_name(&e.installed_path)?;
Some((dir_name, e))
})
.collect()
}
}