Skip to content

Commit

Permalink
refactor: Duplications reduced in order to align implementations of r…
Browse files Browse the repository at this point in the history
…eading history files (#1247)
  • Loading branch information
deicon authored Sep 23, 2023
1 parent cea6866 commit fbed286
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
8 changes: 3 additions & 5 deletions atuin-client/src/import/bash.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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 })
}

Expand Down
7 changes: 3 additions & 4 deletions atuin-client/src/import/fish.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 })
}

Expand Down
8 changes: 8 additions & 0 deletions atuin-client/src/import/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

use async_trait::async_trait;
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions atuin-client/src/import/nu.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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 })
}

Expand Down
8 changes: 3 additions & 5 deletions atuin-client/src/import/resh.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File, io::Read, path::PathBuf};
use std::path::PathBuf;

use async_trait::async_trait;
use directories::UserDirs;
Expand All @@ -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")]
Expand Down Expand Up @@ -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 })
}

Expand Down
8 changes: 3 additions & 5 deletions atuin-client/src/import/zsh.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 })
}

Expand Down

1 comment on commit fbed286

@vercel
Copy link

@vercel vercel bot commented on fbed286 Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

atuin-docs – ./

atuin-docs-git-main-atuin.vercel.app
atuin-docs.vercel.app
atuin-docs-atuin.vercel.app

Please sign in to comment.