From bae59474eef3fd28758a2a4e5e4fb8d50c93a3c4 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sun, 14 Feb 2021 18:00:41 +0000 Subject: [PATCH] a few more tiny touch ups (#7) * a few more tiny touch ups * all praise clippy --- src/command/mod.rs | 19 +++++++++++++++++++ src/local/database.rs | 11 +++-------- src/local/history.rs | 6 +++--- src/local/import.rs | 7 +++---- src/main.rs | 22 ++++++---------------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/command/mod.rs b/src/command/mod.rs index 8d463bd..4ac6238 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 e2df9ba..8e4b00e 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 06a350f..d88353f 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -24,9 +24,9 @@ impl History { session: Option, hostname: Option, ) -> 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 858e578..ddccc75 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 2dbeabf..21241b8 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) } }