diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs index fe080a5..ade1f75 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 82c5901..714b2d0 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 3d38cd2..0c15c9d 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 1131fac..a45d83c 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 5475db5..396d11f 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 632caff..a4b03f2 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 }) }