summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/client.rs9
-rw-r--r--src/config.rs17
-rw-r--r--src/main.rs9
-rw-r--r--src/repl.rs14
-rw-r--r--src/utils.rs11
5 files changed, 25 insertions, 35 deletions
diff --git a/src/client.rs b/src/client.rs
index 3fe75dd..f2aef38 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,7 +1,7 @@
use crate::config::Config;
use crate::repl::ReplyReceiver;
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
use eventsource_stream::Eventsource;
use futures_util::StreamExt;
use reqwest::{Client, Proxy, RequestBuilder};
@@ -26,13 +26,12 @@ impl ChatGptClient {
pub fn init(config: Arc<Config>) -> Result<Self> {
let mut builder = Client::builder();
if let Some(proxy) = config.proxy.as_ref() {
- builder = builder
- .proxy(Proxy::all(proxy).map_err(|err| anyhow!("Invalid config.proxy, {err}"))?);
+ builder = builder.proxy(Proxy::all(proxy).with_context(|| "Invalid config.proxy")?);
}
let client = builder
.connect_timeout(CONNECT_TIMEOUT)
.build()
- .map_err(|err| anyhow!("Failed to init http client, {err}"))?;
+ .with_context(|| "Failed to init http client")?;
let runtime = init_runtime()?;
Ok(Self {
@@ -181,5 +180,5 @@ fn init_runtime() -> Result<Runtime> {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
- .map_err(|err| anyhow!("Failed to init tokio, {err}"))
+ .with_context(|| "Failed to init tokio")
}
diff --git a/src/config.rs b/src/config.rs
index 3a85066..61a6998 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -6,7 +6,7 @@ use std::{
process::exit,
};
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
use inquire::{Confirm, Text};
use serde::Deserialize;
@@ -46,9 +46,9 @@ impl Config {
create_config_file(&config_path)?;
}
let content = read_to_string(&config_path)
- .map_err(|err| anyhow!("Failed to load config at {}, {err}", config_path.display()))?;
- let mut config: Config =
- serde_yaml::from_str(&content).map_err(|err| anyhow!("Invalid config, {err}"))?;
+ .with_context(|| format!("Failed to load config at {}", config_path.display()))?;
+ let mut config: Config = serde_yaml::from_str(&content)
+ .with_context(|| format!("Invalid config at {}", config_path.display()))?;
config.load_roles()?;
Ok(config)
}
@@ -91,7 +91,7 @@ impl Config {
.create(true)
.append(true)
.open(&path)
- .map_err(|err| anyhow!("Failed to create/append {}, {err}", path.display()))?;
+ .with_context(|| format!("Failed to create/append {}", path.display()))?;
Some(file)
} else {
None
@@ -144,9 +144,9 @@ impl Config {
return Ok(());
}
let content = read_to_string(&path)
- .map_err(|err| anyhow!("Failed to load roles at {}, {err}", path.display()))?;
+ .with_context(|| format!("Failed to load roles at {}", path.display()))?;
let roles: Vec<Role> =
- serde_yaml::from_str(&content).map_err(|err| anyhow!("Invalid roles config, {err}"))?;
+ serde_yaml::from_str(&content).with_context(|| "Invalid roles config")?;
self.roles = roles;
Ok(())
}
@@ -192,7 +192,6 @@ fn create_config_file(config_path: &Path) -> Result<()> {
raw_config.push_str("save: true\n");
}
- std::fs::write(config_path, raw_config)
- .map_err(|err| anyhow!("Failed to write to config file, {err}"))?;
+ std::fs::write(config_path, raw_config).with_context(|| "Failed to write to config file")?;
Ok(())
}
diff --git a/src/main.rs b/src/main.rs
index 8dfa2fd..917a20f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,14 +20,7 @@ use clap::Parser;
use render::MarkdownRender;
use repl::{Repl, ReplCmdHandler};
-fn main() {
- if let Err(err) = start() {
- eprintln!("error: {err}");
- exit(1);
- }
-}
-
-fn start() -> Result<()> {
+fn main() -> Result<()> {
let cli = Cli::parse();
let text = cli.text();
let config = Arc::new(Config::init(text.is_none())?);
diff --git a/src/repl.rs b/src/repl.rs
index cd9e990..2a5947f 100644
--- a/src/repl.rs
+++ b/src/repl.rs
@@ -3,7 +3,7 @@ use crate::config::{Config, Role};
use crate::render::{self, MarkdownRender};
use crate::term;
use crate::utils::{copy, dump};
-use anyhow::{anyhow, Result};
+use anyhow::{Context, Result};
use crossbeam::channel::{unbounded, Sender};
use crossbeam::sync::WaitGroup;
use reedline::{
@@ -69,8 +69,7 @@ impl Repl {
handler.ctrlc.store(false, Ordering::SeqCst);
current_ctrlc = true
}
- let sig = self.editor.read_line(&self.prompt);
- match sig {
+ match self.editor.read_line(&self.prompt) {
Ok(Signal::Success(line)) => {
current_ctrlc = false;
match self.handle_line(handler.clone(), line) {
@@ -80,7 +79,8 @@ impl Repl {
}
}
Err(err) => {
- dump(format!("{err:?}"), 1);
+ let err = format!("{err:?}");
+ dump(err.trim(), 2);
}
}
}
@@ -118,9 +118,7 @@ impl Repl {
Some("screen") => term::clear_screen(0)?,
Some("history") => {
let history = Box::new(self.editor.history_mut());
- history
- .clear()
- .map_err(|err| anyhow!("Failed to clear history, {err}"))?;
+ history.clear().with_context(|| "Failed to clear history")?;
dump("", 1);
}
Some("role") => handler.handle(ReplCmd::ClearRole)?,
@@ -205,7 +203,7 @@ impl Repl {
fn create_history() -> Result<Box<FileBackedHistory>> {
Ok(Box::new(
FileBackedHistory::with_file(1000, Config::history_file()?)
- .map_err(|err| anyhow!("Failed to setup history file, {err}"))?,
+ .with_context(|| "Failed to setup history file")?,
))
}
}
diff --git a/src/utils.rs b/src/utils.rs
index b248d92..5bc1237 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -9,14 +9,15 @@ pub fn dump<T: ToString>(text: T, newlines: usize) {
}
pub fn copy(src: &str) -> Result<()> {
- let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
- ctx.set_contents(src.to_string())
- .map_err(|err| anyhow!("{err}"))
+ ClipboardContext::new()
+ .and_then(|mut ctx| ctx.set_contents(src.to_string()))
+ .map_err(|err| anyhow!("Failed to copy, {err}"))
}
pub fn paste() -> Result<String> {
- let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
- ctx.get_contents().map_err(|err| anyhow!("{err}"))
+ ClipboardContext::new()
+ .and_then(|mut ctx| ctx.get_contents())
+ .map_err(|err| anyhow!("Failed to paste, {err}"))
}
pub fn now() -> String {