summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDieter Eickstaedt <eickstaedt@deicon.de>2023-09-23 10:56:55 +0200
committerGitHub <noreply@github.com>2023-09-23 08:56:55 +0000
commitfbed2862fda127b747718e4ae6f5f36a56f29a51 (patch)
treed34481f59f05aa5f9e0131c416373e5c1278dc17
parentcea68660fd43ecdefa735f027199000cc6a60aad (diff)
refactor: Duplications reduced in order to align implementations of reading history files (#1247)
-rw-r--r--atuin-client/src/import/bash.rs8
-rw-r--r--atuin-client/src/import/fish.rs7
-rw-r--r--atuin-client/src/import/mod.rs8
-rw-r--r--atuin-client/src/import/nu.rs8
-rw-r--r--atuin-client/src/import/resh.rs8
-rw-r--r--atuin-client/src/import/zsh.rs8
6 files changed, 23 insertions, 24 deletions
diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs
index fe080a55..ade1f751 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<Self> {
- 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 82c59013..714b2d01 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<Self> {
- 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 3d38cd29..0c15c9dd 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<Vec<u8>> {
+ 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<PathBuf> {
if p.is_file() {
Ok(p)
diff --git a/atuin-client/src/import/nu.rs b/atuin-client/src/import/nu.rs
index 1131fac5..a45d83c5 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<Self> {
- 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 5475db51..396d11fd 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<Self> {
- 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 632caff9..a4b03f2d 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<Self> {
- 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 })
}