refactor: Duplications reduced in order to align implementations of reading history files (#1247)

This commit is contained in:
Dieter Eickstaedt 2023-09-23 10:56:55 +02:00 committed by GitHub
parent cea68660fd
commit fbed2862fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 24 deletions

View file

@ -1,4 +1,4 @@
use std::{fs::File, io::Read, path::PathBuf, str}; use std::{path::PathBuf, str};
use async_trait::async_trait; use async_trait::async_trait;
use directories::UserDirs; use directories::UserDirs;
@ -8,6 +8,7 @@ use time::{Duration, OffsetDateTime};
use super::{get_histpath, unix_byte_lines, Importer, Loader}; use super::{get_histpath, unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
use crate::import::read_to_end;
#[derive(Debug)] #[derive(Debug)]
pub struct Bash { pub struct Bash {
@ -26,10 +27,7 @@ impl Importer for Bash {
const NAME: &'static str = "bash"; const NAME: &'static str = "bash";
async fn new() -> Result<Self> { async fn new() -> Result<Self> {
let mut bytes = Vec::new(); let bytes = read_to_end(get_histpath(default_histpath)?)?;
let path = get_histpath(default_histpath)?;
let mut f = File::open(path)?;
f.read_to_end(&mut bytes)?;
Ok(Self { bytes }) Ok(Self { bytes })
} }

View file

@ -1,7 +1,7 @@
// import old shell history! // import old shell history!
// automatically hoover up all that we can find // 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 async_trait::async_trait;
use directories::BaseDirs; use directories::BaseDirs;
@ -10,6 +10,7 @@ use time::OffsetDateTime;
use super::{unix_byte_lines, Importer, Loader}; use super::{unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
use crate::import::read_to_end;
#[derive(Debug)] #[derive(Debug)]
pub struct Fish { pub struct Fish {
@ -48,9 +49,7 @@ impl Importer for Fish {
const NAME: &'static str = "fish"; const NAME: &'static str = "fish";
async fn new() -> Result<Self> { async fn new() -> Result<Self> {
let mut bytes = Vec::new(); let bytes = read_to_end(default_histpath()?)?;
let mut f = File::open(default_histpath()?)?;
f.read_to_end(&mut bytes)?;
Ok(Self { bytes }) Ok(Self { bytes })
} }

View file

@ -1,3 +1,5 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use async_trait::async_trait; 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> { fn is_file(p: PathBuf) -> Result<PathBuf> {
if p.is_file() { if p.is_file() {
Ok(p) Ok(p)

View file

@ -1,7 +1,7 @@
// import old shell history! // import old shell history!
// automatically hoover up all that we can find // 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 async_trait::async_trait;
use directories::BaseDirs; use directories::BaseDirs;
@ -10,6 +10,7 @@ use time::OffsetDateTime;
use super::{unix_byte_lines, Importer, Loader}; use super::{unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
use crate::import::read_to_end;
#[derive(Debug)] #[derive(Debug)]
pub struct Nu { pub struct Nu {
@ -33,10 +34,7 @@ impl Importer for Nu {
const NAME: &'static str = "nu"; const NAME: &'static str = "nu";
async fn new() -> Result<Self> { async fn new() -> Result<Self> {
let mut bytes = Vec::new(); let bytes = read_to_end(get_histpath()?)?;
let path = get_histpath()?;
let mut f = File::open(path)?;
f.read_to_end(&mut bytes)?;
Ok(Self { bytes }) Ok(Self { bytes })
} }

View file

@ -1,4 +1,4 @@
use std::{fs::File, io::Read, path::PathBuf}; use std::path::PathBuf;
use async_trait::async_trait; use async_trait::async_trait;
use directories::UserDirs; use directories::UserDirs;
@ -10,6 +10,7 @@ use time::OffsetDateTime;
use super::{get_histpath, unix_byte_lines, Importer, Loader}; use super::{get_histpath, unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
use crate::import::read_to_end;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -84,10 +85,7 @@ impl Importer for Resh {
const NAME: &'static str = "resh"; const NAME: &'static str = "resh";
async fn new() -> Result<Self> { async fn new() -> Result<Self> {
let mut bytes = Vec::new(); let bytes = read_to_end(get_histpath(default_histpath)?)?;
let path = get_histpath(default_histpath)?;
let mut f = File::open(path)?;
f.read_to_end(&mut bytes)?;
Ok(Self { bytes }) Ok(Self { bytes })
} }

View file

@ -1,7 +1,7 @@
// import old shell history! // import old shell history!
// automatically hoover up all that we can find // 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 async_trait::async_trait;
use directories::UserDirs; use directories::UserDirs;
@ -10,6 +10,7 @@ use time::OffsetDateTime;
use super::{get_histpath, unix_byte_lines, Importer, Loader}; use super::{get_histpath, unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
use crate::import::read_to_end;
#[derive(Debug)] #[derive(Debug)]
pub struct Zsh { pub struct Zsh {
@ -46,10 +47,7 @@ impl Importer for Zsh {
const NAME: &'static str = "zsh"; const NAME: &'static str = "zsh";
async fn new() -> Result<Self> { async fn new() -> Result<Self> {
let mut bytes = Vec::new(); let bytes = read_to_end(get_histpath(default_histpath)?)?;
let path = get_histpath(default_histpath)?;
let mut f = File::open(path)?;
f.read_to_end(&mut bytes)?;
Ok(Self { bytes }) Ok(Self { bytes })
} }