From 48747e3b7c542c696003f71ef4b4ae457934e57c Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Thu, 21 Apr 2022 08:05:57 +0100 Subject: [PATCH] A few minor tweaks (#314) * use bail macro replace client database errors remove dead code * fix test --- atuin-client/src/api_client.rs | 10 +++---- atuin-client/src/database.rs | 7 ++--- atuin-client/src/encryption.rs | 2 +- atuin-client/src/history.rs | 21 +------------- atuin-client/src/import/fish.rs | 50 ++++++++++++--------------------- src/command/stats.rs | 4 +-- src/main.rs | 1 - 7 files changed, 30 insertions(+), 65 deletions(-) diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 87c4b6a..171012a 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use chrono::Utc; -use eyre::{eyre, Result}; +use eyre::{bail, Result}; use reqwest::header::{HeaderMap, AUTHORIZATION, USER_AGENT}; use reqwest::{StatusCode, Url}; use sodiumoxide::crypto::secretbox; @@ -41,7 +41,7 @@ pub async fn register( let resp = reqwest::blocking::get(url)?; if resp.status().is_success() { - return Err(eyre!("username already in use")); + bail!("username already in use"); } let url = format!("{}/register", address); @@ -54,7 +54,7 @@ pub async fn register( .await?; if !resp.status().is_success() { - return Err(eyre!("failed to register user")); + bail!("failed to register user"); } let session = resp.json::().await?; @@ -73,7 +73,7 @@ pub async fn login(address: &str, req: LoginRequest) -> Result { .await?; if resp.status() != reqwest::StatusCode::OK { - return Err(eyre!("invalid login details")); + bail!("invalid login details"); } let session = resp.json::().await?; @@ -102,7 +102,7 @@ impl<'a> Client<'a> { let resp = self.client.get(url).send().await?; if resp.status() != StatusCode::OK { - return Err(eyre!("failed to get count (are you logged in?)")); + bail!("failed to get count (are you logged in?)"); } let count = resp.json::().await?; diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 8b950de..9efde2c 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -5,15 +5,14 @@ use async_trait::async_trait; use chrono::prelude::*; use chrono::Utc; -use eyre::Result; use itertools::Itertools; use regex::Regex; use fs_err as fs; -use sqlx::sqlite::{ - SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow, +use sqlx::{ + sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow}, + Result, Row, }; -use sqlx::Row; use super::history::History; use super::ordering; diff --git a/atuin-client/src/encryption.rs b/atuin-client/src/encryption.rs index d91ad07..9b6f8a7 100644 --- a/atuin-client/src/encryption.rs +++ b/atuin-client/src/encryption.rs @@ -139,6 +139,6 @@ mod test { }; // this should err - decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key"); + let _ = decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key"); } } diff --git a/atuin-client/src/history.rs b/atuin-client/src/history.rs index 92e92dd..111e463 100644 --- a/atuin-client/src/history.rs +++ b/atuin-client/src/history.rs @@ -1,12 +1,11 @@ use std::env; -use std::hash::{Hash, Hasher}; use chrono::Utc; use atuin_common::utils::uuid_v4; // Any new fields MUST be Optional<>! -#[derive(Debug, Clone, Serialize, Deserialize, Ord, PartialOrd, sqlx::FromRow)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, sqlx::FromRow)] pub struct History { pub id: String, pub timestamp: chrono::DateTime, @@ -46,21 +45,3 @@ impl History { } } } - -impl PartialEq for History { - // for the sakes of listing unique history only, we do not care about - // anything else - // obviously this does not refer to the *same* item of history, but when - // we only render the command, it looks the same - fn eq(&self, other: &Self) -> bool { - self.command == other.command - } -} - -impl Eq for History {} - -impl Hash for History { - fn hash(&self, state: &mut H) { - self.command.hash(state); - } -} diff --git a/atuin-client/src/import/fish.rs b/atuin-client/src/import/fish.rs index 4079e12..7063926 100644 --- a/atuin-client/src/import/fish.rs +++ b/atuin-client/src/import/fish.rs @@ -144,26 +144,9 @@ impl Iterator for Fish { #[cfg(test)] mod test { - use chrono::{TimeZone, Utc}; use std::io::Cursor; use super::Fish; - use crate::history::History; - - // simple wrapper for fish history entry - macro_rules! fishtory { - ($timestamp:literal, $command:literal) => { - History::new( - Utc.timestamp($timestamp, 0), - $command.into(), - "unknown".into(), - -1, - -1, - None, - None, - ) - }; - } #[test] fn parse_complex() { @@ -201,21 +184,24 @@ ERROR - ~/.local/share/fish/fish_history "#; let cursor = Cursor::new(input); - let fish = Fish::new(cursor).unwrap(); + let mut fish = Fish::new(cursor).unwrap(); - let history = fish.collect::, _>>().unwrap(); - assert_eq!( - history, - vec![ - fishtory!(1639162832, "history --help"), - fishtory!(1639162851, "cat ~/.bash_history"), - fishtory!(1639162890, "ls ~/.local/share/fish/fish_history"), - fishtory!(1639162893, "cat ~/.local/share/fish/fish_history"), - fishtory!(1639162933, "echo \"foo\" \\\n'bar' baz"), - fishtory!(1639162939, "cat ~/.local/share/fish/fish_history"), - fishtory!(1639163063, r#"echo "\"" \\ "\\""#), - fishtory!(1639163066, "cat ~/.local/share/fish/fish_history"), - ] - ); + // simple wrapper for fish history entry + macro_rules! fishtory { + ($timestamp:expr, $command:expr) => { + let h = fish.next().expect("missing entry in history").unwrap(); + assert_eq!(h.command.as_str(), $command); + assert_eq!(h.timestamp.timestamp(), $timestamp); + }; + } + + fishtory!(1639162832, "history --help"); + fishtory!(1639162851, "cat ~/.bash_history"); + fishtory!(1639162890, "ls ~/.local/share/fish/fish_history"); + fishtory!(1639162893, "cat ~/.local/share/fish/fish_history"); + fishtory!(1639162933, "echo \"foo\" \\\n'bar' baz"); + fishtory!(1639162939, "cat ~/.local/share/fish/fish_history"); + fishtory!(1639163063, r#"echo "\"" \\ "\\""#); + fishtory!(1639163066, "cat ~/.local/share/fish/fish_history"); } } diff --git a/src/command/stats.rs b/src/command/stats.rs index d7bddc8..6d342c1 100644 --- a/src/command/stats.rs +++ b/src/command/stats.rs @@ -6,7 +6,7 @@ use chrono_english::parse_date_string; use clap::Parser; use cli_table::{format::Justify, print_stdout, Cell, Style, Table}; -use eyre::{eyre, Result}; +use eyre::{bail, Result}; use atuin_client::database::Database; use atuin_client::history::History; @@ -32,7 +32,7 @@ fn compute_stats(history: &[History]) -> Result<()> { let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1)); if most_common_command.is_none() { - return Err(eyre!("No commands found")); + bail!("No commands found"); } let table = vec![ diff --git a/src/main.rs b/src/main.rs index d5a1e82..2fee879 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use eyre::Result; extern crate log; use command::AtuinCmd; - mod command; const VERSION: &str = env!("CARGO_PKG_VERSION");