summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-08 11:27:51 +0800
committerGitHub <noreply@github.com>2023-03-08 11:27:51 +0800
commitb7cb6f89f137e20f1d6dbb1f66e40f319c978a45 (patch)
tree85ebf416d8ce8b2bbb808237578dca5b5be712e2 /src/config.rs
parentb05fce7bca0e0e69851dc609eb527956304defcf (diff)
downloadaichat-b7cb6f89f137e20f1d6dbb1f66e40f319c978a45.tar.gz
fix: repl set save true not work if not rerun(#34)
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs34
1 files changed, 14 insertions, 20 deletions
diff --git a/src/config.rs b/src/config.rs
index dc07c95..8ac99b7 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -103,29 +103,14 @@ impl Config {
Ok(path)
}
- pub fn open_message_file(&self) -> Result<Option<File>> {
+ pub fn save_message(&self, input: &str, output: &str) -> Result<()> {
if !self.save {
- return Ok(None);
+ return Ok(());
}
- let path = Config::messages_file()?;
- let file: Option<File> = if self.save {
- let file = OpenOptions::new()
- .create(true)
- .append(true)
- .open(&path)
- .with_context(|| format!("Failed to create/append {}", path.display()))?;
- Some(file)
- } else {
- None
- };
- Ok(file)
- }
-
- pub fn save_message(&self, file: Option<&mut File>, input: &str, output: &str) -> Result<()> {
- if output.is_empty() || !self.save || file.is_none() {
+ let mut file = self.open_message_file()?;
+ if output.is_empty() || !self.save {
return Ok(());
}
- let file = file.unwrap();
let timestamp = now();
let output = match self.role.as_ref() {
None => {
@@ -261,7 +246,7 @@ impl Config {
match key {
"api_key" => {
if unset {
- return Ok("Not allowed".into());
+ return Ok("Error: Not allowed".into());
} else {
self.api_key = value.to_string();
}
@@ -298,6 +283,15 @@ impl Config {
Ok("".into())
}
+ fn open_message_file(&self) -> Result<File> {
+ let path = Config::messages_file()?;
+ OpenOptions::new()
+ .create(true)
+ .append(true)
+ .open(&path)
+ .with_context(|| format!("Failed to create/append {}", path.display()))
+ }
+
fn load_roles(&mut self) -> Result<()> {
let path = Self::roles_file()?;
if !path.exists() {