izzilis/src/config.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

2021-07-06 02:39:09 +01:00
use std::{error::Error, path::Path};
use serde::{Deserialize, Serialize};
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 fediverse_base_url: String,
pub interval_seconds: MinMax,
pub bot_token: String,
pub chat_ref: ChatId,
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
}
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/"),
fediverse_base_url: String::from("https://lain.com"),
2021-06-25 20:48:06 +01:00
interval_seconds: MinMax {
min: 60 * 30,
max: 60 * 90,
},
2021-07-06 02:39:09 +01:00
bot_token: "".to_owned(),
chat_ref: ChatId::new(0),
}
}
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
}
}