use std::fs;
use std::path::Path;
use crate::config::Config;
use crate::error::CatalogError;
pub(super) fn write(root: &Path, relative: &str, contents: &str) {
let path = root.join(relative);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).expect("create the fixture's parent directory");
}
fs::write(&path, contents).expect("write the fixture file");
}
pub(super) fn prompt_source(name: &str, description: &str) -> String {
format!(
"---\nname: {name}\ndescription: {description}\npromptforge: 1\n---\n\n\
# Title\n\n## Main\n\n```lua\nreturn args\n```\n"
)
}
pub(super) fn unparsable_source() -> &'static str {
"---\npromptforge: 1\nname: placeholder\n---\n\n## S\n\np\n"
}
pub(super) fn write_prompt(root: &Path, relative: &str, name: &str, description: &str) {
write(root, relative, &prompt_source(name, description));
}
pub(super) fn config_at(root: &Path, extra: &str) -> Config {
let prompts = toml_string(&root.display().to_string());
let toml = format!(
"[server]\ntoken = \"shared\"\n\n\
[gateway]\nurl = \"http://127.0.0.1:8081/v1\"\nkey = \"gw\"\n\n\
[paths]\nprompts = {prompts}\n\n{extra}",
);
Config::from_toml_str(&toml).expect("the fixture configuration parses")
}
pub(super) fn toml_string(value: &str) -> String {
toml::Value::String(value.to_owned()).to_string()
}
pub(super) fn fault_text(error: &CatalogError) -> String {
error.to_string()
}