summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-02 19:52:11 +0800
committersigoden <sigoden@gmail.com>2023-03-02 19:52:11 +0800
commitdfebab910debf8375f1b628c0e0494399e21cecb (patch)
treedd9b495b4ff21ab6d51e36234e094380202c01bd /src/config.rs
downloadaichat-dfebab910debf8375f1b628c0e0494399e21cecb.tar.gz
init commit
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..5f76315
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,44 @@
+use std::{fs::read_to_string, path::Path};
+
+use anyhow::{anyhow, Result};
+use serde::Deserialize;
+
+#[derive(Debug, Clone, Deserialize)]
+pub struct Config {
+ /// Openai api key
+ pub api_key: String,
+ /// Set proxy
+ pub proxy: Option<String>,
+ /// Used only for debugging
+ #[serde(default)]
+ pub dry_run: bool,
+ /// Predefined rules
+ #[serde(default)]
+ pub roles: Vec<Role>,
+}
+
+impl Config {
+ pub fn init(path: &Path) -> Result<Config> {
+ let content = read_to_string(path)
+ .map_err(|err| anyhow!("Failed to load config at {}, {err}", path.display()))?;
+ let config: Config =
+ toml::from_str(&content).map_err(|err| anyhow!("Invalid config, {err}"))?;
+ Ok(config)
+ }
+}
+
+#[derive(Debug, Clone, Deserialize)]
+pub struct Role {
+ /// Role name
+ pub name: String,
+ /// Prompt text send to ai for setting up a rule
+ pub prompt: String,
+ /// First sentense will append to prompt
+ pub first_sentense: String,
+}
+
+impl Role {
+ pub fn generate(&self, text: &str) -> String {
+ format!("{} {} {}", self.prompt, self.first_sentense, text)
+ }
+}