use super::builtin::{
create_cli_template, create_embedded_template, create_library_template, create_plugin_template,
create_wasm_template, create_web_service_template, create_workspace_template,
};
use super::manifest::{TemplateKind, TemplateManifest};
use crate::{Error, Result};
use std::collections::HashMap;
use std::path::PathBuf;
pub struct TemplateRegistry {
builtin: HashMap<String, BuiltinTemplate>,
custom: HashMap<String, PathBuf>,
}
pub struct BuiltinTemplate {
pub manifest: TemplateManifest,
pub files: HashMap<String, String>,
}
impl TemplateRegistry {
pub fn new() -> Self {
let mut registry = Self {
builtin: HashMap::new(),
custom: HashMap::new(),
};
registry
.builtin
.insert("cli-app".to_string(), create_cli_template());
registry
.builtin
.insert("embedded".to_string(), create_embedded_template());
registry
.builtin
.insert("library".to_string(), create_library_template());
registry
.builtin
.insert("plugin".to_string(), create_plugin_template());
registry
.builtin
.insert("wasm".to_string(), create_wasm_template());
registry
.builtin
.insert("web-service".to_string(), create_web_service_template());
registry
.builtin
.insert("workspace".to_string(), create_workspace_template());
registry
}
pub fn list_templates(&self) -> Vec<(&str, TemplateKind, &str)> {
let mut templates = Vec::new();
for (name, template) in &self.builtin {
templates.push((
name.as_str(),
template.manifest.kind,
template.manifest.description.as_str(),
));
}
templates.sort_by_key(|t| t.0);
templates
}
pub fn get_builtin(&self, name: &str) -> Option<&BuiltinTemplate> {
self.builtin.get(name)
}
pub fn register_custom(&mut self, name: String, path: PathBuf) -> Result<()> {
if self.builtin.contains_key(&name) {
return Err(Error::validation(format!(
"Cannot override built-in template: {}",
name
)));
}
if !path.exists() {
return Err(Error::validation(format!(
"Template path does not exist: {}",
path.display()
)));
}
self.custom.insert(name, path);
Ok(())
}
}
impl Default for TemplateRegistry {
fn default() -> Self {
Self::new()
}
}