summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConrad Ludgate <conradludgate@gmail.com>2021-02-14 18:00:41 +0000
committerGitHub <noreply@github.com>2021-02-14 18:00:41 +0000
commitbae59474eef3fd28758a2a4e5e4fb8d50c93a3c4 (patch)
treed0c85ade3768dc9b04f6d94c9d9b5c360a17c257
parent72c5ea79147f7bd486839cfeb4aab62a98a0bdd2 (diff)
a few more tiny touch ups (#7)
* a few more tiny touch ups * all praise clippy
-rw-r--r--src/command/mod.rs19
-rw-r--r--src/local/database.rs11
-rw-r--r--src/local/history.rs6
-rw-r--r--src/local/import.rs7
-rw-r--r--src/main.rs22
5 files changed, 34 insertions, 31 deletions
diff --git a/src/command/mod.rs b/src/command/mod.rs
index 8d463bd6..4ac62385 100644
--- a/src/command/mod.rs
+++ b/src/command/mod.rs
@@ -1,4 +1,8 @@
+use eyre::Result;
use structopt::StructOpt;
+use uuid::Uuid;
+
+use crate::local::database::Sqlite;
mod history;
mod import;
@@ -21,3 +25,18 @@ pub enum AtuinCmd {
#[structopt(about = "generates a UUID")]
Uuid,
}
+
+impl AtuinCmd {
+ pub fn run(self, db: &mut Sqlite) -> Result<()> {
+ match self {
+ Self::History(history) => history.run(db),
+ Self::Import(import) => import.run(db),
+ Self::Server(server) => server.run(),
+
+ Self::Uuid => {
+ println!("{}", Uuid::new_v4().to_simple().to_string());
+ Ok(())
+ }
+ }
+ }
+}
diff --git a/src/local/database.rs b/src/local/database.rs
index e2df9ba5..8e4b00ef 100644
--- a/src/local/database.rs
+++ b/src/local/database.rs
@@ -1,7 +1,7 @@
use chrono::Utc;
use std::path::Path;
-use eyre::{eyre, Result};
+use eyre::Result;
use rusqlite::{params, Connection};
use rusqlite::{Transaction, NO_PARAMS};
@@ -125,16 +125,11 @@ impl Database for Sqlite {
where id = ?1",
)?;
- let mut iter = stmt.query_map(params![id], |row| {
+ let history = stmt.query_row(params![id], |row| {
history_from_sqlite_row(Some(id.to_string()), row)
})?;
- let history = iter.next().unwrap();
-
- match history {
- Ok(i) => Ok(i),
- Err(e) => Err(eyre!("could not find item: {}", e)),
- }
+ Ok(history)
}
fn update(&self, h: &History) -> Result<()> {
diff --git a/src/local/history.rs b/src/local/history.rs
index 06a350fc..d88353f9 100644
--- a/src/local/history.rs
+++ b/src/local/history.rs
@@ -24,9 +24,9 @@ impl History {
session: Option<String>,
hostname: Option<String>,
) -> Self {
- let session = session.unwrap_or_else(|| {
- env::var("ATUIN_SESSION").unwrap_or_else(|_| Uuid::new_v4().to_simple().to_string())
- });
+ let session = session
+ .or_else(|| env::var("ATUIN_SESSION").ok())
+ .unwrap_or_else(|| Uuid::new_v4().to_simple().to_string());
let hostname =
hostname.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
diff --git a/src/local/import.rs b/src/local/import.rs
index 858e5786..ddccc75a 100644
--- a/src/local/import.rs
+++ b/src/local/import.rs
@@ -4,9 +4,9 @@
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::{fs::File, path::Path};
-use eyre::{eyre, Result};
+use eyre::{Result, WrapErr};
-use crate::local::history::History;
+use super::history::History;
#[derive(Debug)]
pub struct Zsh {
@@ -72,8 +72,6 @@ impl Iterator for Zsh {
match self.file.read_line(&mut line) {
Ok(0) => None,
- Err(e) => Some(Err(eyre!("failed to parse line: {}", e))),
-
Ok(_) => {
let extended = line.starts_with(':');
@@ -91,6 +89,7 @@ impl Iterator for Zsh {
)))
}
}
+ Err(e) => Some(Err(e).wrap_err("failed to parse line")),
}
}
}
diff --git a/src/main.rs b/src/main.rs
index 2dbeabfe..21241b83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,6 @@ use std::path::PathBuf;
use directories::ProjectDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;
-use uuid::Uuid;
#[macro_use]
extern crate log;
@@ -46,26 +45,17 @@ impl Atuin {
let path = shellexpand::full(path)?;
PathBuf::from(path.as_ref())
} else {
- let project_dirs =
- ProjectDirs::from("com", "elliehuxtable", "atuin").ok_or_else(|| {
+ ProjectDirs::from("com", "elliehuxtable", "atuin")
+ .ok_or_else(|| {
eyre!("could not determine db file location\nspecify one using the --db flag")
- })?;
- let root = project_dirs.data_dir();
- root.join("history.db")
+ })?
+ .data_dir()
+ .join("history.db")
};
let mut db = Sqlite::new(db_path)?;
- match self.atuin {
- AtuinCmd::History(history) => history.run(&mut db),
- AtuinCmd::Import(import) => import.run(&mut db),
- AtuinCmd::Server(server) => server.run(),
-
- AtuinCmd::Uuid => {
- println!("{}", Uuid::new_v4().to_simple().to_string());
- Ok(())
- }
- }
+ self.atuin.run(&mut db)
}
}