diff options
author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-21 08:05:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-21 08:05:57 +0100 |
commit | 48747e3b7c542c696003f71ef4b4ae457934e57c (patch) | |
tree | 0bbde7136b84cda46cdb758d2cbeda67ca436ee8 | |
parent | ed4e07d2e63af1584a262037781729a5144a5502 (diff) |
A few minor tweaks (#314)
* use bail macro
replace client database errors
remove dead code
* fix test
-rw-r--r-- | atuin-client/src/api_client.rs | 10 | ||||
-rw-r--r-- | atuin-client/src/database.rs | 7 | ||||
-rw-r--r-- | atuin-client/src/encryption.rs | 2 | ||||
-rw-r--r-- | atuin-client/src/history.rs | 21 | ||||
-rw-r--r-- | atuin-client/src/import/fish.rs | 52 | ||||
-rw-r--r-- | src/command/stats.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 1 |
7 files changed, 31 insertions, 66 deletions
diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 87c4b6a4..171012ac 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::<RegisterResponse>().await?; @@ -73,7 +73,7 @@ pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> { .await?; if resp.status() != reqwest::StatusCode::OK { - return Err(eyre!("invalid login details")); + bail!("invalid login details"); } let session = resp.json::<LoginResponse>().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::<CountResponse>().await?; diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 8b950de9..9efde2cd 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 d91ad076..9b6f8a78 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 92e92ddf..111e463a 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<Utc>, @@ -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<H: Hasher>(&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 4079e122..70639264 100644 --- a/atuin-client/src/import/fish.rs +++ b/atuin-client/src/import/fish.rs @@ -144,26 +144,9 @@ impl<R: Read> Iterator for Fish<R> { #[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 history = fish.collect::<Result<Vec<_>, _>>().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"), - ] - ); + let mut fish = Fish::new(cursor).unwrap(); + + // 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 d7bddc82..6d342c19 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 d5a1e823..2fee879b 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"); |