use serde_yaml;
use std::{fs::read_to_string, path::PathBuf};
#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
pub struct Config {
pub openai: OpenAi,
pub azure: Azure,
}
impl Config {
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
if let Ok(config_contents) = std::env::var("RUST_AI_CONFIG") {
return match serde_yaml::from_str(&config_contents) {
Ok(config) => Ok(config),
Err(e) => {
log::error!(target: "global", "Unable to parse config: {:?}", e);
Err(e.into())
}
};
} else {
let config_path = PathBuf::from("config.yml");
if !config_path.exists() {
return Err("`config.yml` doesn't exist!".into());
}
return if let Ok(config_contents) = read_to_string(config_path) {
match serde_yaml::from_str(&config_contents) {
Ok(config) => Ok(config),
Err(e) => {
log::error!(target: "global", "Unable to parse config: {:?}", e);
Err(e.into())
}
}
} else {
Err("Unable to read `config.yml`".into())
};
}
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
pub struct OpenAi {
pub api_key: String,
pub org_id: Option<String>,
pub base_endpoint: Option<String>,
}
impl OpenAi {
pub fn base_endpoint(&self) -> String {
self.base_endpoint
.clone()
.unwrap_or("https://api.openai.com".to_string())
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
pub struct Azure {
pub speech: AzureSpeech,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
pub struct AzureSpeech {
pub key: String,
pub region: String,
}