use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf}; use crate::core::manifest::{ComponentConfig, Manifest};
#[derive(Clone, Debug)]
pub struct ComponentAsset {
pub template: String,
pub style: Option<String>,
}
pub struct AssetLoader {
components: HashMap<String, ComponentAsset>,
}
impl AssetLoader {
pub fn new(app_path: &Path, manifest: &Manifest) -> Result<Self, String> {
let mut components = HashMap::new();
let components_path = app_path.join("components");
for (name, config) in &manifest.components {
let (template_path, style_path): (PathBuf, Option<PathBuf>) = match config {
ComponentConfig::Simple(template_file) => {
(components_path.join(template_file), None)
},
ComponentConfig::Detailed { template, style } => {
(
components_path.join(template),
style.as_ref().map(|style_file| components_path.join(style_file))
)
}
};
let template = fs::read_to_string(&template_path)
.map_err(|e| format!("Failed to read template for component '{}' at {:?}: {}", name, template_path, e))?;
let style = match style_path {
Some(path) => Some(fs::read_to_string(&path)
.map_err(|e| format!("Failed to read style for component '{}' at {:?}: {}", name, path, e))?),
None => None,
};
components.insert(name.clone(), ComponentAsset { template, style });
}
Ok(Self { components })
}
pub fn get_component(&self, name: &str) -> Option<&ComponentAsset> {
self.components.get(name)
}
}