use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use toml::Table;
use crate::utils::{get_string_hashmap_from_value, get_vec_string_from_value, is_empty_table};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile {
#[serde(skip)]
pub name: String,
#[serde(skip_serializing_if = "is_empty_table")]
pub variables: Table,
pub dependencies: Vec<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub prompts: HashMap<String, String>,
}
impl Profile {
pub fn new(name: &str) -> Self {
Profile {
name: name.to_string(),
variables: Table::new(),
dependencies: Vec::new(),
prompts: HashMap::new(),
}
}
pub fn from_table(name: &str, table: &Table) -> anyhow::Result<Self> {
let variables = match table.get("variables") {
Some(var_block) => var_block
.as_table()
.ok_or_else(|| anyhow::anyhow!("The 'variables' field must be a table"))?
.clone(),
None => Table::new(),
};
let dependencies = get_vec_string_from_value(table.get("dependencies"))?;
let prompts = get_string_hashmap_from_value(table.get("prompts"))?;
Ok(Self {
name: name.to_string(),
variables,
dependencies,
prompts,
})
}
}