use std::collections::HashMap;
use error_stack::{Result, ResultExt};
use thiserror::Error;
use crate::prompt::model::{Config, Content, Info, Template};
use crate::utils::common::load_toml::load_toml;
#[derive(Debug, Error)]
pub enum PromptLoadError {
#[error("Failed to load config")]
ConfigLoadError,
#[error("Failed to load template")]
TemplateLoadError,
#[error("Failed to load content for {0}")]
ContentLoadError(String),
}
pub fn load() -> Result<(Template, HashMap<Info, Content>), PromptLoadError> {
let config: Config = load_toml("data/prompts/config.toml")
.change_context(PromptLoadError::ConfigLoadError)?;
let template: Template = load_toml(&config.template_path)
.change_context(PromptLoadError::TemplateLoadError)?;
let mut info_with_contents = HashMap::with_capacity(config.prompt_info.len());
for info in &config.prompt_info {
let content: Content = load_toml(&info.path)
.change_context_lazy(|| PromptLoadError::ContentLoadError(info.name.clone()))?;
info_with_contents.insert(info.clone(), content);
}
Ok((template, info_with_contents))
}
#[deprecated(since = "next_version", note = "请使用返回Result的load函数代替")]
pub fn load_unchecked() -> (Template, HashMap<Info, Content>) {
let config: Config = load_toml("data/prompts/config.toml")
.expect("Failed to load config.toml");
let template: Template = load_toml(&config.template_path)
.expect(&format!("Failed to load template from {}", &config.template_path));
let mut info_with_contents = HashMap::with_capacity(config.prompt_info.len());
for info in &config.prompt_info {
let content: Content = load_toml(&info.path)
.expect(&format!("Failed to load content from {}", &info.path));
info_with_contents.insert(info.clone(), content);
}
(template, info_with_contents)
}