izzilis/src/config.rs

77 lines
2.1 KiB
Rust
Raw Normal View History

use core::fmt::Debug;
use serde::{Deserialize, Serialize};
use std::{error::Error, path::Path};
2021-07-06 02:39:09 +01:00
use telegram_bot::ChatId;
2021-07-06 02:39:09 +01:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
2021-07-06 02:39:09 +01:00
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,
2021-07-06 03:13:51 +01:00
pub post_buffer: u32,
pub publisher: Publisher,
2021-06-25 20:48:06 +01:00
}
2021-07-06 02:39:09 +01:00
#[derive(Serialize, Deserialize, Debug, Clone)]
2021-06-25 20:48:06 +01:00
pub struct MinMax {
2021-07-06 02:39:09 +01:00
pub min: u64,
pub max: u64,
2021-06-25 20:48:06 +01:00
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Publisher {
2021-09-30 17:53:33 +01:00
Misskey(FediverseConfig<String, misskey::model::note::Visibility>),
Mastodon(FediverseConfig<Option<mammut::Data>, mammut::status_builder::Visibility>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
2021-09-30 17:53:33 +01:00
pub struct FediverseConfig<T, V> {
pub base_url: String,
pub token: T,
2021-09-30 17:53:33 +01:00
pub visibility: V,
}
2021-07-06 02:39:09 +01:00
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"),
2021-06-25 20:48:06 +01:00
gpt_code_path: String::from("./gpt/"),
interval_seconds: MinMax {
min: 60 * 30,
max: 60 * 90,
},
2021-07-06 03:13:51 +01:00
post_buffer: 5,
2021-07-06 02:39:09 +01:00
bot_token: "".to_owned(),
chat_ref: ChatId::new(0),
publisher: Publisher::Misskey(FediverseConfig {
base_url: "".to_string(),
token: "".to_string(),
2021-09-30 17:53:33 +01:00
visibility: misskey::model::note::Visibility::Public,
}),
}
}
2021-07-06 02:39:09 +01:00
}
2021-07-06 02:39:09 +01:00
impl Config {
pub fn from<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
let file_bytes = std::fs::read(path)?;
2021-07-06 02:39:09 +01:00
Ok(serde_json::from_slice(&file_bytes)?)
}
2021-07-06 02:39:09 +01:00
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>> {
let cfg_json = serde_json::to_vec(self)?;
std::fs::write(path, &cfg_json)?;
2021-06-25 20:48:06 +01:00
2021-07-06 02:39:09 +01:00
Ok(())
2021-06-25 20:48:06 +01:00
}
}