From bce6906380c922847e0ea20cdeca938aec2ec5ed Mon Sep 17 00:00:00 2001 From: Dieter Eickstaedt Date: Tue, 19 Sep 2023 13:59:45 +0200 Subject: [PATCH] refactor: Duplications reduced in order to align implementations of reading history files --- atuin-client/src/import/bash.rs | 8 +++----- atuin-client/src/import/fish.rs | 7 +++---- atuin-client/src/import/mod.rs | 8 ++++++++ atuin-client/src/import/nu.rs | 8 +++----- atuin-client/src/import/resh.rs | 8 +++----- atuin-client/src/import/zsh.rs | 8 +++----- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs index fe080a55f2f..ade1f751f84 100644 --- a/atuin-client/src/import/bash.rs +++ b/atuin-client/src/import/bash.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Read, path::PathBuf, str}; +use std::{path::PathBuf, str}; use async_trait::async_trait; use directories::UserDirs; @@ -8,6 +8,7 @@ use time::{Duration, OffsetDateTime}; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; +use crate::import::read_to_end; #[derive(Debug)] pub struct Bash { @@ -26,10 +27,7 @@ impl Importer for Bash { const NAME: &'static str = "bash"; async fn new() -> Result { - let mut bytes = Vec::new(); - let path = get_histpath(default_histpath)?; - let mut f = File::open(path)?; - f.read_to_end(&mut bytes)?; + let bytes = read_to_end(get_histpath(default_histpath)?)?; Ok(Self { bytes }) } diff --git a/atuin-client/src/import/fish.rs b/atuin-client/src/import/fish.rs index 82c5901390b..714b2d01230 100644 --- a/atuin-client/src/import/fish.rs +++ b/atuin-client/src/import/fish.rs @@ -1,7 +1,7 @@ // import old shell history! // automatically hoover up all that we can find -use std::{fs::File, io::Read, path::PathBuf}; +use std::path::PathBuf; use async_trait::async_trait; use directories::BaseDirs; @@ -10,6 +10,7 @@ use time::OffsetDateTime; use super::{unix_byte_lines, Importer, Loader}; use crate::history::History; +use crate::import::read_to_end; #[derive(Debug)] pub struct Fish { @@ -48,9 +49,7 @@ impl Importer for Fish { const NAME: &'static str = "fish"; async fn new() -> Result { - let mut bytes = Vec::new(); - let mut f = File::open(default_histpath()?)?; - f.read_to_end(&mut bytes)?; + let bytes = read_to_end(default_histpath()?)?; Ok(Self { bytes }) } diff --git a/atuin-client/src/import/mod.rs b/atuin-client/src/import/mod.rs index 3d38cd299a9..0c15c9dd284 100644 --- a/atuin-client/src/import/mod.rs +++ b/atuin-client/src/import/mod.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::io::Read; use std::path::PathBuf; use async_trait::async_trait; @@ -74,6 +76,12 @@ where } } +fn read_to_end(path: PathBuf) -> Result> { + let mut bytes = Vec::new(); + let mut f = File::open(path)?; + f.read_to_end(&mut bytes)?; + Ok(bytes) +} fn is_file(p: PathBuf) -> Result { if p.is_file() { Ok(p) diff --git a/atuin-client/src/import/nu.rs b/atuin-client/src/import/nu.rs index 1131fac5afe..a45d83c5ee9 100644 --- a/atuin-client/src/import/nu.rs +++ b/atuin-client/src/import/nu.rs @@ -1,7 +1,7 @@ // import old shell history! // automatically hoover up all that we can find -use std::{fs::File, io::Read, path::PathBuf}; +use std::path::PathBuf; use async_trait::async_trait; use directories::BaseDirs; @@ -10,6 +10,7 @@ use time::OffsetDateTime; use super::{unix_byte_lines, Importer, Loader}; use crate::history::History; +use crate::import::read_to_end; #[derive(Debug)] pub struct Nu { @@ -33,10 +34,7 @@ impl Importer for Nu { const NAME: &'static str = "nu"; async fn new() -> Result { - let mut bytes = Vec::new(); - let path = get_histpath()?; - let mut f = File::open(path)?; - f.read_to_end(&mut bytes)?; + let bytes = read_to_end(get_histpath()?)?; Ok(Self { bytes }) } diff --git a/atuin-client/src/import/resh.rs b/atuin-client/src/import/resh.rs index 5475db51d33..396d11fdfb1 100644 --- a/atuin-client/src/import/resh.rs +++ b/atuin-client/src/import/resh.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::Read, path::PathBuf}; +use std::path::PathBuf; use async_trait::async_trait; use directories::UserDirs; @@ -10,6 +10,7 @@ use time::OffsetDateTime; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; +use crate::import::read_to_end; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -84,10 +85,7 @@ impl Importer for Resh { const NAME: &'static str = "resh"; async fn new() -> Result { - let mut bytes = Vec::new(); - let path = get_histpath(default_histpath)?; - let mut f = File::open(path)?; - f.read_to_end(&mut bytes)?; + let bytes = read_to_end(get_histpath(default_histpath)?)?; Ok(Self { bytes }) } diff --git a/atuin-client/src/import/zsh.rs b/atuin-client/src/import/zsh.rs index 632caff950f..a4b03f2d890 100644 --- a/atuin-client/src/import/zsh.rs +++ b/atuin-client/src/import/zsh.rs @@ -1,7 +1,7 @@ // import old shell history! // automatically hoover up all that we can find -use std::{fs::File, io::Read, path::PathBuf}; +use std::path::PathBuf; use async_trait::async_trait; use directories::UserDirs; @@ -10,6 +10,7 @@ use time::OffsetDateTime; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; +use crate::import::read_to_end; #[derive(Debug)] pub struct Zsh { @@ -46,10 +47,7 @@ impl Importer for Zsh { const NAME: &'static str = "zsh"; async fn new() -> Result { - let mut bytes = Vec::new(); - let path = get_histpath(default_histpath)?; - let mut f = File::open(path)?; - f.read_to_end(&mut bytes)?; + let bytes = read_to_end(get_histpath(default_histpath)?)?; Ok(Self { bytes }) }