use core::fmt::Debug; use serde::{Deserialize, Serialize}; use std::{error::Error, path::Path}; use telegram_bot::ChatId; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Config { pub python_path: String, pub model_name: String, pub temperature: String, pub top_k: String, pub gpt_code_path: String, pub interval_seconds: MinMax, pub bot_token: String, pub chat_ref: ChatId, pub post_buffer: u32, pub publisher: Publisher, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MinMax { pub min: u64, pub max: u64, } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum Publisher { Misskey(FediverseConfig), Mastodon(FediverseConfig, mammut::status_builder::Visibility>), } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct FediverseConfig { pub base_url: String, pub token: T, pub visibility: V, } impl Default for Config { fn default() -> Self { Config { python_path: String::from("/usr/bin/python3"), model_name: String::from("117M"), temperature: String::from("1"), top_k: String::from("40"), gpt_code_path: String::from("./gpt/"), interval_seconds: MinMax { min: 60 * 30, max: 60 * 90, }, post_buffer: 5, bot_token: "".to_owned(), chat_ref: ChatId::new(0), publisher: Publisher::Misskey(FediverseConfig { base_url: "".to_string(), token: "".to_string(), visibility: misskey::model::note::Visibility::Public, }), } } } impl Config { pub fn from>(path: P) -> Result> { let file_bytes = std::fs::read(path)?; Ok(serde_json::from_slice(&file_bytes)?) } pub fn save>(&self, path: P) -> Result<(), Box> { let cfg_json = serde_json::to_vec(self)?; std::fs::write(path, &cfg_json)?; Ok(()) } }