izzilis/src/config.rs

76 lines
2.0 KiB
Rust

use std::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
python_path: String,
model_name: String,
temperature: String,
top_k: String,
gpt_code_path: String,
fediverse_base_url: String,
}
impl Config {
pub fn default() -> Config {
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("."),
fediverse_base_url: String::from("https://lain.com"),
}
}
pub fn from(path: String) -> Result<Config, Box<dyn Error>> {
let file_bytes = std::fs::read(path)?;
match serde_json::from_slice(&file_bytes) {
Ok(res) => Ok(res),
Err(err) => Err(Box::new(err)),
}
}
pub fn save(&self, path: String) -> Option<Box<dyn Error>> {
let cfg_json = match serde_json::to_vec(self) {
Ok(res) => res,
Err(err) => return Some(Box::new(err)),
};
match std::fs::write(path, &cfg_json) {
Ok(_) => None,
Err(err) => Some(Box::new(err)),
}
}
/// Get a reference to the config's python path.
pub fn python_path(&self) -> String {
self.python_path.clone()
}
/// Get a reference to the config's model name.
pub fn model_name(&self) -> String {
self.model_name.clone()
}
/// Get a reference to the config's temperature.
pub fn temperature(&self) -> String {
self.temperature.clone()
}
/// Get a reference to the config's top k.
pub fn top_k(&self) -> String {
self.top_k.clone()
}
/// Get a reference to the config's gpt code path.
pub fn gpt_code_path(&self) -> String {
self.gpt_code_path.clone()
}
pub fn fediverse_base_url(&self) -> String {
self.fediverse_base_url.clone()
}
}