diff --git a/src/file/env.rs b/src/file/env.rs index 83c6006..005ced2 100644 --- a/src/file/env.rs +++ b/src/file/env.rs @@ -1,13 +1,26 @@ -use std::fs::File; +use std::fs::{File, OpenOptions}; +use std::io::Write; pub struct FileEnv { pub open: FileOpen, + pub append_line: FileAppendLine, } impl Default for FileEnv { fn default() -> Self { Self { open: |path| File::open(path), + append_line: |file_name, line| { + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(file_name) + .unwrap(); + writeln!(file, "{}", line)?; + Ok(()) + }, } } } pub type FileOpen = fn(path: &str) -> std::io::Result; +pub type FileAppendLine = fn(paht: &str, line: &str) -> std::io::Result<()>; diff --git a/src/history/add.rs b/src/history/add.rs index 580c219..90e2628 100644 --- a/src/history/add.rs +++ b/src/history/add.rs @@ -1,19 +1,10 @@ +use crate::file::FileEnv; use crate::prelude::*; -use std::fs::OpenOptions; -use std::io::prelude::*; - use super::Link; -pub fn add(link: &Link, file_name: &str) -> Result<()> { - let mut file = OpenOptions::new() - .write(true) - .append(true) - .create(true) - .open(file_name) - .unwrap(); - - writeln!(file, "{}", link.href)?; +pub fn add(link: &Link, file_name: &str, e: &FileEnv) -> Result<()> { + (e.append_line)(file_name, &link.href)?; Ok(()) } @@ -43,7 +34,7 @@ mod tests { length: None, }; //when - add(&link, &file_name)?; + add(&link, &file_name, &FileEnv::default())?; //then let content: Vec = read_text_file(&file_name)?; @@ -72,7 +63,7 @@ mod tests { length: None, }; //when - add(&link, &file_name)?; + add(&link, &file_name, &FileEnv::default())?; //then let content: Vec = read_text_file(&file_name)?; diff --git a/src/lib.rs b/src/lib.rs index 57fab72..8f4d03a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> if !history::find(&link, history, &e.file)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); (e.fetch.download)(&link)?; - history::add(&link, history)?; + history::add(&link, history, &e.file)?; } } }