Add history param and pass entire Args to FileEnv::create

This commit is contained in:
Paul Campbell 2023-08-06 13:54:13 +01:00
parent d109d73751
commit 61ebddf83b
6 changed files with 47 additions and 16 deletions

View file

@ -1,3 +1,4 @@
use crate::params::Args;
use crate::prelude::*;
use std::fs::{File, OpenOptions};
@ -12,13 +13,13 @@ pub struct FileEnv {
pub append_line: FileAppendLineFn,
}
impl FileEnv {
pub fn create(directory: String) -> Self {
let open_dir = directory.clone();
let append_dir = directory.clone();
pub fn create(a: Args) -> Self {
let open_dir = a.downloads.clone();
let append_dir = a.downloads.clone();
Self {
open: Box::new(move |file_name| {
let path = format!("{}/{}", &open_dir, file_name);
let file = File::open(&path)?;
let file = File::open(path)?;
Ok(file)
}),
append_line: Box::new(move |file_name, line| {
@ -27,7 +28,7 @@ impl FileEnv {
.write(true)
.append(true)
.create(true)
.open(&path)
.open(path)
.unwrap();
writeln!(file, "{}", line)?;
Ok(())

View file

@ -18,7 +18,7 @@ pub fn lines_from(file_name: &str, e: &FileEnv) -> Result<Vec<String>> {
#[cfg(test)]
mod tests {
use crate::test_utils::create_text_file;
use crate::{params::Args, test_utils::create_text_file};
use super::*;
@ -30,7 +30,10 @@ mod tests {
file_name,
include_bytes!("../../test/data/subscriptions.txt"),
)?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
let file_env = FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when
let result = lines_from(file_name, &file_env)?;
@ -49,7 +52,10 @@ mod tests {
file_name,
include_bytes!("../../test/data/subscriptions-blank-line.txt"),
)?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
let file_env = FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when
let result = lines_from(file_name, &file_env)?;
@ -68,7 +74,10 @@ mod tests {
file_name,
include_bytes!("../../test/data/subscriptions-comment.txt"),
)?;
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
let file_env = FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
});
//when
let result = lines_from(file_name, &file_env)?;

View file

@ -13,6 +13,7 @@ mod tests {
use crate::{
history::Link,
params::Args,
test_utils::{create_text_file, read_text_file},
};
@ -38,7 +39,10 @@ mod tests {
add(
&link,
file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()),
&FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?;
//then
@ -69,7 +73,10 @@ mod tests {
add(
&link,
file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()),
&FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?;
//then

View file

@ -18,7 +18,7 @@ pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result<bool> {
#[cfg(test)]
mod test {
use crate::{history::Link, test_utils::create_text_file};
use crate::{history::Link, params::Args, test_utils::create_text_file};
use super::*;
#[test]
@ -38,7 +38,10 @@ mod test {
let result = find(
&link,
file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()),
&FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?;
//then
@ -70,7 +73,10 @@ mod test {
let result = find(
&link,
file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()),
&FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?;
//then
@ -102,7 +108,10 @@ mod test {
let result = find(
&link,
file_name,
&FileEnv::create(dir.path().to_string_lossy().to_string()),
&FileEnv::create(Args {
downloads: dir.path().to_string_lossy().to_string(),
history: "downloaded.txt".to_string(),
}),
)?;
//then

View file

@ -18,7 +18,7 @@ fn main() -> Result<()> {
site,
podal::Env {
network: NetworkEnv::default(),
file: FileEnv::create(args.downloads),
file: FileEnv::create(args),
},
)?;

View file

@ -7,4 +7,9 @@ pub struct Args {
/// Defaults to the current directory
#[arg(short, long, default_value = ".")]
pub downloads: String,
/// The name of the history file.
/// Defaults to "downloaded.txt" located in the downloads directory.
#[arg(long, default_value = "downloaded.txt")]
pub history: String,
}